MediaPlayer崩溃。尝试调用虚拟方法

时间:2016-08-06 11:11:42

标签: java android

public class MainActivity extends AppCompatActivity {

MediaPlayer mp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mp = MediaPlayer.create(this, R.raw.crack);

    final ImageButton imageButton = (ImageButton) findViewById(R.id.nut);
    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            mp.start();
        }
    });
}

此代码给出了以下错误:

致命的例外:主要                                                                          过程: - ,PID:9232                                                                          java.lang.NullPointerException:尝试调用虚方法' void android.media.MediaPlayer.start()'在null对象引用上                                                                              at -.MainActivity $ 1.onClick(MainActivity.java:48)                                                                              在android.view.View.performClick(View.java:5226)                                                                              在android.view.View $ PerformClick.run(View.java:21266)                                                                              在android.os.Handler.handleCallback(Handler.java:739)                                                                              在android.os.Handler.dispatchMessage(Handler.java:95)                                                                              在android.os.Looper.loop(Looper.java:168)                                                                              在android.app.ActivityThread.main(ActivityThread.java:5845)                                                                              at java.lang.reflect.Method.invoke(Native Method)                                                                              在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:797)                                                                              在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)

有谁知道我的代码有什么问题?我遵循了一些教程,我认为我做得对,但显然没有。

0 个答案:

没有答案