[英]Main Activity is not updating with Sub activity's result until called again
I'm trying to update a button's text by using an intent with a subactivity to debug why my player was throwing an exception, but it returns null until I call the subactivity again where it updates with the result of the first call. 我正在尝试通过使用具有子活动的意图来调试按钮的原因来调试我的播放器引发异常的原因,但是它会返回null,直到我再次调用该子活动,并在第一次调用的结果处进行更新。
Main Activity 主要活动
public class MainActivity extends AppCompatActivity {
static final int PICK_AUDIO_REQUEST = 1;
String ResultPath;
String path;
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Sound sound = new Sound(R.id.button,this);
sound.button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(sound.mp == null){
Intent intent = new Intent(MainActivity.this, Pop.class);
startActivityForResult(intent, PICK_AUDIO_REQUEST);
//Pass audio file into sound
/*try {
sound.mp.setDataSource(path);
} catch (IOException e) {
e.printStackTrace();
}*/
}
else if(sound.mp.isPlaying()) {
sound.mp.pause();
}
else{
sound.mp.seekTo(0);
sound.mp.start();
}
}
});
}
Subactivity 子活动
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent resultIntent = new Intent();
resultIntent.putExtra("song path", mMusicList[arg2]);
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
});
Example: 例:
Subroutine is called and I select ring.wav from the popup 调用子例程,然后从弹出窗口中选择ring.wav
Afterwards, selecting ring.wav will not update the button with the correct path until you click on the button again. 之后,选择ring.wav不会使用正确的路径更新按钮,除非再次单击该按钮。
onActivityResult onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (PICK_AUDIO_REQUEST) : {
if (resultCode == Activity.RESULT_OK) {
ResultPath = data.getStringExtra("song path");
path = extStorageDirectory + File.separator + ResultPath;
}
break;
}
}
}
Maybe, you add "button.setText()" in onActivityResult. 也许您在onActivityResult中添加了“ button.setText()”。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (PICK_AUDIO_REQUEST) : {
if (resultCode == Activity.RESULT_OK) {
ResultPath = data.getStringExtra("song path");
path = extStorageDirectory + File.separator + ResultPath;
//Change button with audio file's path
sound.button.setText(path);
}
break;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.