gpt4 book ai didi

Android 在运行时通过 id 查找资源

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:03:35 25 4
gpt4 key购买 nike

如果我收到错误“android.content.res.Resources$NotFoundException: Resource ID #0x7f050007 type #0x12 is not valid”,如果我知道它的 ID,我能找到这个资源是什么吗?

        ListView list = (ListView)findViewById(R.id.messages_list_view);
list.setAdapter(new ArrayAdapter<String>(context,
R.layout.messages_list, headers));

消息列表.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/messages_list_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:id="@+id/messages_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

最佳答案

我在 Fragment 中使用 ListView 时遇到此错误。

通过将 setAdapter 行移动到 Fragment 的 onViewCreated 函数解决。 (有意义的是,在创建 View 之前,ListView 是无效的)。

所以你得到:

public void onViewCreated(View view,Bundle savedInstanceState){
ListView list = (ListView) getView().findViewById(R.id.thelist);
list.setAdapter(mAdapter);
}

关于Android 在运行时通过 id 查找资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7722907/

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