Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Error inflating SimonVT NumberPicker class in my layout xml

I've been at this for days now, and I am at the point of giving up, so any help is much appreciated!

I've been trying to implement the simonVT numberpicker in my android app. Completely new to android, so including the library, referencing this library and getting everything to compile has been a few days mission in itself. Now I finally have everything compiling I get the following error at runtime:

04-06 10:58:37.126: E/AndroidRuntime(14324): java.lang.RuntimeException: 
Unable to start activity ComponentInfo{com.example.goalminder/com.example.goalminder.AddGoal}:
android.view.InflateException: Binary XML file line #81: 
Error inflating class net.simonvt.numberpicker.NumberPicker

Here is the opening of my layout:

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/net.simonvt.numberpicker"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

NB - The 'xmlns:app' part above has a yellow warning marker - it's not being used. I included this per another stackoverflow answer re. a similar problem. Have left in to discourage this suggestion.

Here is the xml for the numberpicker:

<net.simonvt.numberpicker.NumberPicker          
    android:id="@+id/dayPicker"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:layout_marginRight="10dp"
    android:layout_weight="1"/>

I have included the theme as instructed by Simon in my theme file. I wasn't really sure what name to give it, so I called it 'NumberPicker':

<resources>

    <!-- Copy one of these attributes to your own theme (choose either dark or light).
        <item name="numberPickerStyle">@style/NPWidget.Holo.NumberPicker</item>
       <item name="numberPickerStyle">@style/NPWidget.Holo.Light.NumberPicker</item>
    -->
    <style name="NumberPicker" parent="android:Theme">
        <item name="numberPickerStyle">@style/NPWidget.Holo.NumberPicker</item>
    </style>

    <style name="NumberPicker" parent="android:Theme.Light">
        <item name="numberPickerStyle">@style/NPWidget.Holo.Light.NumberPicker</item>
    </style>

</resources>

I have also added the following to my android manifest as a child of application:

 <activity 
     android:name="net.simonvt.numberpicker.Numberpicker" />
 <activity 
     android:name="net.simonvt.numberpicker.Scroller" />

I've been all over stackoverflow, so what we have above is a scatter gun approach of everything I have seen recommended so. As stated before, I'm floundering with this and am close to implementing a standard ugly list.

NB - I got all this working with the native android implementation of Numberpicker. I want to use Simon VT's backport version however as I will be looking to support API < 11, which includes Gingerbread which I believe has a 39.7% distribution. Please let me know if you think I don't need to support this far back.

like image 596
Ste77 Avatar asked Apr 06 '13 10:04

Ste77


2 Answers

you need add theme for the activity on AndroidManifest.xml: Example:

<activity android:name="yourActivity" android:theme="@style/SampleTheme.Light"/>
like image 70
duongvanthai Avatar answered Oct 18 '22 13:10

duongvanthai


If you don't want to create a theme for your own project, you may do the following to the source code of numberpicker to set it to use the default theme NPWidget_Holo_numberPicker.

Replace the constructor with the following

public NumberPicker(Context context, AttributeSet attrs) {
    this(context, attrs, R.style.NPWidget_Holo_NumberPicker);
}

then change the assignment of TypedArray attributesArray to the following:

    TypedArray attributesArray = context.obtainStyledAttributes(
            attrs, R.styleable.NumberPicker, 0, defStyle);
like image 40
Pui Ho Lam Avatar answered Oct 18 '22 15:10

Pui Ho Lam