java - 使用 Parcelable 发送 StringArray 时出现 NullPointerException

标签 java android android-intent parcelable

我在发送带有 parcelable 对象的 Intent 时遇到了奇怪的问题。这个对象有一个字符串数组。使用此答案 How to send an object from one Android Activity to another using Intents?我首先将我的对象创建为可打包的,然后发送它。这是我的代码 fragment :

    public class MyObject implements Parcelable{

    private String _title = null;
    private String[] _genre;
    private int _time = 0;
    private String _disc = null;

    private static final String TAG = "MyObject";

    public MyObject (String title, String[] genre, int time, String disc) {
        _title = title;
        _genre = genre;
        _time = time;
        _disc = disc;
    }



    public MyObject(Parcel source) {
            /*
             * Reconstruct from the Parcel
             */
            Log.v(TAG, "ParcelData(Parcel source): time to put back parcel data");
            _title = source.readString();
            try{
                source.readStringArray(_genre);
            }catch(Exception e){
                Log.e(TAG,e.toString());
            }
        _time = source.readInt();
        _disc = source.readString();
    }

    /*... Getters and setters and other stuff...*/

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        Log.v(TAG, "writeToParcel..."+ flags);
        dest.writeString(_title);
        dest.writeStringArray(_genre);
        dest.writeInt(_time);
        dest.writeString(_disc);
        Log.i(TAG,_genre[0]);
    }

    // this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
    public static final Parcelable.Creator<MyObject> CREATOR = new Parcelable.Creator<MyObject>() {
        public MyObject createFromParcel(Parcel in) {
            return new MyObject(in);
        }

        public MyObject[] newArray(int size) {
            return new MyObject[size];
        }
    };
}

发送似乎没问题。

Intent displayInfo = new Intent(getApplicationContext(), MovieInfo.class);
                    displayInfo.putExtra(OBJECT, mySQLiteAdapter.getEntry(idSet));
                    startActivity(displayInfo);

但是,当我试图获取这些数据时

Intent intent = getIntent();
        myParcel = (MyObject) intent.getParcelableExtra(FBM2Activity.OBJECT);

我从 readStringArray 获取 NullPointerException。有谁知道我在哪里可能出错?

最佳答案

使用 _genre = source.createStringArray()

这背后的原因是 readStringArray(String[] val) 期望 val 是一个已经创建的 String[]具有正确数量的数组元素。当您调用 source.readStringArray(_genre) 时,_genrenull

关于java - 使用 Parcelable 发送 StringArray 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11492942/

相关文章:

Java:如何在递归调用中使用原始参数递归调用我的方法

android - 显示简单的消息对话框

java - 无法将 OnLongClickListener 设置为 ExpandableListView 组

java - getStringExtra() 为空时的默认值

java - 如何在 ListView 到达末尾时向其添加项目

java - ActionListener 和 for 循环

Java多次匹配同一个组

android - 如何在真实设备上运行 android 应用程序?

java - Android 从 URI 读取文本文件

java - Android 应用程序中的 Google Plus 集成