java.lang.RuntimeException: Fehler liefert Ergebnis ResultInfo{who=@android:requestPermissions:, request=10, Ergebnis=-1, data=Vorsatz

BEARBEITEN:

Nun die app funktioniert. Aber es fragt nur für die Berechtigung, nach dem ersten Start(nach der installation). Wenn ich die app verlassen(drücken der zurück-Taste) und dann später neu starten, es doesn ' T bitten um Erlaubnis. Warum ist das passiert?

Nicht durch drücken der zurück-Taste zerstört die Aktivität durch Aufruf von onDestroy() und sollte es nicht Aufruf onCreate () - Methode wieder?

URSPRÜNGLICHE FRAGE

Meine app ermöglicht es dem Benutzer zu finden, der Ihre aktuelle Position auf Knopfdruck.Wenn die app zum ersten mal gestartet, fordert es den Benutzer die Berechtigung, aber es stürzt ab, wenn die Erlaubnis erteilt wird, geben Sie folgenden Fehler:

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

Unten ist meine Tätigkeit 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();
                }
            }

        }
    }



}

Unten ist die logcat Fehler:

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)
  • Das ist, weil es nicht findest R. id.button in dein layout
  • Ich fixierte Sie den code, siehe meine Antwort. @rayan
InformationsquelleAutor rayan | 2017-01-08
Schreibe einen Kommentar