有人可以帮我解决下面代码中的问题,因为我没有通过super.onDestroy()
调用致命错误。
public class MainActivity extends Activity implements OnClickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public void onActivityResult(int requestcode, int resultcode, Intent data){
list_button.setImageResource(R.drawable.listbtn);
super.onActivityResult(requestcode, resultcode, data);
if (resultcode == 100){
//* Intent in3 = getIntent();
int songnum = data.getExtras().getInt("songindex");
playsong(songnum);
player.start();
play_button.setImageResource(R.drawable.play_aft);
pause_button.setImageResource(R.drawable.pause_int);
stop_button.setImageResource(R.drawable.stop_int);
}
seek_bar.setMax(player.getDuration());
}
private void playsong(int songnum) {
try {
player.reset();
player.setDataSource(Songlist.get(songnum).get("songname"));
player.prepare();
seek_bar.setMax(player.getDuration());
seekUpdation();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Runnable run = new Runnable() {
@Override
public void run() {
seekUpdation();
}
};
public void seekUpdation() {
seek_bar.setProgress(player.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
}
public void onDestroy(){
super.onDestroy();
player.stop();
player.release();
Log.d(TAG, "Player Crushed");
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.play:
text_shown.setText("Playing...");
player.start();
pause_button.setImageResource(R.drawable.pause_int);
stop_button.setImageResource(R.drawable.stop_int);
play_button.setImageResource(R.drawable.play_aft);
break;
case R.id.pause:
if(player.isPlaying()){
player.pause();
text_shown.setText("Paused...");
play_button.setImageResource(R.drawable.play_int);
pause_button.setImageResource(R.drawable.pause_aft);
}
break;
case R.id.stop:
if(player.isPlaying()){
player.stop();
text_shown.setText("Stopped...");
play_button.setImageResource(R.drawable.play_int);
pause_button.setImageResource(R.drawable.pause_int);
stop_button.setImageResource(R.drawable.stop_aft);
player.prepareAsync();
player.seekTo(0);
}
else{
try {
player.prepare();
player.seekTo(0);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
}
}
}
logcat的
08-26 23:17:37.993: E/AndroidRuntime(437): FATAL EXCEPTION: main
08-26 23:17:37.993: E/AndroidRuntime(437): android.app.SuperNotCalledException: Activity {com.example.musicplay/com.example.musicplay.MainActivity} did not call through to super.onDestroy()
08-26 23:17:37.993: E/AndroidRuntime(437): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2661)
08-26 23:17:37.993: E/AndroidRuntime(437): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:2690)
08-26 23:17:37.993: E/AndroidRuntime(437): at android.app.ActivityThread.access$2100(ActivityThread.java:117)
08-26 23:17:37.993: E/AndroidRuntime(437): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:964)
08-26 23:17:37.993: E/AndroidRuntime(437): at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 23:17:37.993: E/AndroidRuntime(437): at android.os.Looper.loop(Looper.java:123)
08-26 23:17:37.993: E/AndroidRuntime(437): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-26 23:17:37.993: E/AndroidRuntime(437): at java.lang.reflect.Method.invokeNative(Native Method)
08-26 23:17:37.993: E/AndroidRuntime(437): at java.lang.reflect.Method.invoke(Method.java:507)
08-26 23:17:37.993: E/AndroidRuntime(437): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-26 23:17:37.993: E/AndroidRuntime(437): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-26 23:17:37.993: E/AndroidRuntime(437): at dalvik.system.NativeStart.main(Native Method)
08-26 23:19:31.443: E/MediaPlayer(481): Attempt to call getDuration without a valid mediaplayer
08-26 23:19:31.443: E/MediaPlayer(481): error (-38, 0)
08-26 23:19:31.487: E/MediaPlayer(481): Error (-38,0)
答案 0 :(得分:0)
您应该使用自己的super.onDestroy()
方法拨打onDestroy()
最后:
public void onDestroy(){
player.stop();
player.release();
Log.d(TAG, "Player Crushed");
super.onDestroy();
}
我的假设是MediaPlayer
需要Activity
的上下文,因此在调用Activity
和{{1}后必须销毁stop()
}}