gpt4 book ai didi

java - NullPointerException:尝试在空对象引用上调用虚拟方法 'void android.widget.TextView.setText(java.lang.CharSequence)'

转载 作者:行者123 更新时间:2023-11-29 03:04:11 26 4
gpt4 key购买 nike

<分区>

所以我有这个代码:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favourites);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

SharedPreferences prefs = getSharedPreferences("myFavs", 0);
Map<String, String> m = (Map<String, String>) prefs.getAll();
List<String> list = new ArrayList<>(m.values());

ListView listView = (ListView) findViewById(R.id.favsList);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,R.layout.listlayout, android.R.id.text1, list );
listView.setAdapter(adapter);

它在 logcat 中输出这个错误:

10-08 08:48:44.114  19390-19390/com.example.me1jmo.insult_generator E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.me1jmo.insult_generator, PID: 19390
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:399)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
at android.widget.AbsListView.obtainView(AbsListView.java:2346)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1280)
at android.widget.ListView.onMeasure(ListView.java:1188)
at android.view.View.measure(View.java:18788)
...

我已经阅读了有关为什么会发生这种情况的文章,但我无法弄清楚我需要更改代码的哪一部分才能使其正常工作。

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