空指针通过意图将对象传递给新活动时

时间:2013-11-21 02:57:42

标签: java android android-intent ixmlserializable

所以我在这个网站(以及其他网站)上已经阅读了很多问题,而且我找不到适合我的代码的解决方案,所以我认为下一步就是在这里发布。我正在制作一个迷宫游戏。我有一个生成活动和一个播放活动,我试图将生成的Maze对象传递给PlayActivity。迷宫是可序列化的,但是当我尝试从PlayActivity的意图中获取迷宫时,它返回一个空指针异常。任何想法都会很精彩。

Maze:
public class Maze implements Serializable {

private static final long serialVersionUID = 1L;

然后是GeneratingActivity:

Intent playIntent = new Intent(this , PlayActivity.class);
playIntent.putExtra("MadeMaze" , maze);
startActivity(playIntent);

最后是PlayActivity:

maze = (Maze) getIntent().getSerializableExtra("MadeMaze");
System.out.print(maze.mazecells.toString());
System.out.print("here");

我知道有人可能会建议使用Parcelable,但是我的Maze类有很多数据字段,看起来并不值得(除非有人有充分的理由)。

编辑:这是日志:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity        ComponentInfo{edu.wm.cs.cs301.jesavino/edu.wm.cs.cs301.jesavino.UI.PlayActivity}: java.lang.NullPointerException

11-20 21:46:37.614: E/AndroidRuntime(1940):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-20 21:46:37.614: E/AndroidRuntime(1940):     at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-20 21:46:37.614: E/AndroidRuntime(1940):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-20 21:46:37.614: E/AndroidRuntime(1940):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-20 21:46:37.614: E/AndroidRuntime(1940):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 21:46:37.614: E/AndroidRuntime(1940):     at android.os.Looper.loop(Looper.java:137)
11-20 21:46:37.614: E/AndroidRuntime(1940):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-20 21:46:37.614: E/AndroidRuntime(1940):     at java.lang.reflect.Method.invokeNative(Native Method)
11-20 21:46:37.614: E/AndroidRuntime(1940):     at java.lang.reflect.Method.invoke(Method.java:525)
11-20 21:46:37.614: E/AndroidRuntime(1940):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-20 21:46:37.614: E/AndroidRuntime(1940):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-20 21:46:37.614: E/AndroidRuntime(1940):     at dalvik.system.NativeStart.main(Native Method)
11-20 21:46:37.614: E/AndroidRuntime(1940): Caused by: java.lang.NullPointerException
11-20 21:46:37.614: E/AndroidRuntime(1940):     at edu.wm.cs.cs301.jesavino.UI.PlayActivity.onCreate(PlayActivity.java:36)
11-20 21:46:37.614: E/AndroidRuntime(1940):     at android.app.Activity.performCreate(Activity.java:5133)
11-20 21:46:37.614: E/AndroidRuntime(1940):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-20 21:46:37.614: E/AndroidRuntime(1940):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-20 21:46:37.614: E/AndroidRuntime(1940):     ... 11 more

3 个答案:

答案 0 :(得分:0)

System.out.print(maze.toString())不应为null,请检查

答案 1 :(得分:0)

请尝试使用Bundle

Intent playIntent = new Intent(this , PlayActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("MadeMaze", maze);
playIntent.putExtras(bundle);
startActivity(playIntent);

和PlayActivity

 maze = (Maze) getIntent().getExtra().getSerializable("MadeMaze");

答案 2 :(得分:0)

尝试在Maze课程中实现Parcelable。

Android不支持Serializable,当您尝试在android上下文之间发送数据时,应首先使用Parcelable。