This is the first time I've created a custom view, and I'm trying to populate it with entries via XML - in a way similar to Spinner. I obviously am doing something wrong here, but I developed my approach after Spinner, so I don’t see what went wrong.
To explain the intent of this code, I create a special list of preferences. MultiChooserOption is an item that allows the user to select multiple items from a list of options. TextOption just defines the view that will be displayed in the list, i.e. This is a symbol and another element; for subclasses of TextOption, the other element will be text, not a widget (checkbox, etc.).
Below is the source for everything that should affect how I built it, as well as the output of my logcat.
attrs.xml
<resources> <attr name="caption" format="string" /> <attr name="options" format="reference" /> <declare-styleable name="TextOption"> <attr name="caption" /> <attr name="text" format="string" /> </declare-styleable> <declare-styleable name="MultiChooserOption"> <attr name="options" /> </declare-styleable> </resources>
MultiChooserOption.java
public class MultiChooserOption extends TextOption<CharSequence> { public static final String TAG = MultiChooserOption.class.getSimpleName(); private List<CharSequence> options; private MultiChoiceListView<CharSequence> choiceView; public MultiChooserOption(Context context) { super( context ); } public MultiChooserOption(Context context, AttributeSet attrs) { super( context, attrs ); initOptions( attrs ); } private void initOptions( AttributeSet attrs ) { TypedArray attributesArray = getContext().obtainStyledAttributes( attrs, R.styleable.TextOption, 0, 0 ); try {
TextOption.java
public abstract class TextOption<T> extends LinearLayout { public static final String TAG = TextOption.class.getSimpleName(); private TextView captionTextView; private TextView valueTextView; private String caption; private String value; public TextOption(Context context) { super( context ); init(); } public TextOption(Context context, AttributeSet attrs) { super( context, attrs ); TypedArray attributesArray = context.obtainStyledAttributes( attrs, R.styleable.TextOption, 0, 0 ); try { setCaption( attributesArray.getString( R.styleable.TextOption_caption ) ); setValue( attributesArray.getString( R.styleable.TextOption_text ) ); } finally { attributesArray.recycle(); } init(); } }
options_fragment.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res/com.mypackage" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <com.mypackage.view.MultiChooserOption android:id="@+id/multichooser" android:layout_width="match_parent" android:layout_height="wrap_content" custom:caption="@string/multichooser_caption" custom:options="@array/multichooser_options" /> </LinearLayout> </ScrollView>
arrays.xml
<resources> <string-array name="multichooser_options"> <item>@string/option1</item> <item>@string/option2</item> <item>@string/option3</item> <item>@string/option4</item> <item>@string/option5</item> <item>@string/option6</item> </string-array> </resources>
strings.xml
<resources> <string name="multichooser_caption">MultiChooser</string> <string name="option1">option 1</string> <string name="option2">option 2</string> <string name="option3">option 3</string> <string name="option4">option 4</string> <string name="option5">option 5</string> <string name="option6">option 6</string> </resources>
OptionsFragment.java
public class OptionsFragment extends RoboSherlockFragment { public static final String TAG = OptionsFragment.class.getSimpleName(); @Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) { View view = inflater.inflate( R.layout.options_fragment, container, false ); return view; } }
logcat output
09-18 20:52:36.940: E/AndroidRuntime(4237): FATAL EXCEPTION: main 09-18 20:52:36.940: E/AndroidRuntime(4237): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mypackage/com.mypackage.ui.OptionsActivity}: android.view.InflateException: Binary XML file line
So, when the line CharSequence[] optionsArray = attributesArray.getTextArray( R.styleable.MultiChooserOption_options ); in MultiChooserOption, an exception occurs and the entire shebang crashes. As far as I know, I follow Spinner how important this is.
Please help me find out what I did wrong here.