質問

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.

役に立ちましたか?

解決

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

<activity android:name="yourActivity" android:theme="@style/SampleTheme.Light"/>

他のヒント

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);

See Simon's usage notes:

Requires adding a single attribute to your theme. Check the sample app for how this is done.

values/theme.xml:

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

values-v11/themes.xml:

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

Try replacing net.simonvt.numberpicker.NumberPicker with com.your.package.NumberPicker.

I was having practically the same problem, I was getting the error

11-18 21:13:18.627: W/ResourceType(13799): No package identifier when getting value for resource number 0x00000000

I finally realised that I had to add the style item into my own style definitions (as Paul Lammertsma shows above) as I was just copy/pasting SimonVT's styles, which of course my application wasn't using:

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

Then, after it still not working, I found I'd completely missed a themes.xml file (I have three for different API levels).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top