考虑应用程序有三个活动,即A1、A2、A3:
A1使用附加在其意图“值”中的值调用A2
Intent i=new Intent(A1.this,A2.class);
i.putExtra("value",editTextVal); //editTextVal is got from an editText during Runtime其中A2接受来自A1的附加值并将其存储在“sample”中:
sample=getIntent().getExtra().getString("value");现在控件从A2 .i.e.转到A2意图到A3,现在当A3调用ActivityA2时出现了一个错误,因为A2有一个.getExtra(),它试图从意图中获取附加的数据,因为在A3中使用的意图没有.putExtra(),这很简单,
Intent i3=new Intent(A3.this,A2.class);所以会出现一个运行时错误。帮我解决这个..。
发布于 2017-04-24 13:12:26
当您从A3迁移到A2时,A2活动将搜索一个Bundle对象,但是由于您没有将任何值从A3传递给A2,所以它给空点异常提供了一种方法:设置一个标志(静态变量),如果FLAG==1试图获取Bundle对象,则在从A1到A2的标记上进行从A1到A2的转换时,设置一个标志(静态变量)并为其赋值。每当您从A2移动时,请确保将标志更改为除1以外的其他值。
//Declare a Variable FLAG in A1 as
public static int FLAG;
// For transition from A1 to A2
Intent I =new Intent(A1.this,A2.class);
I.putExtras("Key","Value");
FLAG=1;
startActivity(I);
//on your A2 activity
if(A1.FLAG==1)
{
Bundle extras=getIntent().getExtras();
String Value=extras.getString("Key");
}
//When you make a transition to A3
Intent i1=new Intent(A1.this,A2.class);
A.FLAG=2;
startAcitivy(i1);发布于 2017-04-24 13:11:43
if(getIntent().hasExtra("value")) {
sample=getIntent().getStringExtra("value");
}
/* while using sample check for null
*/
if(!TextUtils.isEmpty(sample)) {
// use sample here
}https://stackoverflow.com/questions/43588875
复制相似问题