当我在Android模拟器中启动以下应用程序时,应用程序将停止并显示以下错误消息:“应用程序播放器(进程kilbo.net)已意外停止。请再试一次。” 没有错误,警告,我在调试器中找不到与问题相关的任何内容。 这是我遇到的一个常见问题,而不仅仅是这个应用程序。
package kilbo.net;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class Player extends Activity implements Runnable, OnClickListener{
private TextView Status;
private ProgressBar progressBar;
private Button StartMedia;
private Button Stop;
private MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Status = (TextView) findViewById(R.id.Status);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
StartMedia = (Button) findViewById(R.id.StartMedia);
Stop = (Button) findViewById(R.id.Stop);
//Removing this two lines "fixes" the problem. So the error is probably related to this
StartMedia.setOnClickListener(this);
Stop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.equals(StartMedia)){
if(mp != null && mp.isPlaying()) return;
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource("http://kilbo.net/musik/Norrskenet.mp3");
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
Status.setText("playingMedia");
progressBar.setVisibility(ProgressBar.VISIBLE);
progressBar.setProgress(0);
progressBar.setMax(mp.getDuration());
new Thread(this).start();
}
if(v.equals(Stop) && mp!=null){
mp.stop();
mp = null;
Status.setText("Stop");
progressBar.setVisibility(ProgressBar.GONE);
}
}
@Override
public void run() {
int CurrentPosition= 0;
int total = mp.getDuration();
while(mp!=null && CurrentPosition<total){
try {
Thread.sleep(1000);
CurrentPosition= mp.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e){
return;
}
progressBar.setProgress(CurrentPosition);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button id="@+id/Stop"
android:layout_width="125px" android:layout_height="50px"
android:background="#FF222222" android:text="Start" />
<Button id="@+id/StartMedia"
android:layout_width="125px" android:layout_height="50px"
android:background="#FF222222" android:text="Start" />
<ProgressBar id="@+id/progressBar"
android:layout_width="125px" android:layout_height="50px"
android:background="#FF222222" android:text="Start"
style="?android:attr/progressBarStyleHorizontal" />
<TextView id="@+id/Status"
android:layout_width="125px" android:layout_height="50px"
android:background="#FF222222" android:text="Start" />
</LinearLayout>
答案 0 :(得分:0)
我认为当de Debugger尝试时你得到一个NPE: StartMedia.setOnClickListener(本);
您应该尝试找到Stacktrace。
答案 1 :(得分:0)
你说错误是
02-24 12:06:17.818: ERROR/AndroidRuntime(369): java.lang.RuntimeException: Unable to start activity ComponentInfo{kilbo.net/kilbo.net.Player}: java.lang.NullPointerException
您的活动是否在清单中正确宣布了?