为啥当我按下返回按钮时我的程序崩溃
Posted
技术标签:
【中文标题】为啥当我按下返回按钮时我的程序崩溃【英文标题】:Why my program crash when i press back Button为什么当我按下返回按钮时我的程序崩溃 【发布时间】:2016-12-21 23:39:30 【问题描述】:[这是sd卡中歌曲的列表视图][1]
****这是我的 MainActivity 代码****
package com.example.jawad.musicplayer;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
ListView musicListView;
ArrayList<File> musicFiles;
MediaPlayer mediaplayer;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
musicListView = (ListView) findViewById(R.id.musiclistviewId);
musicFiles = findSongs(Environment.getExternalStorageDirectory());
CustomListViewAdapter CLV = new CustomListViewAdapter(MainActivity.this,R.layout.list_row,musicFiles);
musicListView.setAdapter(CLV);
musicListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
startActivity(new Intent(MainActivity.this,PlayerActivity.class).putExtra("music",musicFiles).putExtra("pos",position));
);
/*for (int i =0 ;i < music.size();i++)
toast(music.get(i).getName().toString());
*/
public ArrayList<File> findSongs(File root)
File[] files = root.listFiles();
ArrayList<File> tempSongs = new ArrayList<>();
for(File singlefile : files)
if(singlefile.isDirectory() && !singlefile.isHidden())
tempSongs.addAll(findSongs(singlefile));
else
if(singlefile.getName().endsWith(".mp3"))
tempSongs.add(singlefile);
return tempSongs;
public void toast(String text)
Toast.makeText(getApplicationContext(),text,Toast.LENGTH_LONG).show();
@Override
public void onClick(View v)
这是“玩家活动”的代码
package com.example.jawad.musicplayer;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
public class PlayerActivity extends AppCompatActivity implements View.OnClickListener
Button playButton;
MediaPlayer mplayer;
private Button prevButton;
private Button nextButton;
SeekBar sb;
Thread updateThread;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
playButton = (Button) findViewById(R.id.playButtonId);
prevButton=(Button) findViewById(R.id.prevButtonId);
nextButton = (Button) findViewById(R.id.nextButtonId);
sb = (SeekBar) findViewById(R.id.seekbarId);
playButton.setOnClickListener(this);
nextButton.setOnClickListener(this);
prevButton.setOnClickListener(this);
Bundle extra = getIntent().getExtras();
if(extra!=null)
ArrayList<File> list = (ArrayList) extra.getParcelableArrayList("music");
int pos = extra.getInt("pos");
File file = list.get(pos);
// Toast.makeText(getApplicationContext(),file.getName().toString(),Toast.LENGTH_LONG).show();
Uri URI = Uri.parse(file.toString());
mplayer=MediaPlayer.create(getApplicationContext(),URI);
@Override
public void onClick(View v)
int id =v.getId();
switch(id)
case R.id.playButtonId:
if(mplayer.isPlaying())
pauseMusic();
else
playMusic();
break;
case R.id.prevButtonId:
break;
case R.id.nextButtonId:
break;
public void playMusic()
if(mplayer!= null)
mplayer.start();
playButton.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.ic_media_pause));
updateThread = new Thread()
@Override
public void run()
sb.setMax(mplayer.getDuration());
int toatalduration = mplayer.getDuration();
int currentposition = 0;
while (currentposition < toatalduration && mplayer.isPlaying())
try
sleep(500);
currentposition = mplayer.getCurrentPosition();
sb.setProgress(currentposition);
catch (InterruptedException e)
e.printStackTrace();
;
updateThread.start();
@Override
public void onBackPressed()
updateThread.stop();
onDestroy();
public void pauseMusic()
if (mplayer!=null)
mplayer.pause();
playButton.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.ic_media_play));
@Override
protected void onDestroy()
if(mplayer!=null && mplayer.isPlaying())
mplayer.stop();
mplayer.release();
mplayer = null;
super.onDestroy();
基本上我是 android 编程的新手,并练习在 android 中构建媒体播放器。我对 Java 有很好的掌握,但我仍然无法找到这个问题的解决方案,所以欢迎你的帮助。 我还重写了 onBackPressed() 方法,在该方法中我停止线程并停止音乐播放器,但仍然 android studio 给我错误。
这是我的 LogCat:
08-15 14:11:00.721 1429-1429/com.example.jawad.musicplayer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jawad.musicplayer, PID: 1429
android.util.SuperNotCalledException: Activity com.example.jawad.musicplayer/com.example.jawad.musicplayer.PlayerActivity did not call through to super.onDestroy()
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3472)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3501)
at android.app.ActivityThread.access$1400(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1249)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
08-15 14:11:00.721 534-760/system_process W/ActivityManager: Force finishing activity com.example.jawad.musicplayer/.MainActivity
[08-15 14:11:00.741 534: 760 D/] HostConnection::get() New Host Connection established 0xb7cbc840, tid 760
【问题讨论】:
你读过错误吗? “android.util.SuperNotCalledException: Activity com.example.jawad.musicplayer/com.example.jawad.musicplayer.PlayerActivity 没有调用 super.onDestroy()” 我真的建议您(和其他新用户)首先使用调试器并尝试逐步找出您的问题并查看具体内容,而不是来到这里并进行大量代码/logcat 转储 永远不要打电话给Thread.stop()
。
【参考方案1】:
试试下面的代码:
@Override
protected void onDestroy()
if(mplayer!=null && mplayer.isPlaying())
mplayer.stop();
mplayer.release();
mplayer = null;
super.onDestroy();
【讨论】:
以上是关于为啥当我按下返回按钮时我的程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章