当我在eclipse的模拟器中启动我的活动时,遗憾的是我已经停止了应用程序的消息。希望知道问题出在哪里,我不明白LogCat我的logCat说:
06-10 21:13:06.769: I/Process(1158): Sending signal. PID: 1158 SIG: 9
06-10 21:13:21.609: D/AndroidRuntime(1197): Shutting down VM
06-10 21:13:21.609: W/dalvikvm(1197): threadid=1: thread exiting with uncaught exception (group=0xb4a96ba8)
06-10 21:13:21.679: E/AndroidRuntime(1197): FATAL EXCEPTION: main
06-10 21:13:21.679: E/AndroidRuntime(1197): Process: com.example.holamundo1, PID: 1197
06-10 21:13:21.679: E/AndroidRuntime(1197): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.holamundo1/com.example.holamundo1.MainActivity}: java.lang.NullPointerException
06-10 21:13:21.679: E/AndroidRuntime(1197): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-10 21:13:21.679: E/AndroidRuntime(1197): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-10 21:13:21.679: E/AndroidRuntime(1197): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-10 21:13:21.679: E/AndroidRuntime(1197): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-10 21:13:21.679: E/AndroidRuntime(1197): at android.os.Handler.dispatchMessage(Handler.java:102)
06-10 21:13:21.679: E/AndroidRuntime(1197): at android.os.Looper.loop(Looper.java:136)
06-10 21:13:21.679: E/AndroidRuntime(1197): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-10 21:13:21.679: E/AndroidRuntime(1197): at java.lang.reflect.Method.invokeNative(Native Method)
06-10 21:13:21.679: E/AndroidRuntime(1197): at java.lang.reflect.Method.invoke(Method.java:515)
06-10 21:13:21.679: E/AndroidRuntime(1197): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-10 21:13:21.679: E/AndroidRuntime(1197): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-10 21:13:21.679: E/AndroidRuntime(1197): at dalvik.system.NativeStart.main(Native Method)
06-10 21:13:21.679: E/AndroidRuntime(1197): Caused by: java.lang.NullPointerException
06-10 21:13:21.679: E/AndroidRuntime(1197): at com.example.holamundo1.MainActivity.onCreate(MainActivity.java:15)
06-10 21:13:21.679: E/AndroidRuntime(1197): at android.app.Activity.performCreate(Activity.java:5231)
06-10 21:13:21.679: E/AndroidRuntime(1197): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-10 21:13:21.679: E/AndroidRuntime(1197): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-10 21:13:21.679: E/AndroidRuntime(1197): ... 11 more
06-10 21:13:25.819: I/Process(1197): Sending signal. PID: 1197 SIG: 9
代码是:
package com.example.holamundo1;
import android.os.Bundle;
import android.app.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button miBoton = (Button)findViewById(R.id.button);
miBoton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("Botón presionado!");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:0)
首先检查您的Button
是否已导入import android.widget.Button;
检查Button
的第二件事是activity_main.xml
文件,其ID为R.id.button
答案 1 :(得分:0)
activity_main.xml
应该有一个ID为“button
”
miBoton
null ,因为button
activity_main.xml
”的小部件
重命名 xml中定义的按钮ID为“button
”
或者
使用xml 中定义的按钮ID,比如它是否在xml中定义为button1,然后在您的代码中将其用作R.id.button1
答案 2 :(得分:0)
在activity_main.xml文件中定义按钮ID。