Android 从onClick方法中的上一个活动获取额外的字符串值
我的类实现了onClickListener,因此我做了以下工作: 一旦创建:Android 从onClick方法中的上一个活动获取额外的字符串值,android,Android,我的类实现了onClickListener,因此我做了以下工作: 一旦创建: final ImageButton btn_audio = (ImageButton)findViewById(R.id.btn_tool_audio); btn_audio.setOnClickListener(this); onClick方法: @Override public void onClick(View v) { switch(v.g
final ImageButton btn_audio = (ImageButton)findViewById(R.id.btn_tool_audio);
btn_audio.setOnClickListener(this);
onClick方法:
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_tool_audio:
Bundle extras = getIntent().getExtras();
String audio_text_url = extras.getString("audio_text_url");
Intent launchAudio= new Intent(Intent.ACTION_VIEW, audio_text_url);
startActivity(launchAudio);
break;
但是字符串的值是“null”,问题是如何从onClick方法中的前一个活动中获取它的值
仅供参考,当我调用onCreate内部的额外函数时,我在获取值方面没有任何问题。也许可以从onCreate中取出捆绑包,稍后再使用它
Bundle bndl = null;
protected void onCreate(Bundle savedInstanceState) {
...
...
bndl = getIntent().getExtras();
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_tool_audio:
String audio_text_url = bndl.getString("audio_text_url");
Intent launchAudio= new Intent(Intent.ACTION_VIEW, audio_text_url);
startActivity(launchAudio);
break;
也许可以从onCreate中取出捆绑包,稍后再使用它
Bundle bndl = null;
protected void onCreate(Bundle savedInstanceState) {
...
...
bndl = getIntent().getExtras();
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_tool_audio:
String audio_text_url = bndl.getString("audio_text_url");
Intent launchAudio= new Intent(Intent.ACTION_VIEW, audio_text_url);
startActivity(launchAudio);
break;
是否尝试将onCreate中的附加字符串引用到成员并稍后使用
private String _saveMe;
protected void onCreate(Bundle savedInstanceState) {
_saveMe = getIntent().getExtras().getString("audio_text_url");
}
是否尝试将onCreate中的附加字符串引用到成员并稍后使用
private String _saveMe;
protected void onCreate(Bundle savedInstanceState) {
_saveMe = getIntent().getExtras().getString("audio_text_url");
}
使用
而不是
Bundle extras = getIntent().getExtras();
使用
而不是
Bundle extras = getIntent().getExtras();
向我们展示在捆绑包中设置值的代码向我们展示在捆绑包中设置值的代码