使用Bundle传递数据时出现空指针异常 [英] Null pointer exception when using Bundle to pass data

查看:107
本文介绍了使用Bundle传递数据时出现空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个浮点数组 camObjCoord 声明为..

I've got a float array camObjCoord declared as..

public static float camObjCoord[] = new float[8000];

然后我在一个类中填充它的索引,该类执行如下操作..

I'm then filling it's indexes in a class that does something like the following..

camObjCoord[1] = 2.5;

然后我调用 makeview()

       public void makeview() {
    Intent myIntent = new Intent(this, GLCamTest.class);
    this.startActivity(myIntent);
    Bundle b = new Bundle();
    b.putFloatArray("tweets", camObjCoord);
}

然后在新班级中它正在做...

and then in the new class it's doing...

               public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle b = this.getIntent().getExtras();
    float original[] = b.getFloatArray("tweets");
    camObjCoord = original;
    counter++;
}   

但是...我在 float original[] = b.getFloatArray("tweets"); 处得到一个空指针异常,我不知道为什么.我在调用意图等之前尝试过捆绑,但我没有解决问题.有人知道为什么吗?

But... I'm getting a Null pointer Exception at float original[] = b.getFloatArray("tweets"); and I don't know why. I've tried bundling before calling the intent etc. but I've had no luck at a fix. Anyone know why?

我还包含了一些错误,以防大家有兴趣.

I've also included some of the error incase any of you are interested.

            07-14 11:14:35.592: ERROR/AndroidRuntime(7886): Caused by:  java.lang.NullPointerException
            07-14 11:14:35.592: ERROR/AndroidRuntime(7886):     at  org.digital.com.GLCamTest.onCreate(GLCamTest.java:41)
            07-14 11:14:35.592: ERROR/AndroidRuntime(7886):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
            07-14 11:14:35.592: ERROR/AndroidRuntime(7886):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
            07-14 11:14:35.592: ERROR/AndroidRuntime(7886):     ... 11 more

谢谢!

推荐答案

好的,所以这表明 this.getIntent().getExtras() 返回了 null.请注意,在 makeview 中,您在创建包之后还没有任何事情.你需要做什么:

Okay, so that suggests that this.getIntent().getExtras() has returned null. Note that in makeview you haven't done anything after creating the bundle. Do you need to do:

myIntent.putExtras(b);

也许在最后?(我不是 Android 开发人员,所以我不知道 API,但这听起来很可能...)

at the end perhaps? (I'm not an Android developer so I don't know the API, but that sounds likely...)

正如其他人所指出的,您可能应该将 startActivity 调用 after 设置为意图.

As others have pointed out, you should potentially put the startActivity call after setting everything on the intent.

这篇关于使用Bundle传递数据时出现空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆