gpt4 book ai didi

java - Android 应用程序中 setText 的空对象引用

转载 作者:行者123 更新时间:2023-12-01 06:27:04 26 4
gpt4 key购买 nike

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

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

R.java

public static final int city_result=0x7f080041;

此代码位于 public static final class id 方法中。

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

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

 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]);
}

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

    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.access$800(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.access$800(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)

最佳答案

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

表示 city TextView 对象为 null,原因是:

1. 使用findViewById之前未调用setContentView:

2. 不使用使用 city_result id 定义 TextView 的布局。确保您在 setContentView

中传递正确的布局

关于java - Android 应用程序中 setText 的空对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28248220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com