2015-12-03 167 views
-1

我在本网站发现了类似于我的其他问题;我尝试了他们的sugestions,但我不断收到NullPointExceptionError如下:将一个int数组传递给另一个活动

“java.lang.RuntimeException:无法启动活动ComponentInfo java.lang.NullPointerException:尝试调用虚拟方法'int [] android.os.Bundle。 getIntArray(java.lang.String中)”上的空对象引用”

这是我迄今为止:原数组的

活动:接收到该阵列

Bundle myBundle = new Bundle(); 
myBundle.putIntArray("PTArray", PTFindings); 
Intent intent = this.getIntent(); 
intent.putExtra("PTArray", PTFindings); 
startActivity(intent); 

活性:

int[] PTData = getIntent().getIntArrayExtra("PTArray"); 
Bundle myBundle = getIntent().getExtras(); 
PTData = myBundle.getIntArray("PTArray"); 

***任何帮助是极大的赞赏

+0

恕我直之,你可以参考我的答案在http://stackoverflow.com/questions/33866314/when-clicked-on-grid-view-how-to-send-arndistposition-to-another-actvity/33866720#33866720了解其关于将阵列列表传递给其他活动的逻辑。 – BNK

+1

@BNK您可以直接传递Integer数组,无需实现Parcelable的类实现 –

+0

@BenjaminScharbau感谢您的信息,我还没有测试过它:) – BNK

回答

1

试试这个你原来:

Intent intent = new Intent(thisActivity.this, nextActivity.class); 
intent.putExtra("PTArray", PTFindings); 
startActivity(intent); 

这对于获得:

Bundle myBundle = getIntent().getExtras(); 
int[] PTData = myBundle.getIntArray("PTArray"); 
0

你不是在创造它传递给新的活动的意图。尝试改变

Intent intent = this.getIntent(); 

Intent intent = new Intent(this, ComponentInfo.class); 

那么意图应该传递给接受的类。

另一点是,在您的代码中,您不会将Bundle传递给Intent。所以getIntent().getExtras();实际上会在您的代码中返回null,因此myBundle.getIntArray会导致NPE。所以,当你想访问捆绑在你的下一个活动,不要忘记调用

intent.putExtra(myBundle); 
0

你没有你的myBundle或目标活动传递给意图,试图用这样的:

Bundle myBundle = new Bundle(); 
myBundle.putIntArray("PTArray", PTFindings); 
Intent intent = new Intent(this, ComponentInfo.class); 
intent.putExtra("PTArray", PTFindings); 
intent.putExtra(myBundle); 
startActivity(intent); 
相关问题