【问题标题】:Null pointer exception when using Bundle to pass data使用Bundle传递数据时出现空指针异常
【发布时间】:2011-03-15 18:59:56
【问题描述】:

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

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

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

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);
}

然后在它正在做的新课程中......

               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"); 得到一个空指针异常,我不知道为什么。在调用意图等之前我已经尝试过捆绑,但我没有解决问题的运气。有人知道为什么吗?

我还包括了一些错误,以防你们中的任何人感兴趣。

            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

谢谢!

【问题讨论】:

  • @Jon Skeet float original[] = b.getFloatArray("tweets");
  • 好的,这暗示了一个答案。请在以后的问题中提供所有相关信息。

标签: java android nullpointerexception


【解决方案1】:

好的,这表明this.getIntent().getExtras() 已返回null。请注意,在makeview 中,您在创建捆绑包后还没有完成 任何事情。是否需要做:

myIntent.putExtras(b);

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

编辑:正如其他人指出的那样,您应该将startActivity 调用after 设置为意图。

【讨论】:

    【解决方案2】:

    您的makeview 方法存在逻辑缺陷,您需要在启动前将附加内容添加到意图中。此外,强烈建议使用常量 (GLCamTest.TWEETS) 作为密钥。

    public void makeview() {
        Intent myIntent = new Intent(this, GLCamTest.class);
        myIntent.putExtra(GLCamTest.TWEETS, camObjCoord);//assuming camObjCoord is float[]
        this.startActivity(myIntent);
    }
    

    另一边

    Bundle b = this.getIntent().getExtras();
    float original[];
    if (b!=null) {
        original = b.getFloatArray(GLCamTest.TWEETS);
    }
    if (original!=null) {
       //do something with original
    }
    

    【讨论】:

      【解决方案3】:
         public void makeview() {
      Intent myIntent = new Intent(this, GLCamTest.class);
      this.startActivity(myIntent);
      Bundle b = new Bundle();
      b.putFloatArray("tweets", camObjCoord);
      

      }

      我认为你犯的错误是你已经启动了活动this.startActivity(myIntent);,然后你传递了捆绑包值

       Bundle b = new Bundle();
          b.putFloatArray("tweets", camObjCoord);
      

      。 ---->>> so it will pass nothing。所以在接收端

      Bundle b = this.getIntent().getExtras();
          float original[] = b.getFloatArray("tweets");
      

      ---->>> this above code receive "null" value.

      所以尝试先捆绑然后开始你的活动。

      试试这个:

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

      【讨论】:

        【解决方案4】:

        如果您在该行收到 NullPointerException

        float original[] = b.getFloatArray("tweets"); 
        

        那么唯一的选择是 b 为空。 this.getIntent().getExtras() 在某些情况下可以返回 null 吗?你应该检查一下。

        【讨论】:

          【解决方案5】:

          this.getIntent().getExtras() 已返回 null。确保在 makeview 中创建捆绑包后您没有做任何事情。你需要:

          myIntent.putExtras(b);

          最后

          【讨论】:

            【解决方案6】:

            嗯,我有类似的问题。在我的情况下,当我检查我的 bundle.getString() 是否等于 null 时,发生了 null 点异常。

            在我的案例中,我是这样解决的:

            Intent intent = getIntent();        
                if(intent.hasExtra("nomeUsuario")){
                    bd = getIntent().getExtras();
                    if(!bd.getString("nomeUsuario").equals(null)){
                        nomeUsuario = bd.getString("nomeUsuario");
                    }
                }
            

            【讨论】:

              【解决方案7】:

              试试这个,对我有用

               Intent i = getIntent();
              
                 if (i == null) 
                     Log.d(" DEBUG AGILANBU ", "Here the intent value was null");           
                 else
                     Log.d(" DEBUG ", "Intent OK");
              
                 String strGetVal  = i.getStringExtra("PackageName"); // Here we set an exception points to this line
                 Log.d("*** DEBUG", rec + " " + strGetVal);
              

              快乐编码

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2023-03-31
                • 1970-01-01
                • 1970-01-01
                • 2020-03-21
                • 2014-09-26
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多