gpt4 book ai didi

android - 为什么可打包对象在接收 Activity 中为空?

转载 作者:太空宇宙 更新时间:2023-11-03 12:13:20 24 4
gpt4 key购买 nike

我遵循 Android 开发人员指南中的 Parcelable 示例,目的是将一个更复杂的对象发送到不同的 Activity 。现在,我的类是 String 成员的包装器。如果我将对象放入 Intent 中并从 Intent 中读回它,它会按预期读取,但是,在接收方,它始终是空指针。

我的类(class):

package com.mobibob.android.myapp;

import android.os.Parcel;
import android.os.Parcelable;


public class ContentItem implements Parcelable {
public String name = "name";
public static final String EXTRA_CONTENT_DETAIL = "contentDetail";

ContentItem(String n) {
name = n;
}

ContentItem(Parcel in) {
in.readParcelable(ContentItem.class.getClassLoader()); <--- NEW CODE
name = in.readString();
}

@Override
public String toString() {
return name.toString();
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
}

public final Parcelable.Creator<ContentItem> CREATOR = new Parcelable.Creator<ContentItem>() {
public ContentItem createFromParcel(Parcel in) {
return new ContentItem(in);
}

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

}

我的 Intent/回读/startActivity 设置:

    ContentItem ci = new ContentItem("mobibob");
// Launch ...
Intent i = new Intent(getApplicationContext(), ContentDetailActivity.class);
i.putExtra(ContentItem.EXTRA_CONTENT_DETAIL, ci);
ContentItem readbackCi = i.getParcelableExtra(ContentItem.EXTRA_CONTENT_DETAIL);
Log.d(TAG, "\n\n\t" + readbackCi.name);
startActivity(i);

我的接收 Activity :

package com.mobibob.android.myapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class ContentDetailActivity extends Activity {

public static final String TAG = "ContentDetailActivity";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_detail);

try {
Intent i = getIntent();
Log.d(TAG, "intent: " + i.toString());
Log.d(TAG, "extras: " + i.getExtras());

ContentItem ci = (ContentItem) i.getParcelableExtra(ContentItem.EXTRA_CONTENT_DETAIL);

Log.d(TAG, "content-item: " + ci.toString());

((TextView) findViewById(R.id.name_of_content)).setText(ci.name);

} catch (Exception e) {
Log.d(TAG, e.toString());
}
}

}

我的日志:

08-16 15:03:40.121 I/HomeActivity( 9840): onItemClick - AdapterView (pos=1 id=1)
08-16 15:03:40.121 D/HomeActivity( 9840):
08-16 15:03:40.121 D/HomeActivity( 9840):
08-16 15:03:40.121 D/HomeActivity( 9840): Rock/Pop
08-16 15:03:40.131 I/ActivityManager( 1186): Starting activity: Intent { cmp=com.mobibob.android.myapp/.ContentDetailActivity (has extras) }
08-16 15:03:40.341 D/ContentDetailActivity( 9840): intent: Intent { cmp=com.mobibob.android.myapp/.ContentDetailActivity (has extras) }
08-16 15:03:40.341 D/ContentDetailActivity( 9840): extras: Bundle[mParcelledData.dataSize=148]
08-16 15:03:40.351 D/ContentDetailActivity( 9840): java.lang.NullPointerException
08-16 15:03:40.653 I/ActivityManager( 1186): Displayed activity com.mobibob.android.myapp/.ContentDetailActivity: 336 ms (total 336 ms)

我的堆栈回溯:

08-16 17:19:52.170 I/HomeActivity( 3147): onItemClick - AdapterView (pos=3 id=3)
08-16 17:19:52.170 D/HomeActivity( 3147):
08-16 17:19:52.170 D/HomeActivity( 3147):
08-16 17:19:52.170 D/HomeActivity( 3147): mobibob
08-16 17:19:52.170 I/ActivityManager( 1202): Starting activity: Intent { cmp=com.mobibob.android.myapp/.ContentDetailActivity (has extras) }
08-16 17:19:52.410 D/ContentDetailActivity( 3147): intent: Intent { cmp=com.mobibob.android.myapp/.ContentDetailActivity (has extras) }
08-16 17:19:52.410 D/ContentDetailActivity( 3147): extras: Bundle[mParcelledData.dataSize=144]
08-16 17:19:52.410 D/ContentDetailActivity( 3147): java.lang.NullPointerException
08-16 17:19:52.410 W/System.err( 3147): java.lang.NullPointerException
08-16 17:19:52.420 W/System.err( 3147): at java.lang.reflect.Field.getField(Native Method)
08-16 17:19:52.420 W/System.err( 3147): at java.lang.reflect.Field.get(Field.java:247)
08-16 17:19:52.420 W/System.err( 3147): at android.os.Parcel.readParcelable(Parcel.java:1811)
08-16 17:19:52.420 W/System.err( 3147): at android.os.Parcel.readValue(Parcel.java:1713)
08-16 17:19:52.420 W/System.err( 3147): at android.os.Parcel.readMapInternal(Parcel.java:1947)
08-16 17:19:52.420 W/System.err( 3147): at android.os.Bundle.unparcel(Bundle.java:169)
08-16 17:19:52.420 W/System.err( 3147): at android.os.Bundle.getParcelable(Bundle.java:1037)
08-16 17:19:52.430 W/System.err( 3147): at android.content.Intent.getParcelableExtra(Intent.java:3276)
08-16 17:19:52.430 W/System.err( 3147): at com.mobibob.android.myapp.ContentDetailActivity.onCreate(ContentDetailActivity.java:24)
08-16 17:19:52.430 W/System.err( 3147): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-16 17:19:52.430 W/System.err( 3147): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
08-16 17:19:52.430 W/System.err( 3147): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
08-16 17:19:52.430 W/System.err( 3147): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
08-16 17:19:52.430 W/System.err( 3147): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
08-16 17:19:52.430 W/System.err( 3147): at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 17:19:52.430 W/System.err( 3147): at android.os.Looper.loop(Looper.java:123)
08-16 17:19:52.430 W/System.err( 3147): at android.app.ActivityThread.main(ActivityThread.java:4363)
08-16 17:19:52.430 W/System.err( 3147): at java.lang.reflect.Method.invokeNative(Native Method)
08-16 17:19:52.430 W/System.err( 3147): at java.lang.reflect.Method.invoke(Method.java:521)
08-16 17:19:52.430 W/System.err( 3147): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-16 17:19:52.430 W/System.err( 3147): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-16 17:19:52.440 W/System.err( 3147): at dalvik.system.NativeStart.main(Native Method)
08-16 17:19:52.895 I/ActivityManager( 1202): Displayed activity com.mobibob.android.myapp/.ContentDetailActivity: 582 ms (total 582 ms)

最佳答案

我猜 in.readParcelable(ContentItem.class.getClassLoader()); 已经返回了您重新创建的 ContentItem。因此,您应该将该行放入 Parcelable.Creator 的 createFromParcel-Method 中。不过只是猜测。

关于android - 为什么可打包对象在接收 Activity 中为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3496903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com