我正在制作一个bustin bieber应用程序,当我运行它时说:
threadid=1: thread exiting with uncaught exception (group=0xb60734f0)
我不知道我做错了! 这是我的代码:
//Packege
package com.stony.bustinbieber;
//Imports
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity{
//counter
int counter = 0;
ImageButton finishhimbutton;
ImageButton resetbutton;
ImageView jb1;
ImageView jb2;
ImageView jb3;
ImageView jb4;
ImageView jb5;
ImageView jb6;
MediaPlayer mp1;
MediaPlayer mp2;
MediaPlayer mp3;
TextView txtCount;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp1 = MediaPlayer.create(this, R.drawable.slap);
mp2 = MediaPlayer.create(this, R.drawable.resetsound);
mp3 = MediaPlayer.create(this, R.drawable.fatality);
txtCount.setText(String.valueOf(counter));
jb1 = (ImageView) findViewById(R.id.JB1);
jb2 = (ImageView) findViewById(R.id.JB2);
jb3 = (ImageView) findViewById(R.id.JB3);
jb4 = (ImageView) findViewById(R.id.JB4);
jb5 = (ImageView) findViewById(R.id.JB5);
jb6 = (ImageView) findViewById(R.id.JB6);
finishhimbutton = (ImageButton) findViewById(R.id.finishhim);
resetbutton = (ImageButton) findViewById(R.id.reserbutton);
txtCount = (TextView)findViewById(R.id.timer);
finishhimbutton.setOnClickListener(new OnClickListener() {
//Finish him button
@Override
public void onClick(View v) {
counter++;
txtCount.setText(String.valueOf(counter));
mp1.start();
switch(counter){
case 10:
jb1.setVisibility(ImageView.INVISIBLE);
jb2.setVisibility(ImageView.VISIBLE);
jb3.setVisibility(ImageView.INVISIBLE);
jb4.setVisibility(ImageView.INVISIBLE);
jb5.setVisibility(ImageView.INVISIBLE);
jb6.setVisibility(ImageView.INVISIBLE);
break;
case 20:
jb1.setVisibility(ImageView.INVISIBLE);
jb2.setVisibility(ImageView.INVISIBLE);
jb3.setVisibility(ImageView.VISIBLE);
jb4.setVisibility(ImageView.INVISIBLE);
jb5.setVisibility(ImageView.INVISIBLE);
jb6.setVisibility(ImageView.INVISIBLE);
break;
case 30:
jb1.setVisibility(ImageView.INVISIBLE);
jb2.setVisibility(ImageView.INVISIBLE);
jb3.setVisibility(ImageView.INVISIBLE);
jb4.setVisibility(ImageView.VISIBLE);
jb5.setVisibility(ImageView.INVISIBLE);
jb6.setVisibility(ImageView.INVISIBLE);
break;
case 40:
jb1.setVisibility(ImageView.INVISIBLE);
jb2.setVisibility(ImageView.INVISIBLE);
jb3.setVisibility(ImageView.INVISIBLE);
jb4.setVisibility(ImageView.INVISIBLE);
jb5.setVisibility(ImageView.VISIBLE);
jb6.setVisibility(ImageView.INVISIBLE);
break;
case 50:
jb1.setVisibility(ImageView.INVISIBLE);
jb2.setVisibility(ImageView.INVISIBLE);
jb3.setVisibility(ImageView.INVISIBLE);
jb4.setVisibility(ImageView.INVISIBLE);
jb5.setVisibility(ImageView.INVISIBLE);
jb6.setVisibility(ImageView.VISIBLE);
mp3.start();
break;
case 51:
jb1.setVisibility(ImageView.VISIBLE);
jb2.setVisibility(ImageView.INVISIBLE);
jb3.setVisibility(ImageView.INVISIBLE);
jb4.setVisibility(ImageView.INVISIBLE);
jb5.setVisibility(ImageView.INVISIBLE);
jb6.setVisibility(ImageView.INVISIBLE);
counter = 0;
txtCount.setText(String.valueOf(counter));
break;
default:
break;
}
}
}
);
resetbutton.setOnClickListener(new OnClickListener() {
//Reset Button
@Override
public void onClick(View v) {
mp2.start();
counter = 0;
txtCount.setText(String.valueOf(counter));
jb1.setVisibility(ImageView.VISIBLE);
jb2.setVisibility(ImageView.INVISIBLE);
jb3.setVisibility(ImageView.INVISIBLE);
jb4.setVisibility(ImageView.INVISIBLE);
jb5.setVisibility(ImageView.INVISIBLE);
jb6.setVisibility(ImageView.INVISIBLE);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
请帮助
答案 0 :(得分:2)
txtCount
未初始化
txtCount.setText(String.valueOf(counter));
此外,无需创建3个MediaPlayer对象。你可以拥有一首并播放不同的歌曲。
http://developer.android.com/reference/android/media/MediaPlayer.html
android:play audio files using single mediaplayer object and also display images
答案 1 :(得分:0)
这一行
txtCount.setText(String.valueOf(counter));
将来到
txtCount = (TextView)findViewById(R.id.timer);
因为如果你在此之前尝试使用它,它将为null