在 android 2.2 上创建自定义评级栏时无法解析 xml
我需要带有自定义图像的只读评级栏。我尝试了几种方法来做到这一点: Android 评级栏更改星形颜色 和 如何在 Android 中创建自定义评分栏。 这两种情况都存在 xml 解析问题。
我想这可能是因为我的android版本 - 2.2。有什么建议吗?
logcat 的异常:
02-25 15:16:51.713: E/AndroidRuntime(309): FATAL EXCEPTION: main 02-25 15:16:51.713: E/AndroidRuntime(309): android.view.InflateException: Binary XML file line #70: Error inflating class android.widget.RatingBar 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.LayoutInflater.createView(LayoutInflater.java:513) 02-25 15:16:51.713: E/AndroidRuntime(309): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.LayoutInflater.inflate(LayoutInflater.java:407) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 02-25 15:16:51.713: E/AndroidRuntime(309): at kz.mobidev.kinozavr.MovieListAdapter.getView(MovieListAdapter.java:40) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.AbsListView.obtainView(AbsListView.java:1315) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.ListView.makeAndAddView(ListView.java:1727) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.ListView.fillDown(ListView.java:652) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.ListView.fillFromTop(ListView.java:709) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.ListView.layoutChildren(ListView.java:1580) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.AbsListView.onLayout(AbsListView.java:1147) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.View.layout(View.java:7035) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.View.layout(View.java:7035) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.View.layout(View.java:7035) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.ViewRoot.performTraversals(ViewRoot.java:1045) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.os.Handler.dispatchMessage(Handler.java:99) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.os.Looper.loop(Looper.java:123) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.app.ActivityThread.main(ActivityThread.java:4627) 02-25 15:16:51.713: E/AndroidRuntime(309): at java.lang.reflect.Method.invokeNative(Native Method) 02-25 15:16:51.713: E/AndroidRuntime(309): at java.lang.reflect.Method.invoke(Method.java:521) 02-25 15:16:51.713: E/AndroidRuntime(309): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 02-25 15:16:51.713: E/AndroidRuntime(309): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 02-25 15:16:51.713: E/AndroidRuntime(309): at dalvik.system.NativeStart.main(Native Method) 02-25 15:16:51.713: E/AndroidRuntime(309): Caused by: java.lang.reflect.InvocationTargetException 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.RatingBar.(RatingBar.java:114) 02-25 15:16:51.713: E/AndroidRuntime(309): at java.lang.reflect.Constructor.constructNative(Native Method) 02-25 15:16:51.713: E/AndroidRuntime(309): at java.lang.reflect.Constructor.newInstance(Constructor.java:446) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.LayoutInflater.createView(LayoutInflater.java:500) 02-25 15:16:51.713: E/AndroidRuntime(309): ... 32 more 02-25 15:16:51.713: E/AndroidRuntime(309): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ratingbar_red.xml from drawable resource ID #0x7f02001f 02-25 15:16:51.713: E/AndroidRuntime(309): at android.content.res.Resources.loadDrawable(Resources.java:1697) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.content.res.TypedArray.getDrawable(TypedArray.java:601) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.ProgressBar.(ProgressBar.java:178) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.AbsSeekBar.(AbsSeekBar.java:61) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.RatingBar.(RatingBar.java:84) 02-25 15:16:51.713: E/AndroidRuntime(309): ... 36 more 02-25 15:16:51.713: E/AndroidRuntime(309): Caused by: java.lang.NullPointerException 02-25 15:16:51.713: E/AndroidRuntime(309): at android.graphics.drawable.LayerDrawable.addLayer(LayerDrawable.java:185) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.graphics.drawable.LayerDrawable.inflate(LayerDrawable.java:156) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:788) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:729) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.content.res.Resources.loadDrawable(Resources.java:1694) 02-25 15:16:51.713: E/AndroidRuntime(309): ... 40 more
我从 Android RatingBar 更改星形颜色 中获取了以下代码: ratingbar_red.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" android:drawable="@drawable/star_white" />
<item android:id="@android:id/secondaryProgress" android:drawable="@drawable/star_green" />
<item android:id="@android:id/progress" android:drawable="@drawable/star_green" />
</layer-list>
并将此可绘制的 ratingbar_red.xml 分配给我的评级栏:
<RatingBar android:id="@+id/rbRating"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:progressDrawable="@drawable/ratingbar_red" />
I need read-only Rating Bar with custom images. I tried several methods to do this:
Android RatingBar change star colors
and
How to create Custom Ratings bar in Android.
In both cases there's an xml parsing problem.
I think it might be because of my android version - 2.2. Any suggestions?
Exception from logcat:
02-25 15:16:51.713: E/AndroidRuntime(309): FATAL EXCEPTION: main 02-25 15:16:51.713: E/AndroidRuntime(309): android.view.InflateException: Binary XML file line #70: Error inflating class android.widget.RatingBar 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.LayoutInflater.createView(LayoutInflater.java:513) 02-25 15:16:51.713: E/AndroidRuntime(309): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.LayoutInflater.inflate(LayoutInflater.java:407) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 02-25 15:16:51.713: E/AndroidRuntime(309): at kz.mobidev.kinozavr.MovieListAdapter.getView(MovieListAdapter.java:40) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.AbsListView.obtainView(AbsListView.java:1315) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.ListView.makeAndAddView(ListView.java:1727) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.ListView.fillDown(ListView.java:652) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.ListView.fillFromTop(ListView.java:709) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.ListView.layoutChildren(ListView.java:1580) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.AbsListView.onLayout(AbsListView.java:1147) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.View.layout(View.java:7035) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.View.layout(View.java:7035) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.View.layout(View.java:7035) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.ViewRoot.performTraversals(ViewRoot.java:1045) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.os.Handler.dispatchMessage(Handler.java:99) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.os.Looper.loop(Looper.java:123) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.app.ActivityThread.main(ActivityThread.java:4627) 02-25 15:16:51.713: E/AndroidRuntime(309): at java.lang.reflect.Method.invokeNative(Native Method) 02-25 15:16:51.713: E/AndroidRuntime(309): at java.lang.reflect.Method.invoke(Method.java:521) 02-25 15:16:51.713: E/AndroidRuntime(309): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 02-25 15:16:51.713: E/AndroidRuntime(309): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 02-25 15:16:51.713: E/AndroidRuntime(309): at dalvik.system.NativeStart.main(Native Method) 02-25 15:16:51.713: E/AndroidRuntime(309): Caused by: java.lang.reflect.InvocationTargetException 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.RatingBar.(RatingBar.java:114) 02-25 15:16:51.713: E/AndroidRuntime(309): at java.lang.reflect.Constructor.constructNative(Native Method) 02-25 15:16:51.713: E/AndroidRuntime(309): at java.lang.reflect.Constructor.newInstance(Constructor.java:446) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.view.LayoutInflater.createView(LayoutInflater.java:500) 02-25 15:16:51.713: E/AndroidRuntime(309): ... 32 more 02-25 15:16:51.713: E/AndroidRuntime(309): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ratingbar_red.xml from drawable resource ID #0x7f02001f 02-25 15:16:51.713: E/AndroidRuntime(309): at android.content.res.Resources.loadDrawable(Resources.java:1697) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.content.res.TypedArray.getDrawable(TypedArray.java:601) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.ProgressBar.(ProgressBar.java:178) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.AbsSeekBar.(AbsSeekBar.java:61) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.widget.RatingBar.(RatingBar.java:84) 02-25 15:16:51.713: E/AndroidRuntime(309): ... 36 more 02-25 15:16:51.713: E/AndroidRuntime(309): Caused by: java.lang.NullPointerException 02-25 15:16:51.713: E/AndroidRuntime(309): at android.graphics.drawable.LayerDrawable.addLayer(LayerDrawable.java:185) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.graphics.drawable.LayerDrawable.inflate(LayerDrawable.java:156) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:788) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:729) 02-25 15:16:51.713: E/AndroidRuntime(309): at android.content.res.Resources.loadDrawable(Resources.java:1694) 02-25 15:16:51.713: E/AndroidRuntime(309): ... 40 more
I took this code from Android RatingBar change star colors:
ratingbar_red.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" android:drawable="@drawable/star_white" />
<item android:id="@android:id/secondaryProgress" android:drawable="@drawable/star_green" />
<item android:id="@android:id/progress" android:drawable="@drawable/star_green" />
</layer-list>
and assigned this drawable ratingbar_red.xml to my ratingbar:
<RatingBar android:id="@+id/rbRating"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:progressDrawable="@drawable/ratingbar_red" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的 retingbar.xml 在 res/drawable 文件夹中吗?检查一下。
Is your retingbar.xml in res/drawable folder? Check it.