1

我得到一个空指针异常,这是它放置的代码。

    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

谢谢。

4

3 回答 3

3

实际上你得到了NullPointerException因为你TextView viewButton nextNULL

解决方案:

您必须在Activity中定义TextView viewButton nextaftersetContentView()方法。onCreate()

像:

setContentView(R.layout.activity_main);
final TextView view = (TextView) findViewById(R.id.textView2);
final Button next = (Button) findViewById(R.id.button1);
于 2012-11-24T15:30:43.640 回答
1

试试这个

final TextView view;
final Button next;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    view = (TextView) findViewById(R.id.textView2);
    next = (Button) findViewById(R.id.button1);
    next.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            view.setText("These questions will help me to help you :)");
        }
    });

在您的代码中

final TextView view = (TextView) findViewById(R.id.textView2);
final Button next = (Button) findViewById(R.id.button1);

之前被调用

setContentView(R.layout.activity_main);

这就是为什么意见没有得到解决

于 2012-11-24T15:30:25.147 回答
0

你得到空指针异常是因为你在 setContentView() 之前找到了按钮和 textViews。在了解布局之前,无法识别按钮和 textView。你的代码应该像 -

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

  final TextView view = (TextView) findViewById(R.id.textView2);
   final Button next = (Button) findViewById(R.id.button1);
   next.setOnClickListener(new OnClickListener() 
 {

    public void onClick(View v) {
        view.setText("These questions will help me to help you :)");


    }
});

还有一个建议是不要对 UI 控件使用 final 修饰符。如果您遇到必须在每次按钮单击时更新视图的情况,则最终变量不会在每个按钮单击实例上被修改。

于 2012-11-24T16:42:40.530 回答