应用程序在打开时崩溃

app crashes when opening

提问人:nitzdaking 提问时间:6/15/2015 最后编辑:Jitesh Prajapatinitzdaking 更新时间:8/16/2016 访问量:318

问:

我正在尝试制作一个在单击按钮时触发声音的应用程序。但是每次打开应用程序时,我的应用程序都会崩溃,这是 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();

            }
        });
    }
}

日志:

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)

请引导我,我做错了什么。

Android 崩溃

评论

0赞 Selvin 6/15/2015
什么是空指针异常的可能重复,我该如何修复它?
0赞 Selvin 6/15/2015
显然,带有 ID 的视图:不是布局的一部分,故事结束R.id.btnR.layout.activity_main

答:

0赞 Kartheek 6/15/2015 #1

错误出在按钮初始化时

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

确保您已在 中创建了一个按钮,其 id 为 .activity_main.xmlbtn