1

我有带有自定义适配器的 ExpandableListView,在这个列表的 groupview 中,我使用动态属性作为它的 imagebutton src drawable,以这种风格声明:

g_view.xml

... android:src="?attr/img_add" ...

attrs.xml

attr name="img_add" format="reference" />

主题.xml

@drawable/ic_action_add_fdark

充气代码:*

LayoutInflater inflater = (LayoutInflater)
 mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                     convertView = inflater.inflate(R.layout.g_view, null);

当我在 g_view.xml 中使用src ="@drawable/img_add"时- 一切正常,但我需要使用动态 attr,但它根本不起作用......

日志:

 01-25 00:16:35.662  14223-14223/xxx E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: xxx, PID: 14223
        android.view.InflateException: Binary XML file line #19: Error inflating class android.widget.ImageButton
                at android.view.LayoutInflater.createView(LayoutInflater.java:620)
                at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
                at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
                at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
                at xxxx.ActSignsManager$_sManagerExpAdapter.getGroupView(ActSignsManager.java:345)
                at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:446)
                at android.widget.AbsListView.obtainView(AbsListView.java:2263)
                at android.widget.ListView.makeAndAddView(ListView.java:1790)
                at android.widget.ListView.fillDown(ListView.java:691)
                at android.widget.ListView.fillFromTop(ListView.java:752)
                at android.widget.ListView.layoutChildren(ListView.java:1616)
                at android.widget.AbsListView.onLayout(AbsListView.java:2091)
                at android.view.View.layout(View.java:14948)
                at android.view.ViewGroup.layout(ViewGroup.java:4631)
                at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
                at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
                at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
                at android.view.View.layout(View.java:14948)
                at android.view.ViewGroup.layout(ViewGroup.java:4631)
                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                at android.view.View.layout(View.java:14948)
                at android.view.ViewGroup.layout(ViewGroup.java:4631)
                at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
                at android.view.View.layout(View.java:14948)
                at android.view.ViewGroup.layout(ViewGroup.java:4631)
                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                at android.view.View.layout(View.java:14948)
                at android.view.ViewGroup.layout(ViewGroup.java:4631)
                at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1991)
                at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1748)
                at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
                at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5692)
                at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
                at android.view.Choreographer.doCallbacks(Choreographer.java:574)
                at android.view.Choreographer.doFrame(Choreographer.java:544)
                at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
                at android.os.Handler.handleCallback(Handler.java:733)
                at android.os.Handler.dispatchMessage(Handler.java:95)
                at android.os.Looper.loop(Looper.java:212)
                at android.app.ActivityThread.main(ActivityThread.java:5151)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
                at dalvik.system.NativeStart.main(Native Method)
         Caused by: java.lang.reflect.InvocationTargetException
                at java.lang.reflect.Constructor.constructNative(Native Method)
                at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
                at android.view.LayoutInflater.createView(LayoutInflater.java:594)
                at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
                at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
                at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
                at xxx s.ActSignsManager$_sManagerExpAdapter.getGroupView(ActSignsManager.java:345)
                at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:446)
                at android.widget.AbsListView.obtainView(AbsListView.java:2263)
                at android.widget.ListView.makeAndAddView(ListView.java:1790)
                at android.widget.ListView.fillDown(ListView.java:691)
                at android.widget.ListView.fillFromTop(ListView.java:752)
                at android.widget.ListView.layoutChildren(ListView.java:1616)
                at android.widget.AbsListView.onLayout(AbsListView.java:2091)
                at android.view.View.layout(View.java:14948)
                at android.view.ViewGroup.layout(ViewGroup.java:4631)
                at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
                at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
                at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
                at android.view.View.layout(View.java:14948)
                at android.view.ViewGroup.layout(ViewGroup.java:4631)
                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                at android.view.View.layout(View.java:14948)
                at android.view.ViewGroup.layout(ViewGroup.java:4631)
                at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
                at android.view.View.layout(View.java:14948)
                at android.view.ViewGroup.layout(ViewGroup.java:4631)
                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                at android.view.View.layout(View.java:14948)
                at android.view.ViewGroup.layout(ViewGroup.java:4631)
                at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1991)
                at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1748)
                at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
                at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5692)
                at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
                at android.view.Choreographer.doCallbacks(Choreographer.java:574)
                at android.view.Choreographer.doFrame(Choreographer.java:544)
                at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
                at android.os.Handler.handleCallback(Handler.java:733)
                at android.os.Handler.dispatchMessage(Handler.java:95)
                at android.os.Looper.loop(Looper.java:212)
                at android.app.ActivityThread.main(ActivityThread.java:5151)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
                at dalvik.system.NativeStart.main(Native Method)
         Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f01002d a=-1}
                at android.content.res.Resources.loadDrawable(Resources.java:2081)
                at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
                at android.widget.ImageView.<init>(ImageView.java:129)
                at android

PS是我检查过的,没有任何帮助..

4

1 回答 1

2

经过三天的分析和失败,当希望几乎失去时..我修复了它!

我的适配器初始化代码是:

 adapter = new _sManagerExpAdapter(getApplicationContext(), cats, groups);

它必须是:

  adapter = new _sManagerExpAdapter(this, cats, groups);

现在,一切正常,感谢Patrick Arminio

于 2015-01-25T08:55:12.753 回答