我正在尝试为checkboxPreferences编写自定义视图。我有:
<CheckBoxPreference
android:key="@string/pref_acceptableads"
android:summaryOff="@string/pref_acceptableads_summary_off"
android:summaryOn="@string/pref_acceptableads_summary_on"
android:title="@string/pref_acceptableads_title"
android:widgetLayout="@layout/helpfulcheckbox_layout" />
和
<?xml version="1.0" encoding="UTF-8"?>
<ViewGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/image" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</ViewGroup>
问题在于我的视图组。我收到一个例外:
03-11 13:49:11.520:E / AndroidRuntime(12286):致命异常:主要 03-11 13:49:11.520:E / AndroidRuntime(12286):流程: org.adblockplus.android,PID:12286 03-11 13:49:11.520: E / AndroidRuntime(12286):android.view.InflateException:二进制XML 文件行#2:错误膨胀类android.view.ViewGroup 03-11 13:49:11.520:E / AndroidRuntime(12286):at android.view.LayoutInflater.createView(LayoutInflater.java:620)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.view.LayoutInflater.onCreateView(LayoutInflater.java:652) 03-11 13:49:11.520:E / AndroidRuntime(12286):at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66) 03-11 13:49:11.520:E / AndroidRuntime(12286):at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669) 03-11 13:49:11.520:E / AndroidRuntime(12286):at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694) 03-11 13:49:11.520:E / AndroidRuntime(12286):at android.view.LayoutInflater.inflate(LayoutInflater.java:469)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.view.LayoutInflater.inflate(LayoutInflater.java:397)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.view.LayoutInflater.inflate(LayoutInflater.java:353)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.preference.Preference.onCreateView(Preference.java:489)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.preference.Preference.getView(Preference.java:460)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.preference.PreferenceGroupAdapter.getView(PreferenceGroupAdapter.java:221) 03-11 13:49:11.520:E / AndroidRuntime(12286):at android.widget.AbsListView.obtainView(AbsListView.java:2255)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.widget.ListView.makeAndAddView(ListView.java:1790)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.widget.ListView.fillDown(ListView.java:691)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.widget.ListView.fillFromTop(ListView.java:752)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.widget.ListView.layoutChildren(ListView.java:1616)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.widget.AbsListView.onLayout(AbsListView.java:2087)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.view.View.layout(View.java:14841)03-11 13:49:11.520: E / AndroidRuntime(12286):at android.view.ViewGroup.layout(ViewGroup.java:4631)03-11 13:49:11.520: E / AndroidRuntime(12286):at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) 03-11 13:49:11.520:E / AndroidRuntime(12286):at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) 03-11 13:49:11.520:E / AndroidRuntime(12286):at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.view.View.layout(View.java:14841)03-11 13:49:11.520: E / AndroidRuntime(12286):at android.view.ViewGroup.layout(ViewGroup.java:4631)03-11 13:49:11.520: E / AndroidRuntime(12286):at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.widget.FrameLayout.onLayout(FrameLayout.java:388)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.view.View.layout(View.java:14841)03-11 13:49:11.520: E / AndroidRuntime(12286):at android.view.ViewGroup.layout(ViewGroup.java:4631)03-11 13:49:11.520: E / AndroidRuntime(12286):at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374) 03-11 13:49:11.520:E / AndroidRuntime(12286):at android.view.View.layout(View.java:14841)03-11 13:49:11.520: E / AndroidRuntime(12286):at android.view.ViewGroup.layout(ViewGroup.java:4631)03-11 13:49:11.520: E / AndroidRuntime(12286):at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.widget.FrameLayout.onLayout(FrameLayout.java:388)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.view.View.layout(View.java:14841)03-11 13:49:11.520: E / AndroidRuntime(12286):at android.view.ViewGroup.layout(ViewGroup.java:4631)03-11 13:49:11.520: E / AndroidRuntime(12286):at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1983)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1740) 03-11 13:49:11.520:E / AndroidRuntime(12286):at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5600) 03-11 13:49:11.520:E / AndroidRuntime(12286):at android.view.Choreographer $ CallbackRecord.run(Choreographer.java:761) 03-11 13:49:11.520:E / AndroidRuntime(12286):at android.view.Choreographer.doCallbacks(Choreographer.java:574)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.view.Choreographer.doFrame(Choreographer.java:544)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:747) 03-11 13:49:11.520:E / AndroidRuntime(12286):at android.os.Handler.handleCallback(Handler.java:733)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.os.Handler.dispatchMessage(Handler.java:95)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.os.Looper.loop(Looper.java:136)03-11 13:49:11.520: E / AndroidRuntime(12286):at android.app.ActivityThread.main(ActivityThread.java:5139)03-11 13:49:11.520:E / AndroidRuntime(12286):at java.lang.reflect.Method.invokeNative(Native Method)03-11 13:49:11.520:E / AndroidRuntime(12286):at java.lang.reflect.Method.invoke(Method.java:515)03-11 13:49:11.520: E / AndroidRuntime(12286):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:796) 03-11 13:49:11.520:E / AndroidRuntime(12286):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)03-11 13:49:11.520:E / AndroidRuntime(12286):at dalvik.system.NativeStart.main(Native Method)03-11 13:49:11.520: E / AndroidRuntime(12286):引起:java.lang.InstantiationException: 无法实例化类android.view.ViewGroup 03-11 13:49:11.520: E / AndroidRuntime(12286):at java.lang.reflect.Constructor.constructNative(Native Method)03-11 13:49:11.520:E / AndroidRuntime(12286):at java.lang.reflect.Constructor.newInstance(Constructor.java:423)03-11 13:49:11.520:E / AndroidRuntime(12286):at android.view.LayoutInflater.createView(LayoutInflater.java:594)03-11 13:49:11.520:E / AndroidRuntime(12286):... 51 more
你能告诉我发生了什么吗?
答案 0 :(得分:12)
ViewGroup是一个抽象类。
使用其他布局RelativeLayout或LinearLayout
在LogCat中你有:
E/AndroidRuntime(12286): Caused by: java.lang.InstantiationException: can't instantiate class android.view.ViewGroup