本文介绍了为什么在接收活动parcelable对象空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下了Android开发人员指南使用具有更复杂的对象,我发送到不同的活动,打算在Parcelable例子。现在,我的班是一个String成员的包装。如果我把对象的意图和阅读从中读取预期,但是,在接收方的意图回来,它始终是空指针。

我的类:

 包com.mobibob.android.myapp;进口android.os.Parcel;
进口android.os.Parcelable;
公共类ContentItem实现Parcelable {
    公共字符串名称=名;
    公共静态最后弦乐EXTRA_CONTENT_DETAIL =contentDetail;    ContentItem(字符串n){
        名称= N;
    }    ContentItem(包裹中){
            in.readParcelable(ContentItem.class.getClassLoader()); < ---新的code
        名称= in.readString();
    }    @覆盖
    公共字符串的toString(){
        返回name.toString();
    }    @覆盖
    公众诠释describeContents(){
        返回0;
    }    @覆盖
    公共无效writeToParcel(DEST包裹,INT标志){
        dest.writeString(名);
    }    公众最终Parcelable.Creator< ContentItem> CREATOR =新Parcelable.Creator< ContentItem>(){
        公共ContentItem createFromParcel(包裹中){
        返回新ContentItem(中);
        }        公共ContentItem [] newArray(INT大小){
        返回新ContentItem【尺寸】;
        }
    };}

我的意图/回读/ startActivity的设置:

  ContentItem CI =新ContentItem(mobibob);
//启动...
意图I =新意图(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(ⅰ);

我的接收活动:

 包com.mobibob.android.myapp;进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.util.Log;
进口android.widget.TextView;公共类ContentDetailActivity延伸活动{    公共静态最后的字符串标记=ContentDetailActivity;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
        的setContentView(R.layout.content_detail);        尝试{
            意向I = getIntent();
            Log.d(TAG的意图:+ i.toString());
            Log.d(TAG,演员:+ i.getExtras());            ContentItem CI =(ContentItem)i.getParcelableExtra(ContentItem.EXTRA_CONTENT_DETAIL);            Log.d(TAG,内容项:+ ci.toString());            ((的TextView)findViewById(R.id.name_of_content))的setText(ci.name)。        }赶上(例外五){
            Log.d(TAG,e.toString());
    }
    }}

我的日志:

  15 08-16:03:40.121 I / HomeActivity(9840):onItemClick  - 适配器视图(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):摇滚/流行
08-16 15:03:40.131 I / ActivityManager(1186):启动活动:意向{CMP = com.mobibob.android.myapp / .ContentDetailActivity(有临时演员)}
08-16 15:03:40.341 D / ContentDetailActivity(9840):意向:意向{CMP = com.mobibob.android.myapp / .ContentDetailActivity(有临时演员)}
08-16 15:03:40.341 D / ContentDetailActivity(9840):演员:捆绑[mParcelledData.dataSize = 148]
08-16 15:03:40.351 D / ContentDetailActivity(9840):显示java.lang.NullPointerException
08-16 15:03:40.653 I / ActivityManager(1186):显示活动com.mobibob.android.myapp / .ContentDetailActivity:336毫秒(总336毫秒)

我的堆栈跟踪:

  08-16 17:19:52.170 I / HomeActivity(3147):onItemClick  - 适配器视图(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):启动活动:意向{CMP = com.mobibob.android.myapp / .ContentDetailActivity(有临时演员)}
08-16 17:19:52.410 D / ContentDetailActivity(3147):意向:意向{CMP = com.mobibob.android.myapp / .ContentDetailActivity(有临时演员)}
08-16 17:19:52.410 D / ContentDetailActivity(3147):演员:捆绑[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):在java.lang.reflect.Field.getField(本机方法)
08-16 17:19:52.420 W / System.err的(3147):在java.lang.reflect.Field.get(Field.java:247)
08-16 17:19:52.420 W / System.err的(3147):在android.os.Parcel.readParcelable(Parcel.java:1811)
08-16 17:19:52.420 W / System.err的(3147):在android.os.Parcel.readValue(Parcel.java:1713)
08-16 17:19:52.420 W / System.err的(3147):在android.os.Parcel.readMapInternal(Parcel.java:1947)
08-16 17:19:52.420 W / System.err的(3147):在android.os.Bundle.unparcel(Bundle.java:169)
08-16 17:19:52.420 W / System.err的(3147):在android.os.Bundle.getParcelable(Bundle.java:1037)
08-16 17:19:52.430 W / System.err的(3147):在android.content.Intent.getParcelableExtra(Intent.java:3276)
08-16 17:19:52.430 W / System.err的(3147):在com.mobibob.android.myapp.ContentDetailActivity.onCreate(ContentDetailActivity.java:24)
08-16 17:19:52.430 W / System.err的(3147):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-16 17:19:52.430 W / System.err的(3147):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
08-16 17:19:52.430 W / System.err的(3147):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
08-16 17:19:52.430 W / System.err的(3147):在android.app.ActivityThread.access $ 2200(ActivityThread.java:119)
08-16 17:19:52.430 W / System.err的(3147):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1863)
08-16 17:19:52.430 W / System.err的(3147):在android.os.Handler.dispatchMessage(Handler.java:99)
08-16 17:19:52.430 W / System.err的(3147):在android.os.Looper.loop(Looper.java:123)
08-16 17:19:52.430 W / System.err的(3147):在android.app.ActivityThread.main(ActivityThread.java:4363)
08-16 17:19:52.430 W / System.err的(3147):在java.lang.reflect.Method.invokeNative(本机方法)
08-16 17:19:52.430 W / System.err的(3147):在java.lang.reflect.Method.invoke(Method.java:521)
08-16 17:19:52.430 W / System.err的(3147):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
08-16 17:19:52.430 W / System.err的(3147):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-16 17:19:52.440 W / System.err的(3147):在dalvik.system.NativeStart.main(本机方法)
08-16 17:19:52.895 I / ActivityManager(1202):显示活动com.mobibob.android.myapp / .ContentDetailActivity:582毫秒(总582毫秒)


解决方案

我猜 in.readParcelable(ContentItem.class.getClassLoader()); 已返回您重新ContentItem。所以,你应该把这些线成Parcelable.Creator的createFromParcel法。
只是一个猜测,但。

I am following the Parcelable example in the Android developer guide with the intention of having a more complex object that I send to different activities. Right now, my class is a wrapper of a String member. If I put the object in the intent and read it back from the Intent it reads as expected, however, on the receiver side, it is always null-pointer.

My 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];
        }
    };

}

My setup of intent / read-back / 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);

My receiving 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());
    }
    }

}

My log:

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)

My stack backtrace:

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)
解决方案

I'd guess that in.readParcelable(ContentItem.class.getClassLoader()); returns already your recreated ContentItem. So you should put that line into the createFromParcel-Method in Parcelable.Creator. Just a guess though.

这篇关于为什么在接收活动parcelable对象空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 23:18