应用程序在打开时崩溃
Posted
技术标签:
【中文标题】应用程序在打开时崩溃【英文标题】:app crashes when opening 【发布时间】:2015-08-30 19:22:10 【问题描述】:我正在尝试制作一个在单击按钮时触发声音的应用。但是我的应用程序每次打开应用程序时都会崩溃,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 ComponentInfosp.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)
请指导我,我做错了什么。
【问题讨论】:
What is a Null Pointer Exception, and how do I fix it? 的可能重复项 显然查看 id:R.id.btn
不是R.layout.activity_main
布局的一部分,故事结束
【参考方案1】:
错误是在按钮初始化时
btn = (Button) findViewById(R.id.btn);
确保您在activity_main.xml
中创建了一个ID 为btn
的按钮。
【讨论】:
以上是关于应用程序在打开时崩溃的主要内容,如果未能解决你的问题,请参考以下文章
我创建的简单apk在打开时崩溃(可能是我的Java代码中的错误)