gpt4 book ai didi

java - 在android中的 View 上使用append方法出现NullPointerException?

转载 作者:行者123 更新时间:2023-12-01 19:13:49 27 4
gpt4 key购买 nike

我正在尝试使用以下代码将数据从数据库填充到 TextView 中:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView view = (TextView) findViewById(R.id.textView);

db = (new DatabaseHelper(this)).getWritableDatabase();
cursor = getCursor();
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
String text = cursor.getString(2);
view.append(text);
cursor.moveToNext();
}

但是我在这一行收到 NullPointerException:

 view.append(text);

我不确定 View 存在的原因。

最佳答案

However I am getting a NullPointerException on this line:

view.append(text); and I am not sure about the reason as the view exists.

如果您确定 view 存在且不为 null,则 text 必须为 null。例如,您可以将其重写为 view.append(text != null ? text : "(null)");

关于java - 在android中的 View 上使用append方法出现NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7543560/

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