我发生了一个非常不寻常的错误,从未经历过这样的事情。
我在我的应用程序中使用片段。在Android M及以上我使用的应用程序,当我最小化应用程序并授予权限并再次恢复应用程序时,应用程序崩溃,因为我试图在主要活动中使用对象,但我得到null。但是,当我这样做并删除许可时,我不会崩溃。
只有在应用程序最小化时尝试授予权限时才会发生这种情况。如果我关闭应用程序并授予权限并再次启动应用程序,那么我不会遇到任何此类问题。
我收到错误的行:
Screen_title.setText("Survey No. " + mainActivity.currentTask.farmerData.farms.survey_number);
其中“mainActivity.currentTask”为空。
Logcat:
04-13 10:44:26.226 16742-16742/com.app.farmtrace.fieldagent E/AndroidRuntime: FATAL EXCEPTION: main Process: com.app.farmtrace.fieldagent, PID: 16742 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.farmtrace.fieldagent/com.app.farmtrace.fieldagent.Activity.MainActivity}: java.lang.NullPointerException: Attempt to read from field 'com.app.farmtrace.fieldagent.Data.Farmer_Data.FarmerData com.app.farmtrace.fieldagent.Data.Task.farmerData' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3256)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3352)
at android.app.ActivityThread.access$1100(ActivityThread.java:223)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to read from field 'com.app.farmtrace.fieldagent.Data.Farmer_Data.FarmerData com.app.farmtrace.fieldagent.Data.Task.farmerData' on a null object reference
at com.app.farmtrace.fieldagent.Fragment.Farmer_Details.bindView(Farmer_Details.java:88)
at com.app.farmtrace.fieldagent.Fragment.Farmer_Details.onViewCreated(Farmer_Details.java:64)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1315)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1523)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1585)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2832)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:603)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:181)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1266)
at android.app.Activity.performStart(Activity.java:6916)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3219)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3352)
at android.app.ActivityThread.access$1100(ActivityThread.java:223)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答案 0 :(得分:0)
答案 1 :(得分:0)
private Context mContext;
private HomeTab mActivity;
mActivity = ((HomeTab) getActivity());
mContext = ((HomeTab) getContext());
检查片段中的权限,如下所示,传递您的活动和上下文
if (ActivityCompat.checkSelfPermission(getContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(getContext(),
android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(),
new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION,
android.Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_LOCATION);
} else {
Log.e("DB", "PERMISSION GRANTED");
}