如何修复“尝试在空对象引用上调用虚拟方法“ android.support.v4.app.p android.support.v4.app.k.a()””

时间:2019-01-30 06:30:32

标签: android

尝试获取片段管理器时,我从多个用户处崩溃。知道为什么会崩溃吗?

@Override
 public void onNoInternetError() {
   Log.i("base", "no internet error!");
   final FragmentTransaction ft = getFragmentManager().beginTransaction();
   final NoInternetFragment fragment = new NoInternetFragment();
   ft.replace(R.id.content_frame, fragment, "noInternetFragment");
   ft.addToBackStack(null);
   ft.commit();

错误

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.p android.support.v4.app.k.a()' on a null object reference
       at com.abc.abc.fragments.ABCFragmentBase$1.onNoInternetError(ABCFragmentBase.java:112)
       at com.abc.abc.ui.components.NoInternetListener.showNoInternetFragment(NoInternetListener.java:14)
       at com.abc.abc.ServerManager.handleFailure(ServerManager.java:1875)
       at com.abc.abc.ServerManager.access$100(ServerManager.java:54)
       at com.abc.abc.ServerManager$33.onFailure(ServerManager.java:1220)
       at com.abc.sdk.JsonObjectRequest.deliverError(JsonObjectRequest.java:70)
       at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:108)
       at android.os.Handler.handleCallback(Handler.java:789)
       at android.os.Handler.dispatchMessage(Handler.java:98)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6944)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

1 个答案:

答案 0 :(得分:1)

从您的代码看来,File.ReadAllBytes(fileToOpen);调用返回的是null,这可能意味着您的片段尚未通过getFragmentManager()

您可以添加连接错误标记,然后在onAttach()显示相关信息之后