gpt4 book ai didi

android - 打开时应用崩溃

转载 作者:行者123 更新时间:2023-12-03 17:49:25 25 4
gpt4 key购买 nike

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

如果我做错了,请引导我。

最佳答案

错误是在按钮初始化时

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

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

关于android - 打开时应用崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30842769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com