我已经有这个简单的程序大约两个月了,前几天晚上,一个错误出现在用户面前,我不明白为什么。
public class mainmenu extends Activity implements OnPreparedListener {
private PopupWindow pw;
public static int RandomNum;
public static String quickStart;
private dbadapter mydbhelper;
MediaPlayer mpButtonClick;
Intent intent;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
Button bsound = (Button) findViewById(R.id.bCategories);
bsound.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
quickStart = "Cate";
onPrepared(mpButtonClick);
intent = new Intent ("wanted.pro.madlibslite.CATEGORIES");
intent.putExtra("quickStart", quickStart);
startActivity(intent);
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
//Menu Items
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.mainmenu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.help:
showHelp();
return true;
}
return false;
}
private void showHelp() {
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
pw = new PopupWindow(
inflater.inflate(R.layout.help, null, false),
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
true);
pw.showAtLocation(this.findViewById(R.id.bCategories), Gravity.CENTER, 0, 0);
}
public void onClickHelp(View helper){pw.dismiss();
}
//QuickStart
public void onClickQuick(View main) {
Toast.makeText(this, "Quick Start only available in full version.", 1000).show();
//RandomNum = (int) (Math.random()*101);
//quickStart = "Quick";
//mpButtonClick.start();
//startActivity(new Intent("wanted.pro.madlibslite.INPUTPAGE"));
}
public void onPrepared(MediaPlayer player) {
mpButtonClick.setOnPreparedListener(this);
try {
mpButtonClick.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mpButtonClick.start();
}
@Override
protected void onResume() {
mydbhelper = dbadapter.getInstance(mainmenu.this);
mydbhelper.open();
mpButtonClick = MediaPlayer.create(this, R.raw.button50);
super.onResume();
}
@Override
protected void onPause() {
mpButtonClick.release();
super.onPause();
}
}
有问题的错误...
java.lang.NullPointerException
at wanted.pro.madlibslite.mainmenu.onPrepared(mainmenu.java:107)
at wanted.pro.madlibslite.mainmenu$1.onClick(mainmenu.java:45)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9089)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3806)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
可悲的是,我没有他们使用的设备。但是希望这里的某人可能会看到我做错的事情,这会导致 onprepared 出错。