1

我只是试着做这个教程来创建自定义 Android SeekBar -- >> http://www.mokasocial.com/2011/02/create-a-custom-styled-ui-slider-seekbar-in-android/

看起来我已经完成了所有工作,但是当我尝试运行应用程序时,它会在 LogCat 中启动很多错误,并且我的应用程序停止工作。这是 LogCat 抛出的内容:

09-23 21:21:19.390: I/ApplicationPackageManager(3171): cscCountry is not German : SEB
09-23 21:21:19.460: D/AndroidRuntime(3171): Shutting down VM
09-23 21:21:19.460: W/dalvikvm(3171): threadid=1: thread exiting with uncaught exception (group=0x40018578)
09-23 21:21:19.484: E/AndroidRuntime(3171): FATAL EXCEPTION: main
09-23 21:21:19.484: E/AndroidRuntime(3171): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.uisample/com.example.uisample.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class android.widget.SeekBar
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.os.Looper.loop(Looper.java:130)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.app.ActivityThread.main(ActivityThread.java:3687)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at java.lang.reflect.Method.invokeNative(Native Method)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at java.lang.reflect.Method.invoke(Method.java:507)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at dalvik.system.NativeStart.main(Native Method)
09-23 21:21:19.484: E/AndroidRuntime(3171): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class android.widget.SeekBar
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.view.LayoutInflater.createView(LayoutInflater.java:518)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:209)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.app.Activity.setContentView(Activity.java:1657)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at com.example.uisample.MainActivity.onCreate(MainActivity.java:13)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
09-23 21:21:19.484: E/AndroidRuntime(3171):     ... 11 more
09-23 21:21:19.484: E/AndroidRuntime(3171): Caused by: java.lang.reflect.InvocationTargetException
09-23 21:21:19.484: E/AndroidRuntime(3171):     at java.lang.reflect.Constructor.constructNative(Native Method)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.view.LayoutInflater.createView(LayoutInflater.java:505)
09-23 21:21:19.484: E/AndroidRuntime(3171):     ... 22 more
09-23 21:21:19.484: E/AndroidRuntime(3171): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/seekbar_progress.xml from drawable resource ID #0x7f020003
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.content.res.Resources.loadDrawable(Resources.java:1697)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.widget.ProgressBar.<init>(ProgressBar.java:179)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.widget.AbsSeekBar.<init>(AbsSeekBar.java:61)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.widget.SeekBar.<init>(SeekBar.java:81)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.widget.SeekBar.<init>(SeekBar.java:77)
09-23 21:21:19.484: E/AndroidRuntime(3171):     ... 25 more
09-23 21:21:19.484: E/AndroidRuntime(3171): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #5: <item> tag requires a 'drawable' attribute or child tag defining a drawable
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.graphics.drawable.LayerDrawable.inflate(LayerDrawable.java:150)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
09-23 21:21:19.484: E/AndroidRuntime(3171):     at android.content.res.Resources.loadDrawable(Resources.java:1694)
09-23 21:21:19.484: E/AndroidRuntime(3171):     ... 30 more

*seekbar_progress.xml*

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@android:id/background">
        <nine-patch
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:dither="true"
            android:src="@drawable/seekbar_background" />
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <gradient
                    android:angle="270"
                    android:centerColor="#80127fb1"
                    android:centerY="0.75"
                    android:endColor="#a004638f"
                    android:startColor="#80028ac8" />
            </shape>
        </clip>
    </item>
    <item
        android:id="@android:id/progress"
        android:drawable="@drawable/seekbar_progress_bg"/>  

</layer-list>

*seekbar_progress_bg.xml*

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <clip>
            <shape>
                <gradient
                    android:angle="270"
                    android:centerColor="#FF32a0d2"
                    android:centerY="0.1"
                    android:endColor="#FF13729e"
                    android:startColor="#FF5e8ea3" />
            </shape>
        </clip>
    </item>
    <item>
        <clip>
            <bitmap
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:antialias="true"
                android:dither="false"
                android:filter="false"
                android:gravity="left"
                android:src="@drawable/stripe_bg"
                android:tileMode="repeat" />
        </clip>
    </item>

</layer-list>

*activity_main.xml*

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="185dp"
        android:progressDrawable="@drawable/seekbar_progress"
        android:thumb="@drawable/seek_thumb_normal" />

</RelativeLayout>

我知道只有在stackoverflow中有很多带有这个错误的主题,但我还没有找到解决方案,所以希望有人知道解决方案并可以帮助我。欣赏。

4

1 回答 1

1

你得到一个 XmlPullParserException,这是因为你的 xml 试图获取一个包含九个补丁的图像,但实际上你没有一个。所以改为使用以下 seekbar_progress.xml:

<item android:id="@android:id/background"
      android:drawable="@drawable/seekbar_background">
</item>
<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
            <gradient
                android:angle="270"
                android:centerColor="#80127fb1"
                android:centerY="0.75"
                android:endColor="#a004638f"
                android:startColor="#80028ac8" />
        </shape>
    </clip>
</item>
<item
    android:id="@android:id/progress"
    android:drawable="@drawable/seekbar_progress_bg"/>  

</layer-list>
于 2013-10-03T08:04:10.927 回答