开发者

Android find resource by id during the runtime

开发者 https://www.devze.com 2023-04-12 17:06 出处:网络
If I get the error \"android.content.res.Resources$NotF开发者_运维知识库oundException: Resource ID #0x7f050007 type #0x12 is not valid\" can I find some what this resource is if I know its ID?

If I get the error "android.content.res.Resources$NotF开发者_运维知识库oundException: Resource ID #0x7f050007 type #0x12 is not valid" can I find some what this resource is if I know its ID?

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

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>


I Got this error when using ListView in a Fragment.

Resolved by moving the setAdapter lines to the onViewCreated function of the Fragment. (makes sense that before the view is created the ListView is invalid).

so you get :

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


For those whom other mentioned solutions don't work.

I did this silly mistake:-

setContentView(R.id.something);

Instead of

setContentView(R.layout.something);

Corrected that, and error was gone :D


You can either use the search function in eclipse, search for "0x7f050007" or go to projectfolder/gen/path/R.java that contains your resources.

You'll find something like this:

public static final int lineItem=0x7f07001c;

Then search for(in this example) lineItem with Eclipses search function. It'll take you to your resource in code.


Check your imports (at the top of your class-file). Maybe you imported

android.R

(which provides access to the platform-resources) instead of

{your_package_name}.R

(you can also leave it blank).

0

精彩评论

暂无评论...
验证码 换一张
取 消