-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: FATAL EXCEPTION: main Process: com.praful.lochub, PID: 3742 java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=10, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.praful.lochub/com.praful.lochub.ProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at android.app.ActivityThread.deliverResults(ActivityThread.java:4053) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096) at android.app.ActivityThread.-wrap20(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.praful.lochub.ProfileActivity.configureButton(ProfileActivity.java:121) at com.praful.lochub.ProfileActivity.onRequestPermissionsResult(ProfileActivity.java:115) at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:7069) at android.app.Activity.dispatchActivityResult(Activity.java:6921) at android.app.ActivityThread.deliverResults(ActivityThread.java:4049) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096)  at android.app.ActivityThread.-wrap20(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6077)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)  Application terminated.

    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>
Praful
  • 19
  • 5
  • 3
    nullpointerexception means you have to DEBUG. use debug, stop on the line throwing error, see what is null and solve it.. it's written in the exception where the problem is. you have the button null, it means you pointed to the wrong xml id or something similar – Pier Giorgio Misley Oct 19 '16 at 12:30

1 Answers1

3

Place your initialization:

//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);

at the top of your onCreate method right after

setContentView(R.layout.activity_profile);

Your configureButton() method is the issue since it uses btnGet before it's initialized. You can see this from this line of your log:

reference at com.praful.lochub.ProfileActivity.configureButton(ProfileActivity.java:121)
Elias N
  • 1,430
  • 11
  • 19