내가

2016-10-19 4 views
-3
ProfileActivity.java; 

    import android.Manifest; 
    import android.content.Intent; 
    import android.content.pm.PackageManager; 
    import android.location.Location; 
    import android.location.LocationListener; 
    import android.location.LocationManager; 
    import android.os.Build; 
    import android.os.Bundle; 
    import android.provider.Settings; 
    import android.support.v4.app.ActivityCompat; 
    import android.support.v7.app.AppCompatActivity; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.EditText; 
    import android.widget.TextView; 
import com.google.firebase.auth.FirebaseAuth; 
import com.google.firebase.auth.FirebaseUser; 

public class ProfileActivity extends AppCompatActivity implements View.OnClickListener { 

//firebase auth object 
    private FirebaseAuth firebaseAuth; 

//view objects 
    public TextView textViewUserEmail; 
    private Button buttonLogout,btnGet; 
    public EditText edtxtLati, edtxtLongi; 
    private LocationManager locationManager; 
    private LocationListener locationListener; 
    private Double lati, longi; 


@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_profile); 


     locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
     locationListener = new LocationListener() { 
      @Override 
      public void onLocationChanged(Location location) { 
       lati = location.getLatitude(); 
       edtxtLati.append("" + lati); 
       longi = location.getLongitude(); 
       edtxtLongi.append("" + longi); 
      } 

      @Override 
      public void onStatusChanged(String provider, int status, Bundle extras) { 

      } 

      @Override 
      public void onProviderEnabled(String provider) { 

      } 

      @Override 
      public void onProviderDisabled(String provider) { 
       Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       startActivity(intent); 
      } 
     }; 
     //VERSION AND PERMISSION CHECKING 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
      { 

       requestPermissions(new String[]{ 
         Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.INTERNET}, 10); 
       return; 
      } 
     } else { 
      configureButton(); 
     } 

     //initializing firebase authentication object 
     firebaseAuth = FirebaseAuth.getInstance(); 

     //if the user is not logged in 
     //that means current user will return null 
     if (firebaseAuth.getCurrentUser() == null) { 
      //closing this activity 
      finish(); 
      //starting login activity 
      startActivity(new Intent(this, LoginActivity.class)); 
     } 

     //getting current user 
     FirebaseUser user = firebaseAuth.getCurrentUser(); 

     //initializing views 
     textViewUserEmail = (TextView) findViewById(R.id.textViewUserEmail); 
     buttonLogout = (Button) findViewById(R.id.buttonLogout); 
     edtxtLati = (EditText)findViewById(R.id.edtxtLati); 
     edtxtLongi = (EditText)findViewById(R.id.edtxtLongi); 
     btnGet = (Button)findViewById(R.id.btnGet); 

     //displaying logged in user name 
     textViewUserEmail.setText("Welcome "+user.getEmail()); 

     //adding listener to button 
     buttonLogout.setOnClickListener(this); 
    } 

    public void onRequestPermissionsResult(int requestCode, String[] permission, int[] grantResults) { 
     switch(requestCode) 
     { 
      case 10: 
       if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) 
        configureButton(); 
     } 

    } 

    private void configureButton() { 
     btnGet.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       locationManager.requestLocationUpdates("gps", 5000, 0, locationListener); 
      } 

     }); 

    } 


    @Override 
    public void onClick(View view) { 
     //if logout is pressed 
     if (view == buttonLogout) { 
      //logging out the user 
      firebaseAuth.signOut(); 
      //closing activity 
      finish(); 
      //starting login activity 
      startActivity(new Intent(this, LoginActivity.class)); 
     } 
    } 
} 

/AndroidRuntime을 무엇을해야 btnGet가 작동하지 않는 버튼 : 치명적인 예외 : 주요 프로세스 : com.praful.lochub, PID : 3742 java.lang.RuntimeException가 : 실패 제공하는 결과 ResultInfo { {act = android.content.pm.action.REQUEST_PERMISSIONS (엑스트라가 있음)}}에서 으로 이동 {com.praful.lochub/com.praful.lochub.ProfileActivity} : java.lang.NullPointerException : 가상 메소드 'void를 호출하려고 시도했습니다. void android.widget.Button.setOnClickListener (android.view.View $ OnCl ickListener) '에서 android.app.ActivityThread.- android.app.ActivityThread.handleSendResult (ActivityThread.java:4096) 에서 android.app.ActivityThread.deliverResults (ActivityThread.java:4053) 에서 널 객체 참조 에 wrap20 (ActivityThread.java) at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1516) at android.os.Handler.dispatchMessage (Handler.java:102) android.os.Looper.loop에서 Looper.java:154) android.app.ActivityThread.main (ActivityThread.java:6077) at java.lang.reflect.Method.invoke (기본 메소드) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller .run (ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:755) 에 의해 발생 : java.lang.NullPointerException : 가상 메서드 'void를 호출하려고 시도'void android.widget.Button.setOnClickListener (android.view. 보기 $ OnClickListener를) '안드로이드에서 com.praful.lochub.ProfileActivity.onRequestPermissionsResult (ProfileActivity.java:115) 에서 com.praful.lochub.ProfileActivity.configureButton (ProfileActivity.java:121) 에서 null 객체 참조 에 .app.Activity.dispatchRequestPermissionsResult (Activity.java:7069) and35.p.Activity.dispatchActivityResult android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1516) 에서 android.app.ActivityThread.-wrap20 (ActivityThread.java) 에서에서 android.app.ActivityThread.handleSendResult (ActivityThread.java:4096) 에서 android.os.Handler.dispatchMessage (Handler.java:102) (android.os.Looper.loop (Looper.java:154) at android.app.ActivityThread.main (ActivityThread.java:6077) at java. img.Invoke (네이티브 메소드) : 755) 응용 프로그램이 종료되었습니다.내가

activity_profile.xml 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.praful.lochub.ProfileActivity" 
    android:background="#ffffff"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Large Text" 
     android:id="@+id/textViewUserEmail" 
     android:layout_gravity="center_horizontal" 
     android:fontFamily="casual" 
     android:textColor="@color/colorAccent" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Logout" 
     android:id="@+id/buttonLogout" 
     android:layout_gravity="center_horizontal" 
     android:fontFamily="casual" 
     android:textColor="@color/common_google_signin_btn_text_dark_default" 
     android:textStyle="normal|bold" 
     android:textAllCaps="false" 
     android:textSize="18sp" 
     android:background="@color/colorAccent" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentTop="true" 
     android:layout_alignBottom="@+id/textViewUserEmail" /> 


    <Button 
     android:text="Get Location" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/btnGet" 
     style="@style/Widget.AppCompat.Button.Colored" 
     android:fontFamily="casual" 
     android:textStyle="normal|bold" 
     android:textAllCaps="false" 
     android:textSize="16sp" 
     android:layout_below="@+id/edtxtLongi" 
     android:layout_centerHorizontal="true" /> 

    <ImageButton 
     android:background="@drawable/showonmap" 
     android:layout_width="50dip" 
     android:layout_height="50dip" 
     android:layout_below="@+id/edtxtLongi" 
     android:layout_alignParentEnd="true" 
     android:layout_marginEnd="12dp" 
     android:id="@+id/imgbtnMap" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="numberDecimal" 
     android:ems="10" 
     android:id="@+id/edtxtLati" 
     android:hint="Latitude" 
     android:textAlignment="center" 
     android:fontFamily="casual" 
     android:layout_marginTop="15dp" 
     android:layout_below="@+id/textViewUserEmail" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentEnd="true" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="numberDecimal" 
     android:ems="10" 
     android:id="@+id/edtxtLongi" 
     android:fontFamily="casual" 
     android:hint="Longitude" 
     android:textAlignment="center" 
     android:layout_below="@+id/edtxtLati" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentEnd="true" /> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@drawable/loc" 
     android:id="@+id/imgbtnLocate" 
     android:background="@null" 
     android:layout_marginBottom="22dp" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" /> 

</RelativeLayout> 
+3

nullpointerexception은 DEBUG를 의미합니다. 디버그를 사용하여 오류를 던지는 줄에서 멈추고, 무엇이 null인지 확인하고 해결하십시오. 문제가있는 예외 상황에서 작성되었습니다. 당신은 버튼 null을 가지고, 그것은 당신이 잘못된 xml id 나 비슷한 것을 가리킨다는 것을 의미합니다. –

답변

3

놓습니다 초기화 : 그것은 전에 btnGet를 사용하기 때문에 당신의에서 onCreate 메소드의 상단에

//initializing views 
textViewUserEmail = (TextView) findViewById(R.id.textViewUserEmail); 
buttonLogout = (Button) findViewById(R.id.buttonLogout); 
edtxtLati = (EditText)findViewById(R.id.edtxtLati); 
edtxtLongi = (EditText)findViewById(R.id.edtxtLongi); 
btnGet = (Button)findViewById(R.id.btnGet); 

바로

setContentView(R.layout.activity_profile); 

귀하의 configureButton() 메소드 후 문제 초기화 됨.이 로그 라인에서 확인할 수 있습니다.

reference at com.praful.lochub.ProfileActivity.configureButton(ProfileActivity.java:121) 
+0

대단히 고마워요. 앱 개발의 초보자이기 때문에 문제가되지 않았다. 실제로 null 포인터 예외가 무엇인지 알았지 만 정확한 오류를 파악하지 못했습니다. 다시 한번 감사드립니다 –

+0

안녕하십니까 @PrafulMishra 이것이 귀하의 질문을 해결했다면 [확인] (http://meta.stackexchange.com/q/5234/179419)을 클릭하여 확인하십시오. 이는 해결책을 찾았으며 응답자와 자신에게 어느 정도의 평판을 제공한다는 것을 더 넓은 커뮤니티에 나타냅니다. 이를 수행 할 의무는 없습니다. –