I have an inner class to create and show a simple list dialog.
private static class DisplayListDialogFragment extends DialogFragment
{
List<String> list;
OnClickListener clickListener;
private static DisplayListDialogFragment newInstance(List<String> list, String title, OnClickListener clickListener)
{
DisplayListDialogFragment dldf = new DisplayListDialogFragment();
dldf.list = (List<String>)list;
dldf.clickListener = clickListener;
Bundle bundle = new Bundle();
bundle.putString("title", title);
dldf.setArguments(bundle);
return dldf;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
ArrayAdapter<String> ad = new ArrayAdapter<String>(getActivity(), R.id.simpleListItem, this.list);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string._select_display)
.setSingleChoiceItems(ad, 1, this.clickListener);
return builder.create();
}
}
The resources referenced are declared in the strings file
<string name="_select_display">Select Display</string>
and a small file called simple_list.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/simpleListItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
The app blows up somewhere in the depths of the resource inflater at the line
return builder.create();
Stack trace is
11-12 17:21:01.664: E/AndroidRuntime(1335): android.content.res.Resources$NotFoundException: Resource ID #0x7f040052 type #0x12 is not valid
11-12 17:21:01.664: E/AndroidRuntime(1335): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1874)
11-12 17:21:01.664: E/AndroidRuntime(1335): at android.content.res.Resources.getLayout(Resources.java:731)
11-12 17:21:01.664: E/AndroidRuntime(1335): at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
11-12 17:21:01.664: E/AndroidRuntime(1335): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:332)
11-12 17:21:01.664: E/AndroidRuntime(1335): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
Resource ID is defined in R.java as
public static final int simpleListItem=0x7f040052
(i.e. the TextView declared in the simple_list.xml file above)
Can anybody see what’s wrong? I’m scratching my head at the moment.
Thanks
Andrew
Have a look at the documentation for the specific
ArrayAdapterconstructor you’re calling:In particular, it explains the second parameter parameter:
In other words, you’re currently passing in a wrong ID type (that’s what the LogCat error says too). Rather than supplying a
TextViewID within a layout file, you should supply the ID of the layout file. Think about it: how would Android know where to inflate yourTextViewfrom if you don’t mention the layout?Change creating the
ArrayAdapterto:Alternatively, pass in both the layout and
TextViewID:PS: I do agree that the documentation may lead to some confusion as the meaning of the
textViewResourceIdparameter differs with the various constructors.