在ListFragment中的适配器上调用notifyDataSetChanged()会导致NullPointerException?

时间:2014-03-24 09:22:43

标签: nullpointerexception adapter android-listfragment notifydatasetchanged fragment-lifecycle

简介:

我有以下设置:

  1. MainActivity方法onNewIntentprocessNewIntentMainActivity也实施FragmentActivity;并有一个标签片段,实现FragmentList

  2. 创建新listaddactivity Parcelable

  3. listControlObject个活动
  4. listaddactivity活动发送一个带有parcel的intent,然后重新构建MainActivity的{​​{1}}和onNewIntent类中的listControlObject。

  5. 然而;我可以将processNewIntent纳入我的intent方法,然后构建新的onNewIntent罚款;但是当我在listControlObject课程上调用adapter.notifyDataSetChanged方法时。

  6. Logcat错误:

    FragmentList

    03-24 04:18:44.149: E/Trace(16964): error opening trace file: No such file or directory (2) 03-24 04:18:49.543: E/AndroidRuntime(16964): FATAL EXCEPTION: main 03-24 04:18:49.543: E/AndroidRuntime(16964): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nanospark.upcdemo/com.nanospark.upcdemo.MainActivity}: java.lang.NullPointerException 03-24 04:18:49.543: E/AndroidRuntime(16964): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184) 03-24 04:18:49.543: E/AndroidRuntime(16964): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211) 03-24 04:18:49.543: E/AndroidRuntime(16964): at android.app.ActivityThread.access$600(ActivityThread.java:149) 03-24 04:18:49.543: E/AndroidRuntime(16964): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300) 03-24 04:18:49.543: E/AndroidRuntime(16964): at android.os.Handler.dispatchMessage(Handler.java:99) 03-24 04:18:49.543: E/AndroidRuntime(16964): at android.os.Looper.loop(Looper.java:153) 03-24 04:18:49.543: E/AndroidRuntime(16964): at android.app.ActivityThread.main(ActivityThread.java:4987) 03-24 04:18:49.543: E/AndroidRuntime(16964): at java.lang.reflect.Method.invokeNative(Native Method) 03-24 04:18:49.543: E/AndroidRuntime(16964): at java.lang.reflect.Method.invoke(Method.java:511) 03-24 04:18:49.543: E/AndroidRuntime(16964): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821) 03-24 04:18:49.543: E/AndroidRuntime(16964): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584) 03-24 04:18:49.543: E/AndroidRuntime(16964): at dalvik.system.NativeStart.main(Native Method) 03-24 04:18:49.543: E/AndroidRuntime(16964): Caused by: java.lang.NullPointerException 03-24 04:18:49.543: E/AndroidRuntime(16964): at com.nanospark.upcdemo.MainActivity.processNewIntent(MainActivity.java:102) 03-24 04:18:49.543: E/AndroidRuntime(16964): at com.nanospark.upcdemo.MainActivity.onCreate(MainActivity.java:81) 03-24 04:18:49.543: E/AndroidRuntime(16964): at android.app.Activity.performCreate(Activity.java:5020) 03-24 04:18:49.543: E/AndroidRuntime(16964): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 03-24 04:18:49.543: E/AndroidRuntime(16964): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) 03-24 04:18:49.543: E/AndroidRuntime(16964): ... 11 more 方法:

    onNewIntent

    @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); processNewIntent(intent); } 方法:

    processNewIntent

    抛出nullpointerexception的特定行:

    private void processNewIntent(Intent intent) {
        cyclefragment = new cycleviewfragment();
        Log.d("TEST", "METHOD CALLED TEST");
        if (intent.hasExtra("custom_object")) {
            if (intent.getParcelableExtra("custom_object").equals(null)) {
                Log.d("Testing null", "Object is null");
            } else {
                Log.d("Testing null", "Object is not null");
    
                 cyclefragment.getA1().add((listControlObject)
                 intent.getParcelableExtra("custom_object"));
                 cyclefragment.adapter.notifyDataSetChanged();
    
            }
        }
    

    cyclefragment.adapter.notifyDataSetChanged(); // - THIS IS LINE 102 类:

    ListFragment

1 个答案:

答案 0 :(得分:1)

试试这个:

if (cyclefragment != null && cyclefragment.adapter != null) {
    cyclefragment.adapter.notifyDataSetChanged();
}