通过 Parcelable 发送嵌套 ArrayList 时出现 NullPointerException

Posted

技术标签:

【中文标题】通过 Parcelable 发送嵌套 ArrayList 时出现 NullPointerException【英文标题】:NullPointerException when sending nested ArrayList via Parcelable 【发布时间】:2021-09-30 02:36:05 【问题描述】:

我正在尝试通过 Parcelable 意图发送嵌套的 ArrayList 对象。自定义对象的名称是“ExampleItem”,其中包含嵌套的自定义对象“B”。我从 MainActivity 发送到 Activity2。我收到 NullPointerException 错误 “尝试在空对象引用上调用接口方法 'boolean java.util.List.add(java.lang.Object)'”。如何通过 Parcelable 成功发送?

主要活动

public class MainActivity extends AppCompatActivity 

    private ArrayList<ExampleItem> mExampleList;
    private ArrayList<B> mBList;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    

    public void Press(View view) 

        B b = new B("hello");
        mBList = new ArrayList<>();
        mBList.add(b);

        mExampleList = new ArrayList<>();
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_background, "Line 1", "Line 2", mBList));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_foreground, "Line 3", "Line 4", mBList));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_foreground, "Line 5", "Line 6", mBList));

        Intent intent = new Intent(MainActivity.this, Activity2.class);
        intent.putParcelableArrayListExtra("Example Item", mExampleList);
        startActivity(intent);

    

活动2

public class Activity2 extends AppCompatActivity 
    
        @Override
        protected void onCreate(Bundle savedInstanceState) 
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity2);
    
            Intent intent = getIntent();
            ArrayList<ExampleItem> exampleItem = intent.getParcelableArrayListExtra("Example Item");
    
        
    

示例项目

public class ExampleItem implements Parcelable 
    private int mImageResource;
    private String mText1;
    private String mText2;
    private ArrayList<B> mList;


    public ExampleItem(int imageResource, String text1, String text2, ArrayList<B> list) 
        mImageResource = imageResource;
        mText1 = text1;
        mText2 = text2;
        mList = list;
    

    protected ExampleItem(Parcel in) 
        mImageResource = in.readInt();
        mText1 = in.readString();
        mText2 = in.readString();
        in.readList(mList, this.getClass().getClassLoader());
    

    public static final Creator<ExampleItem> CREATOR = new Creator<ExampleItem>() 
        @Override
        public ExampleItem createFromParcel(Parcel in) 
            return new ExampleItem(in);
        

        @Override
        public ExampleItem[] newArray(int size) 
            return new ExampleItem[size];
        
    ;

    public int getImageResource() 
        return mImageResource;
    

    public String getText1() 
        return mText1;
    

    public String getText2() 
        return mText2;
    

    public ArrayList<B> getmList() 
        return mList;
    

    @Override
    public int describeContents() 
        return 0;
    

    @Override
    public void writeToParcel(Parcel dest, int flags) 
        dest.writeInt(mImageResource);
        dest.writeString(mText1);
        dest.writeString(mText2);
        dest.writeList(mList);
    


B

public class B implements Parcelable 

    public String str;

    @Override
    public int describeContents() 
        // TODO Auto-generated method stub
        return 0;
    


    public B(String str) 
        this.str = str;
    

    private B(Parcel in) 
        str = in.readString();
    

    public static final Parcelable.Creator<B> CREATOR
            = new Parcelable.Creator<B>() 
        public B createFromParcel(Parcel in) 
            return new B(in);
        

        public B[] newArray(int size) 
            return new B[size];
        
    ;

    @Override
    public void writeToParcel(Parcel dest, int flags) 
        dest.writeString(str);
    


输出日志

E/androidRuntime: FATAL EXCEPTION: main
    Process: com.codinginflow.exampleparcelable, PID: 9376
    java.lang.RuntimeException: Unable to start activity ComponentInfocom.codinginflow.exampleparcelable/com.codinginflow.exampleparcelable.Activity2: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
        at android.os.Parcel.readListInternal(Parcel.java:3632)
        at android.os.Parcel.readList(Parcel.java:2559)
        at com.codinginflow.exampleparcelable.ExampleItem.<init>(ExampleItem.java:26)
        at com.codinginflow.exampleparcelable.ExampleItem$1.createFromParcel(ExampleItem.java:32)
        at com.codinginflow.exampleparcelable.ExampleItem$1.createFromParcel(ExampleItem.java:29)
        at android.os.Parcel.readParcelable(Parcel.java:3281)
        at android.os.Parcel.readValue(Parcel.java:3174)
        at android.os.Parcel.readListInternal(Parcel.java:3630)
        at android.os.Parcel.readArrayList(Parcel.java:2758)
        at android.os.Parcel.readValue(Parcel.java:3195)
        at android.os.Parcel.readArrayMapInternal(Parcel.java:3567)
        at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:292)
        at android.os.BaseBundle.unparcel(BaseBundle.java:236)
        at android.os.Bundle.getParcelable(Bundle.java:1001)
        at android.content.Intent.getParcelableExtra(Intent.java:8276)
        at com.codinginflow.exampleparcelable.Activity2.onCreate(Activity2.java:23)
        at android.app.Activity.performCreate(Activity.java:8000)
        at android.app.Activity.performCreate(Activity.java:7984)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)

【问题讨论】:

【参考方案1】:

首先,从示例项目中删除空格你应该像这样传递数据

添加数据

ArrayList<ExampleItem> mExampleList = new ArrayList<>();
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_background, "Line 1", "Line 2", mBList));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_foreground, "Line 3", "Line 4", mBList));
        mExampleList.add(new ExampleItem(R.drawable.ic_launcher_foreground, "Line 5", "Line 6", mBList));

发送数据

首先,创建一个捆绑包并将您的项目放在那里,然后将捆绑包传递给您的意图

Intent intent = new Intent(FromActivity.this, ToActivity.class); 
Bundle bundle;
bundle.putParcelableArrayList("example_item", items); 
intent.putExtras(bundle);

获取数据

 ArrayList<ExampleItem> items= intent.getParcelableArrayListExtra("example_item");

编辑

在我看来,您应该使用 JsonParsing,然后使用 StringExtra 发送您的数据。

您应该使用 parcelable

来实现您的 pojo 项目

看看这个例子how to pass Array list <Object> to another activity

【讨论】:

我仍然收到以下错误消息:“尝试在空对象引用上调用虚拟方法 'void android.os.Bundle.putParcelableArrayList(java.lang.String, java.util.ArrayList)' "

以上是关于通过 Parcelable 发送嵌套 ArrayList 时出现 NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章

意图getextra中嵌套Parcelable的空指针异常

如何通过parcelable发送包含列表的列表对象

如何通过 Parcelable 接口将 WifiP2pManager 对象发送到另一个活动

不将数据发送到下一个活动,而是发送到之后的活动(使用 parcelable)

Android 使用 setonclickfillinintent 发送 Parcelable

将对象从服务发送到活动(无法编组非 Parcelable)