尝试将对象传递给活动时得到nullpointer
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了尝试将对象传递给活动时得到nullpointer相关的知识,希望对你有一定的参考价值。
我正在为一个项目制作一个简单的android应用程序。我有一个显示会话列表视图的活动,当我点击其中一个列出的会话时,我想在其中显示消息。为此,我使用了一个Parcelable对象。
import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.widget.Toast;
import java.util.concurrent.ConcurrentHashMap;
public class ParcelableChat implements Parcelable {
private Chat c;
public Chat getChat (){
return this.c;
}
public ParcelableChat(Chat c){
this.c = c;
}
public ParcelableChat(Parcel in){
c = new Chat();
c.setObject(in.readString());
c.setMessageManager((MessageManager)in.readParcelable(MessageManager.class.getClassLoader()));
}
public static final Creator<ParcelableChat> CREATOR =
new ClassLoaderCreator<ParcelableChat>() {
@Override
public ParcelableChat createFromParcel(Parcel parcel) {
return new ParcelableChat(parcel);
}
@Override
public ParcelableChat[] newArray(int i) {
return new ParcelableChat[i];
}
@Override
public ParcelableChat createFromParcel(Parcel parcel, ClassLoader classLoader) {
return new ParcelableChat(parcel);
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
}
}
在mainActivity我做:
intent intent = new Intent(this,MessageActivity.class);
intent.putExtra("chat", new ParcelableChat(item));//Where item is the Chat object from the ListView.
startActivity(intent);
在新的Activity(MessageActivity)中,我做了:
Bundle extras = getIntent().getExtras();
ParcelableChat parcelableChat= (ParcelableChat)extras.getParcelable("chat");
Chat chat = parcelableChat.getChat();
当我尝试使用以下方式显示聊天的最后一条消息:
Toast.makeText(getApplicationContext(),"Value : "+ chat.getLastMessage(),Toast.LENGTH_SHORT ).show();
logcat的:
Process: project.ingsw.maboo, PID: 31172
java.lang.RuntimeException: Unable to start activity ComponentInfo{project.ingsw.maboo/project.ingsw.maboo.MessageActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at project.ingsw.maboo.Chat.getMessage(Chat.java:36)
at project.ingsw.maboo.MessageActivity.onCreate(MessageActivity.java:37)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)
我得到NullPointerException ....有什么问题?
答案
只需修改您的新活动,如下所示:
ParcelableChat parcelableChat= (ParcelableChat)getIntent().getParcelableExtra("chat");
Chat chat = parcelableChat.getChat();
问题是在启动活动时你附加了parcelable到intent但是在让你正在读取额外内容时。所以你在那里变得空虚。
以上是关于尝试将对象传递给活动时得到nullpointer的主要内容,如果未能解决你的问题,请参考以下文章