【发布时间】:2018-12-11 22:33:53
【问题描述】:
希望找到解决我的问题的方法,所以当单击 ListView 项目和播放音频文件时,我有一个进度条显示,问题是当音频文件完成播放应用程序崩溃并在 logcat 中给我这个错误:
12-11 14:32:17.330 6190-6216/com.example.android.app E/AndroidRuntime: 致命异常: Thread-2 进程:com.example.android.app,PID:6190 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“boolean android.media.MediaPlayer.isPlaying()” 在 com.example.android.app.MainActivity$4.run(MainActivity.java:371) 在 java.lang.Thread.run(Thread.java:764)
这是进度条的代码:
Runnable _progressUpdater;
private void createProgressParentThread() {
_progressUpdater = new Runnable() {
@Override
public void run() {
while(mMediaPlayer.isPlaying()) {
try
{
int current = 0;
int total = mMediaPlayer.getDuration();
progressBarParent.setMax(total);
Log.d("ThangTB", "total:"+total);
progressBarParent.setIndeterminate(false);
while(mMediaPlayer!=null && current<total){
try {
Thread.sleep(200); //Update once per second
current = mMediaPlayer.getCurrentPosition();
//Removing this line, the track plays normally.
progressBarParent.setProgress(current);
} catch (InterruptedException e) {
} catch (Exception e){
}
}
}
catch(Exception e)
{
//Don't want this thread to intefere with the rest of the app.
}
}
if (!mMediaPlayer.isPlaying()) {
try {
Log.d("ThangTB", "callllllllllllllllll");
GONELAYOUT();
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
Thread thread = new Thread(_progressUpdater);
thread.start();
}
public void GONELAYOUT(){
this.runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
progressBarParent.setProgress(0);
linearLayout_contentProgress.setVisibility(View.GONE);
}
});
}
public void VISIBLELAYOUT(){
this.runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
progressBarParent.setProgress(0);
linearLayout_contentProgress.setVisibility(View.VISIBLE);
}
});
}
我在 onItemClick 中调用这些方法:
mMediaPlayer.start();
VISIBLELAYOUT();
createProgressParentThread();
【问题讨论】:
-
MainActivity 的第 371 行在哪里?
-
@IntsabHaider 行是 371 while(mMediaPlayer.isPlaying()) {try int current = 0;整数 = mMediaPlayer.getDuration(); progressBarParent.setMax(total); Log.d("ThangTB", "total:"+total); progressBarParent.setIndeterminate(false);
-
当时基本上你的mMediaPlayer是Null
-
添加完整的活动代码很容易解决为什么它会为空
标签: android listview progress-bar android-mediaplayer