设置标题时,ItemDetail片段中的Android NullPointerException

时间:2018-06-06 13:31:08

标签: java android nullpointerexception

我不知道为什么,但我得到的报告显示我的很多用户在此行中获得了NullPointerExceptionappBarLayout.setTitle("אזעקה (" + mItem.datetime + ")");

我已经尝试了很多东西来捕获这个异常,但是我无法弄清楚它是如何到达这一行并得到NullPointerException的。

首先,我确保appBarLayout不为空。然后我检查了我的mItem.datetime确实包含它应该的日期时间。

这是我的代码:

    mItem = AlertsDetails.items_map.get(getArguments().getString(ARG_ITEM_ID));

    Activity activity = this.getActivity();

    if (mItem == null || mItem.datetime == null)
    {
        activity.finish();
    }

    CollapsingToolbarLayout appBarLayout = (CollapsingToolbarLayout) activity.findViewById(R.id.toolbar_layout);
    if (appBarLayout != null) {
        appBarLayout.setTitle("אזעקה (" + mItem.datetime + ")");
        appBarLayout.setBackgroundResource(R.mipmap.alert_background);
    }

我在这里想念什么?如果appBarLayoutmItem.datetime都不为空,但setTitle方法创建NullPointerException

,那怎么可能?

这些是我得到的错误日志:

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2955)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3030)
  at android.app.ActivityThread.-wrap11 (Unknown Source)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1696)
  at android.os.Handler.dispatchMessage (Handler.java:105)
  at android.os.Looper.loop (Looper.java:164)
  at android.app.ActivityThread.main (ActivityThread.java:6938)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:327)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374)
Caused by: java.lang.NullPointerException: 
  at com.morha.cumtaalerts.fragments.ItemDetailFragment.onCreate (ItemDetailFragment.java:63)
  at android.support.v4.app.Fragment.performCreate (Fragment.java:2328)
  at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1379)
  at android.support.v4.app.FragmentTransition.addToFirstInLastOut (FragmentTransition.java:1188)
  at android.support.v4.app.FragmentTransition.calculateFragments (FragmentTransition.java:1071)
  at android.support.v4.app.FragmentTransition.startTransitions (FragmentTransition.java:115)
  at android.support.v4.app.FragmentManagerImpl.executeOpsTogether (FragmentManager.java:2379)
  at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute (FragmentManager.java:2337)
  at android.support.v4.app.FragmentManagerImpl.execPendingActions (FragmentManager.java:2244)
  at android.support.v4.app.FragmentManagerImpl.dispatchStateChange (FragmentManager.java:3255)
  at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated (FragmentManager.java:3205)
  at android.support.v4.app.FragmentController.dispatchActivityCreated (FragmentController.java:195)
  at android.support.v4.app.FragmentActivity.onStart (FragmentActivity.java:597)
  at android.support.v7.app.AppCompatActivity.onStart (AppCompatActivity.java:177)
  at android.app.Instrumentation.callActivityOnStart (Instrumentation.java:1340)
  at android.app.Activity.performStart (Activity.java:7191)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2918)

谢谢!

1 个答案:

答案 0 :(得分:1)

完成return;后添加Activity

if (mItem == null || mItem.datetime == null)
{
    activity.finish();
    return;
}