package com.ex06.button;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ActivityButton extends Activity {
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setTitle("button1 点击后不可见");
button1.setVisibility(View.INVISIBLE);
button2.setVisibility(View.VISIBLE);
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setTitle("button2 点击后不可见");
button2.setVisibility(View.INVISIBLE);
button1.setVisibility(View.VISIBLE);
}
});
}
}
我建两个button,为什么会出现这种情况
logcat
12-02 20:57:40.865: E/AndroidRuntime(1658): FATAL EXCEPTION: main
12-02 20:57:40.865: E/AndroidRuntime(1658): Process: com.ex06.button, PID: 1658
12-02 20:57:40.865: E/AndroidRuntime(1658): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ex06.button/com.ex06.button.ActivityButton}: java.lang.NullPointerException
12-02 20:57:40.865: E/AndroidRuntime(1658): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)
12-02 20:57:40.865: E/AndroidRuntime(1658): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
12-02 20:57:40.865: E/AndroidRuntime(1658): at android.app.ActivityThread.access$700(ActivityThread.java:135)
12-02 20:57:40.865: E/AndroidRuntime(1658): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
12-02 20:57:40.865: E/AndroidRuntime(1658): at android.os.Handler.dispatchMessage(Handler.java:102)
12-02 20:57:40.865: E/AndroidRuntime(1658): at android.os.Looper.loop(Looper.java:137)
12-02 20:57:40.865: E/AndroidRuntime(1658): at android.app.ActivityThread.main(ActivityThread.java:4998)
12-02 20:57:40.865: E/AndroidRuntime(1658): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 20:57:40.865: E/AndroidRuntime(1658): at java.lang.reflect.Method.invoke(Method.java:515)
12-02 20:57:40.865: E/AndroidRuntime(1658): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-02 20:57:40.865: E/AndroidRuntime(1658): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-02 20:57:40.865: E/AndroidRuntime(1658): at dalvik.system.NativeStart.main(Native Method)
12-02 20:57:40.865: E/AndroidRuntime(1658): Caused by: java.lang.NullPointerException
12-02 20:57:40.865: E/AndroidRuntime(1658): at android.app.Activity.findViewById(Activity.java:1883)
12-02 20:57:40.865: E/AndroidRuntime(1658): at com.ex06.button.ActivityButton.<init>(ActivityButton.java:11)
12-02 20:57:40.865: E/AndroidRuntime(1658): at java.lang.Class.newInstanceImpl(Native Method)
12-02 20:57:40.865: E/AndroidRuntime(1658): at java.lang.Class.newInstance(Class.java:1208)
12-02 20:57:40.865: E/AndroidRuntime(1658): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
12-02 20:57:40.865: E/AndroidRuntime(1658): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
12-02 20:57:40.865: E/AndroidRuntime(1658): ... 11 more
12-02 20:57:45.135: I/Process(1658): Sending signal. PID: 1658 SIG: 9
------解决方案--------------------
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
这两句应该放在oncreate的setcontent之后
------解决方案--------------------
楼上正解!
setContentView()之前, findViewById()都会异常的。 因为View没有初始化。
------解决方案--------------------
button1和button2不能定义的时候就给他初始化值,应该在setContentView之后赋值。
------解决方案--------------------
1
2
3
4
5