Why is my custom layout file not recognized?

两盒软妹~` 提交于 2019-12-11 04:35:58

问题


I created a file in \res\layout named contactlist.xml

But it is not recognized in my code:

SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
        //android.R.layout.simple_list_item_1, mContacts, //if cre8 own layout, replace "simple_[etc]"
        //android.R.layout.simple_list_item_checked, mContacts, // or simple_list_item_multiple_choice
        //android.R.layout.simple_list_item_multiple_choice, mContacts,
        android.R.layout.contactlist, mContacts, // <- contact list ist xml-non-grata
        new String[] { ContactsContract.Contacts.DISPLAY_NAME },
        new int[] { android.R.id.text1 });

I want to create a custom layout that for each Contact has three checkboxes.

Why is my custom layout not accepted as valid?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Updated 2/9/2012:

Finally!

With the help of stackOverflowers and this article: http://www.vogella.de/articles/AndroidListView/article.html

I finally got it working; as usual, it's not that tough once you grok a couple of concepts. It boils down to using this sort of code, which I begged/borrowed/stole and adapted:

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

// Return all contacts, ordered by name
String[] projection = new String[] { ContactsContract.Contacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME }; 
mContacts = managedQuery(ContactsContract.Contacts.CONTENT_URI,
        projection, null, null, ContactsContract.Contacts.DISPLAY_NAME);

// Display all contacts in a ListView
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
        R.layout.ondemandandautomatic_authorize, mContacts,
        new String[] { ContactsContract.Contacts.DISPLAY_NAME },
        new int[] { R.id.contactLabel });

setListAdapter(mAdapter);

}

...and making sure "ondemandandautomatic_authorize" (or whatever you name your layout file) is something like this (unfinished, but you get the idea):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1" />

        <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2" />

        <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3" />

    <TextView
        android:id="@+id/contactLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="(replace this)"
        android:textSize="20px" >
    </TextView>

</LinearLayout>

...and that "R.id.contactLabel" is replaced with "R.id."

There's more to be done, obviously, but a big obstacle has been hurdled.


回答1:


It should be

R.layout.contactlist

and not

android.R.layout.contactlist

android is used when you are using the system resources.




回答2:


use your application generated R file(R.layout.contactlist) rather than using android generated R file(android.R.layout.contactlist).




回答3:


If you are using Eclipse, I think you should try re-start Eclipse. Sometimes, when i add new xml or delete some, my eclipse's not smart enough, i don't know why. There are other ways like Clean Project(But you have to make sure that there is No Error in your XML, otherwise your R will gone forever).

Not quite sure, hope it help




回答4:


I think u missed setContentView(R.Layout.contactlist);...also u need to specify this in your manifest.




回答5:


Sometimes, even when you replace the android.R.layout.zzz with R.layout.zzz, Eclipse will still show you an error that your customer layout is no recognized.

I just faced this same problem now.

The solution for me was to remove that text and write it again. You may also write it gradually by writing "R." then pressing Alt+Space, Eclipse will give you 2 options (android.R and R.), just pick the R. and then continue the rest.



来源:https://stackoverflow.com/questions/9170827/why-is-my-custom-layout-file-not-recognized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!