Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=10, result=-1, data=Intent

EDIT:

Now the app is working. But it only asks for the permission after the first time it is started(after installation). If I leave the app(pressing back button) and then later start it again it doesn't ask for the permission. Why is this happening?

Doesn't pressing the back button destroys the activity by calling onDestroy() and shouldn't it call onCreate() method again?

ORIGINAL QUESTION

My app allows the user to find their current location on the press of the button.Whenever the app is started for the first time, it asks for the user permission but it crashes when the permission is granted giving following error:

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.example.hpi5.location/com.example.hpi5.location.MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
on a null object reference

Below is my Activity code:

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.content.pm.PackageManager;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button button ;
    LocationListener locationListener;
    LocationManager locationManager;

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


        if (Build.VERSION.SDK_INT >=23 ){
            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                        10);
                return;
            }
        }

        button = (Button) findViewById(R.id.button);

        locationManager = (LocationManager)
                getSystemService(LOCATION_SERVICE);

        locationListener = new LocationListener()
        {

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                double latitude = location.getLatitude();
                double longitude = location.getLongitude();
                //double speed = location.getSpeed(); //spe2d in meter/minute
                //speed = (speed*3600)/1000;      // speed in km/minute
                Toast.makeText(getApplicationContext(), "lat" + latitude + "lon" + longitude,Toast.LENGTH_SHORT).show();
            }
        };




    }
    void requestUpdate(){
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
            }
        });

    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String permissions[], @ NonNull int[] grantResults) {
        switch (requestCode) {
            case -1:{
                //hello
            }

            case 10: {
                if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    requestUpdate();
                }
            }

        }
    }



}

Below is the logcat error:

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.example.hpi5.location/com.example.hpi5.location.MainActivity}:
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:3699)
                                                                           at
    android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
                                                                            at android.app.ActivityThread.-wrap16(ActivityThread.java)
     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:148)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                         Caused by: java.lang.NullPointerException: Attempt to invoke virtual
     method 'void
  android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
     on a null object reference
com.example.hpi5.location.MainActivity.requestUpdate(MainActivity.java:80)
     com.example.hpi5.location.MainActivity.onRequestPermissionsResult(MainActivity.java:95)
    android.app.Activity.dispatchRequestPermissionsResult(Activity.java:6553)
                                                                         at android.app.Activity.dispatchActivityResult(Activity.java:6432)
                                                                        at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
 android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 
                                                                        at android.app.ActivityThread.-wrap16(ActivityThread.java) 
 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:148) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                          at java.lang.reflect.Method.invoke(Native Method) 
     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
like image 447
ray an Avatar asked Jan 08 '17 20:01

ray an


2 Answers

Edits made Coarse Permission, button init. onCreate() and break ponit added onRequestPermissionsResult().

As the error is saying is NullPointException caused by null object of Button:

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.content.pm.PackageManager;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

Button button ;
LocationListener locationListener;
LocationManager locationManager;

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

    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            requestUpdate();
        }
    });

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission
                        (this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,},
                    10);
            return;
        }
    }

    locationManager = (LocationManager)
            getSystemService(LOCATION_SERVICE);

    locationListener = new LocationListener()
    {

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
            //double speed = location.getSpeed(); //spe2d in meter/minute
            //speed = (speed*3600)/1000;      // speed in km/minute
            Toast.makeText(getApplicationContext(), "lat" + latitude + "lon" + longitude,Toast.LENGTH_SHORT).show();
        }
    };
}

void requestUpdate(){    
  locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       @NonNull String permissions[], @ NonNull int[] grantResults) {

    switch (requestCode) {
        case -1:
            //hello
            break;


        case 10: 
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                requestUpdate();
            }else{
            //do something if permission not granted.
            }
            break;

    }
 }
}
like image 80
W4R10CK Avatar answered Oct 30 '22 01:10

W4R10CK


You request the permission before initializing button, and I assume the callback completes before it has the chance to initialize the Button object fully. Try moving the permission check to the end of onCreate, or failing that, double check the button's ID is correct.

like image 45
ditn Avatar answered Oct 30 '22 01:10

ditn