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

Ser*_*gey 5 resources android

如果我得到的错误"android.content.res.Resources $ NotFoundException:资源ID#0x7f050007型#0×12是无效的"我能找到一些这方面的资源是什么,如果我知道它的ID?

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

messages_list.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>
Run Code Online (Sandbox Code Playgroud)

Ore*_*ren 5

我在片段中使用ListView时出现此错误.

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

所以你得到:

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


Shi*_*pta 5

对于那些其他提到的解决方案不起作用的人.

我做了这个愚蠢的错误: -

setContentView(R.id.something);
Run Code Online (Sandbox Code Playgroud)

代替

setContentView(R.layout.something);
Run Code Online (Sandbox Code Playgroud)

纠正了,错误消失了:D


Fin*_*sen 3

您可以使用 eclipse 中的搜索功能,搜索"0x7f050007"或转到projectfolder/gen/path/R.java包含您的资源。

你会发现类似这样的东西:

public static final int lineItem=0x7f07001c;
Run Code Online (Sandbox Code Playgroud)

然后使用 Eclipse 的搜索功能进行搜索(在本例中)lineItem。它将带您到代码中的资源。