我正在尝试通过使用具有子活动的意图来更新按钮的文本来调试我的玩家抛出异常的原因,但是它返回null,直到我再次调用子活动,其中它更新了第一次调用的结果。
主要活动
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();
}
}
});
}
子活动
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();
}
});
示例:
Subroutine is called and I select ring.wav from the popup
之后,选择ring.wav将不会使用正确的路径更新按钮,直到再次单击该按钮。
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;
}
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
也许,你添加&#34; button.setText()&#34;在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;
//Change button with audio file's path
sound.button.setText(path);
}
break;
}
}
}