Java Android 应用中 setText 上的空对象引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28248220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:51:23  来源:igfitidea点击:

Null object reference on setText in Android app

javaandroidtextview

提问by Zip

My app crashes on this setTexthere. I think probably because it cannot find the id so I looked into to R.java and found the id there. I'm confused why it still crashes.

我的应用程序在setText这里崩溃了。我想可能是因为它找不到 id,所以我查看了 R.java 并在那里找到了 id。我很困惑为什么它仍然崩溃。

TextView city = (TextView)findViewById(R.id.city_result);
city.setText(msg[0]);

R.java

R.java

public static final int city_result=0x7f080041;

and this code is in the public static final class idmethod.

并且此代码在public static final class id方法中。

Any help will be appreciated! Thanks!!

任何帮助将不胜感激!谢谢!!

================ More Code:

================ 更多代码:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_weather_report);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

    //get message from intent
    Intent intent = getIntent();
    String[] message = intent.getStringArrayExtra(MainActivity.EXTRA_MESSAGE);
    setResult(message);
}
public void setResult(String[] msg)
    {
        //test
        TextView city = (TextView)findViewById(R.id.city_result);
        city.setText(msg[0]);
    }

This file I just started, elsewhere is just system generated new file code, I didn't edit yet. Here's logcat:

这个文件我刚开始,其他地方只是系统生成的新文件代码,我还没有编辑。这是logcat:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chen.weather/com.example.chen.weather.WeatherReport}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access0(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
            at com.example.chen.weather.WeatherReport.setResult(WeatherReport.java:78)
            at com.example.chen.weather.WeatherReport.onCreate(WeatherReport.java:32)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
    ????????????at android.app.ActivityThread.access0(ActivityThread.java:144)
    ????????????at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
    ????????????at android.os.Handler.dispatchMessage(Handler.java:102)
    ????????????at android.os.Looper.loop(Looper.java:135)
    ????????????at android.app.ActivityThread.main(ActivityThread.java:5221)
    ????????????at java.lang.reflect.Method.invoke(Native Method)
    ????????????at java.lang.reflect.Method.invoke(Method.java:372)
    ????????????at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
    ????????????at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

回答by ρяσ?ρ?я K

NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText

NullPointerException: 尝试调用虚方法 'void android.widget.TextView.setText

Means cityTextView object is nulldue to :

意味着cityTextView 对象是null由于:

1.setContentViewis not called before using findViewById:

1.setContentView使用前不调用findViewById

2.Not using layout in which TextViewis defined with city_resultid. make sure you are passing right layout in setContentView

2.不使用idTextView定义的布局city_result。确保你传递了正确的布局setContentView

回答by thuongnh.uit

Which layout did you put the TextView? I doubt that in this case, you put the TextView in PlaceHolder fragment layout (not the activity_weather_report.xmllayout). If so, putting the TextView in activity_weather_report.xmlwill solve your problem.

你把 TextView 放在哪个布局?我怀疑在这种情况下,您将 TextView 放在 PlaceHolder 片段布局(不是activity_weather_report.xml布局)中。如果是这样,将 TextView 放入activity_weather_report.xml将解决您的问题。

回答by David Hackro

check that your activity is correct!!

检查您的活动是否正确!!

setContentView(R.layout.activity_weather_report);

的setContentView(R.layout。activity_weather_report);

回答by Faisal Zaheer

Problem is just the id

问题只是 id

Make sure that you are using correct id for TextViewin Layout xmlfile . I had same issue and it resolved by solving spelling mistake .

确保您TextView在布局xml文件中使用了正确的 id 。我有同样的问题,它通过解决拼写错误解决了。

回答by user8398214

it is showing this exception because your textview is not getting data at time of onCeate method. declare your textview in onCreate method, put yourtextview.setText(xyz) wherever you are getting that "xyz" value. Hope this will work.

它显示此异常是因为您的 textview 在 onCate 方法时未获取数据。在 onCreate 方法中声明您的 textview,将 yourtextview.setText(xyz) 放在您获得“xyz”值的任何地方。希望这会奏效。