Java 如果未播放mediaplayer,应用程序在应用程序退出时崩溃
我的应用程序在应用程序退出时崩溃。除非歌曲正在播放且您退出,否则它永远不会崩溃。但如果歌曲未播放/播放,我退出应用程序,它将退出并崩溃。我在ondestroy函数的活动中终止了您的任务。 请帮帮我 这是Tip1Activity.javaJava 如果未播放mediaplayer,应用程序在应用程序退出时崩溃,java,android,android-mediaplayer,Java,Android,Android Mediaplayer,我的应用程序在应用程序退出时崩溃。除非歌曲正在播放且您退出,否则它永远不会崩溃。但如果歌曲未播放/播放,我退出应用程序,它将退出并崩溃。我在ondestroy函数的活动中终止了您的任务。 请帮帮我 这是Tip1Activity.java public class Tip1Activity extends AppCompatActivity { private ArrayList<Music> arrayList; private CustomMusicAdapter
public class Tip1Activity extends AppCompatActivity {
private ArrayList<Music> arrayList;
private CustomMusicAdapter adapter;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tip1);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
recyclerView = findViewById(R.id.recyclerViewList);
arrayList = new ArrayList<>();
arrayList.add(new Music("001 Veni, veni, Emmanuel", "SONGS OF WORSHIP", R.raw.veni_emmanuel));
arrayList.add(new Music("002 O come. O come Emmanuel", "SONGS OF WORSHIP", R.raw.o_come_emmanuel));
arrayList.add(new Music("003 Savior of the nations, come", "SONGS OF WORSHIP", R.raw.savior_of_nations));
arrayList.add(new Music("004 On Jordan's bank the Baptist's cry", "SONGS OF WORSHIP", R.raw.on_jordans_bank));
arrayList.add(new Music("005 Lo, he comes", "SONGS OF WORSHIP", R.raw.lo_he_comes));
arrayList.add(new Music("006 O Word that goest forth on high", "SONGS OF WORSHIP", R.raw.o_word_that_goes_forth));
arrayList.add(new Music("007 Wake, awake", "SONGS OF WORSHIP", R.raw.wake_awake));
arrayList.add(new Music("008 Rorate caeli", "SONGS OF WORSHIP", R.raw.rorate_caeli));
arrayList.add(new Music("009 Conditor alme siderum", "SONGS OF WORSHIP", R.raw.conditor_alme_siderum));
arrayList.add(new Music("010 Creator of the stars of night", "SONGS OF WORSHIP", R.raw.creator_of_the_stars));
adapter = new CustomMusicAdapter(this, R.layout.custom_music_row, arrayList);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(false);
}
public void onDestroy() {
super.onDestroy();
if( adapter !=null ){
adapter.release();
}
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to close App?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Tip1Activity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem menuItem = menu.findItem(R.id.search_view);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return true;
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.item1:
Toast.makeText(this, "Settings Selected", Toast.LENGTH_SHORT).show();
startActivity(new Intent(this, Main2Activity.class));
return true;
//DO YOUR FUNCTIONALITY HERE
default:
return super.onOptionsItemSelected(item);
}
}
}
这就是我所做的,仍然给我带来错误的事情
//停止播放器
holder.mImageStop.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
如果(mediaPlayer!=null)
{
if(mediaPlayer.isPlaying())
{
mediaPlayer.stop();
mediaPlayer.release();
}
holder.mImageaplay.setImageResource(R.drawable.ic_播放);
}}
});
}
原因:java.lang.NullPointerException:尝试调用虚拟机
空对象上的方法“void android.media.MediaPlayer.stop()”
参考文献
试一试
if(mediaPlayer!=null)
{
if(mediaPlayer.isPlaying())
{
mediaPlayer.stop();
mediaPlayer.release();
}
}
除非有人单击播放按钮,否则您的
mediaPlayer
不会初始化,您必须在调用mediaPlayer.stop()之前检查它是否为null代码>它发生在这里:CustomMusicAdapter.release(CustomMusicAdapter.java:38)
检查(mediaplayer!=null)
如果mediaplyser不是null
它仍然是空的,您只能毫无例外地释放它crashing@Joseph您是否在release()
方法上添加了空检查?
2020-06-24 12:44:35.598 27137-27137/com.josephgritchen.songsofworship E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.josephgritchen.songsofworship, PID: 27137
java.lang.RuntimeException: Unable to destroy activity {com.josephgritchen.songsofworship/com.josephgritchen.songsofworship.Tip1Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.stop()' on a null object reference
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4425)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4443)
at android.app.ActivityThread.-wrap5(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1684)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6549)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.stop()' on a null object reference
at com.josephgritchen.songsofworship.CustomMusicAdapter.release(CustomMusicAdapter.java:38)
at com.josephgritchen.songsofworship.Tip1Activity.onDestroy(Tip1Activity.java:67)
at android.app.Activity.performDestroy(Activity.java:7239)
at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1249)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4412)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4443)
at android.app.ActivityThread.-wrap5(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1684)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6549)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)
if(mediaPlayer!=null)
{
if(mediaPlayer.isPlaying())
{
mediaPlayer.stop();
mediaPlayer.release();
}
}