问题描述
我的问题是有点基础。我一直在学习JAVA的和Android code。在这里,我有点困惑于如何调用,我已经通过一个意图发送的值。
My question is a bit basic. I've been learning to code on JAVA and android. Here I am a bit confused on how to call the values that I have sent via an intent.
在我的第一个活动,这是我使用的意图。
In my first activity this is the intent that I am using.
Intent intent = new Intent(MainActivity.this, Secondactivity.class);
String regName1 = regName;
intent.putExtra(regName1, regNameSplit[0]);
startActivity(intent);
下面regName1将包含三个值。的SessionID,URL,名称由分裂 -
Here regName1 will contain three values. SessionID,URL,Name split by "-".
在我SecondActivity
In my SecondActivity
public class Secondactivity extends Activity {
public final String TAG = "###---Secondactivity---###";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity);
Log.i(TAG,"before if statement");
if (getIntent().getStringExtra("regName1") != null){
getIntent().getStringExtra("regName1").split("-");
String[] str = "regName";
Log.i(TAG, ""+str[0]+str[1]+str[2])
}
}
}
如果regName1的价值永远是空。
The value if regName1 always comes as null.
推荐答案
这行
intent.putExtra(regName1, regNameSplit[0]);
必须是这样的,而不是
Needs to be like this instead
intent.putExtra("regName1", regNameSplit[0]); // note the quotes
但是,你正在使用regName1作为变量...你期望怎样二等知道变量?
BUT you are using regName1 as a variable... how do you expect the second class to know that variable?
使用字符串资源来代替。
Use a string resource instead.
这篇关于从另一个类检索的意图值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!