在onCreate()中更改文本时出现Nullpointer异常

时间:2012-11-24 15:28:00

标签: java android nullpointerexception

我得到一个nullpointer异常,这是它的铺设代码。

    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

感谢。

3 个答案:

答案 0 :(得分:3)

实际上你有 NullPointerException ,因为你的TextView viewButton nextNULL

<强>解决方案:

您必须在活动TextView view的{​​{1}}方法后定义Button nextsetContentView()

像:

onCreate()

答案 1 :(得分: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);

这就是为什么视图无法解决

答案 2 :(得分:0)

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

@覆盖      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修饰符。如果您有必须更新每个按钮上的视图的情况,请单击每个按钮单击实例上的最终变量。