gpt4 book ai didi

android - java.lang.RuntimeException : Failure delivering result ResultInfo{who=@android:requestPermissions:, 请求=10,结果=-1,数据= Intent

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:56:33 26 4
gpt4 key购买 nike

编辑:

现在该应用程序正在运行。但它只在第一次启动后(安装后)请求许可。如果我离开应用程序(按后退按钮)然后再次启动它,它不会请求许可。为什么会这样?

按下后退按钮不会通过调用 onDestroy() 来销毁 Activity,它不应该再次调用 onCreate() 方法吗?

原始问题

我的应用程序允许用户在按下按钮时找到他们当前的位置。每当应用程序第一次启动时,它都会请求用户许可,但在授予许可权时它会崩溃并出现以下错误:

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

下面是我的 Activity 代码:

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();
}
}

}
}



}

下面是 logcat 错误:

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)

最佳答案

编辑粗略权限,按钮初始化。 onCreate() 和 break ponit 添加了 onRequestPermissionsResult()

如错误所述,NullPointException 是由 Button 的 null 对象引起的:

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;

}
}
}

关于android - java.lang.RuntimeException : Failure delivering result ResultInfo{who=@android:requestPermissions:, 请求=10,结果=-1,数据= Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41537388/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com