我得到一个空指针异常,这是它放置的代码。
final TextView view = (TextView) findViewById(R.id.textView2);
final Button next = (Button) findViewById(R.id.button1);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
view.setText("These questions will help me to help you :)");
}
});
这是我的日志文件:
24 15:21:45.441: E/AndroidRuntime(376): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.icemountaininc.app/com.icemountaininc.app.MainActivity}: java.lang.NullPointerException
11-24 15:21:45.441: E/AndroidRuntime(376): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1660)
11-24 15:21:45.441: E/AndroidRuntime(376): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
11-24 15:21:45.441: E/AndroidRuntime(376): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
11-24 15:21:45.441: E/AndroidRuntime(376): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
11-24 15:21:45.441: E/AndroidRuntime(376): at android.os.Handler.dispatchMessage(Handler.java:99)
11-24 15:21:45.441: E/AndroidRuntime(376): at android.os.Looper.loop(Looper.java:126)
11-24 15:21:45.441: E/AndroidRuntime(376): at android.app.ActivityThread.main(ActivityThread.java:3997)
11-24 15:21:45.441: E/AndroidRuntime(376): at java.lang.reflect.Method.invokeNative(Native Method)
11-24 15:21:45.441: E/AndroidRuntime(376): at java.lang.reflect.Method.invoke(Method.java:491)
11-24 15:21:45.441: E/AndroidRuntime(376): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
11-24 15:21:45.441: E/AndroidRuntime(376): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
11-24 15:21:45.441: E/AndroidRuntime(376): at dalvik.system.NativeStart.main(Native Method)
11-24 15:21:45.441: E/AndroidRuntime(376): Caused by: java.lang.NullPointerException
11-24 15:21:45.441: E/AndroidRuntime(376): at android.app.Activity.findViewById(Activity.java:1741)
11-24 15:21:45.441: E/AndroidRuntime(376): at com.icemountaininc.app.MainActivity.<init>(MainActivity.java:14)
11-24 15:21:45.441: E/AndroidRuntime(376): at java.lang.Class.newInstanceImpl(Native Method)
11-24 15:21:45.441: E/AndroidRuntime(376): at java.lang.Class.newInstance(Class.java:1424)
11-24 15:21:45.441: E/AndroidRuntime(376): at android.app.Instrumentation.newActivity(Instrumentation.java:1022)
11-24 15:21:45.441: E/AndroidRuntime(376): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
11-24 15:21:45.441: E/AndroidRuntime(376): ... 11 more
请问有什么帮助吗?
被这个问题困了很久!
我使用 Android 的 SDK VERSION 8
谢谢。