无法使用动态attr drawable来扩充视图

时间:2015-01-24 21:34:40

标签: android expandablelistview dynamic-attributes

我有自定义适配器的ExpandableListView,在这个列表的groupview中我使用动态attrs为它的imagebutton src drawable,以这样的样式声明:

g_view.xml

... 机器人:?SRC =" ATTR / img_add" ...

attrs.xml

attr name =" img_add"格式="参考" />

的themes.xml

@绘制/ 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

P.S。 This我查了一下,没有任何帮助......

1 个答案:

答案 0 :(得分:1)

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

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

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

它必须是:

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

现在,由于Patrick Arminio

,一切正常