应用程序在打开时崩溃

时间:2015-06-15 10:23:10

标签: android crash

我正在尝试创建一个在点击按钮时触发声音的应用。但是每当我打开应用程序时,我的应用程序崩溃,java代码的问题,因为当我删除代码时,它运行正常。

我的代码是。

    public class MainActivity extends Activity {

    LinearLayout hello;
    Button btn;
    MediaPlayer sound;

    @Override
    protected void onPause() {
        super.onPause();
        sound.release();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sound = MediaPlayer.create(this, R.raw.kick);
        btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.kick);
                mp.start();

            }
        });
    }
}

logcat的:

java.lang.RuntimeException: Unable to start activity ComponentInfo{sp.umavibe.com.sp404/sp.umavibe.com.sp404.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1343)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5401)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:725)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:615)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
            at sp.umavibe.com.sp404.MainActivity.onCreate(MainActivity.java:28)
            at android.app.Activity.performCreate(Activity.java:6116)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
    at             android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
     at android.app.ActivityThread.-wrap11(ActivityThread.java)
     at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1343)
     at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5401)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:725)
            at       com.android.internal.os.ZygoteInit.main(ZygoteInit.java:615)

请指导我,我做错了。

1 个答案:

答案 0 :(得分:0)

错误发生在按钮初始化

btn = (Button) findViewById(R.id.btn);

确保您已在activity_main.xml中创建了一个ID为btn的按钮。