我在我的程序中使用了布局inflater。在Android 4中一切都很好但是当在android 2.2中使用时,程序终止时出现运行时错误(强制关闭)。
问题出在哪里?
部分代码:
Log.i("Hossein", "1");
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Log.i("Hossein", "2");
View itemView = inflater.inflate(R.layout.drawer_list_item, parent,
false);
Log.i("Hossein", "3");
布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="?attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?attr/dropdownListPreferredItemHeight"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="horizontal" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical|left"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
style="?attr/spinnerDropDownItemStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true" />
<TextView
android:id="@+id/subtitle"
style="?attr/spinnerDropDownItemStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textAppearance="?attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
Logcat消息:
09-30 11:26:32.934:I / Hossein(416):1
09-30 11:26:32.934:I / Hossein(416):2
09-30 11:26:32.974:D / AndroidRuntime(416):关闭虚拟机
09-30 11:26:32.974:W / dalvikvm(416):threadid = 1:线程退出时未捕获异常(组= 0x4001d800)
09-30 11:26:33.034:E / AndroidRuntime(416):致命异常:主
09-30 11:26:33.034:E / AndroidRuntime(416):android.view.InflateException:二进制XML文件行#1:错误导致类
09-30 11:26:33.034:E / AndroidRuntime(416):在android.view.LayoutInflater.createView(LayoutInflater.java:513)
09-30 11:26:33.034:E / AndroidRuntime(416):at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.view.LayoutInflater.inflate(LayoutInflater.java:385)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.view.LayoutInflater.inflate(LayoutInflater.java:320)
09-30 11:26:33.034:E / AndroidRuntime(416):at com.androidbegin.sidemenututorial.MenuListAdapter.getView(MenuListAdapter.java:56)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.widget.AbsListView.obtainView(AbsListView.java:1315)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.widget.ListView.makeAndAddView(ListView.java:1727)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.widget.ListView.fillDown(ListView.java:652)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.widget.ListView.fillFromTop(ListView.java:709)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.widget.ListView.layoutChildren(ListView.java:1580)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.widget.AbsListView.onLayout(AbsListView.java:1147)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.view.View.layout(View.java:7035)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:702)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.view.View.layout(View.java:7035)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.widget.FrameLayout.onLayout(FrameLayout.java:333)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.view.View.layout(View.java:7035)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.view.View.layout(View.java:7035)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.widget.FrameLayout.onLayout(FrameLayout.java:333)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.view.View.layout(View.java:7035)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.widget.FrameLayout.onLayout(FrameLayout.java:333)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.view.View.layout(View.java:7035)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.os.Handler.dispatchMessage(Handler.java:99)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.os.Looper.loop(Looper.java:123)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.app.ActivityThread.main(ActivityThread.java:4627)
09-30 11:26:33.034:E / AndroidRuntime(416):at java.lang.reflect.Method.invokeNative(Native Method)
09-30 11:26:33.034:E / AndroidRuntime(416):at java.lang.reflect.Method.invoke(Method.java:521)
09-30 11:26:33.034:E / AndroidRuntime(416):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
09-30 11:26:33.034:E / AndroidRuntime(416):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-30 11:26:33.034:E / AndroidRuntime(416):at dalvik.system.NativeStart.main(Native Method)
09-30 11:26:33.034:E / AndroidRuntime(416):引起:java.lang.reflect.InvocationTargetException
09-30 11:26:33.034:E / AndroidRuntime(416):在android.widget.LinearLayout。(LinearLayout.java:115)
09-30 11:26:33.034:E / AndroidRuntime(416):at java.lang.reflect.Constructor.constructNative(Native Method)
09-30 11:26:33.034:E / AndroidRuntime(416):at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.view.LayoutInflater.createView(LayoutInflater.java:500)
09-30 11:26:33.034:E / AndroidRuntime(416):... 34更多
09-30 11:26:33.034:E / AndroidRuntime(416):引起:android.content.res.Resources $ NotFoundException:来自可绘制资源ID的文件res / drawable / list_selector_background.xml#0x0
09-30 11:26:33.034:E / AndroidRuntime(416):在android.content.res.Resources.loadDrawable(Resources.java:1697)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.content.res.TypedArray.getDrawable(TypedArray.java:601)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.view.View。(View.java:1885)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.view.View。(View.java:1834)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.view.ViewGroup。(ViewGroup.java:285)
09-30 11:26:33.034:E / AndroidRuntime(416):... 38更多
09-30 11:26:33.034:E / AndroidRuntime(416):引起:android.content.res.Resources $ NotFoundException:来自xml类型drawable资源ID#0x0的文件res / drawable / list_selector_background.xml
09-30 11:26:33.034:E / AndroidRuntime(416):在android.content.res.Resources.loadXmlResourceParser(Resources.java:1924)
09-30 11:26:33.034:E / AndroidRuntime(416):在android.content.res.Resources.loadDrawable(Resources.java:1692)
09-30 11:26:33.034:E / AndroidRuntime(416):... 42更多
09-30 11:26:33.143:D / dalvikvm(416):GC_FOR_MALLOC在102ms内释放了3421个对象/ 286952个字节
09-30 11:26:35.735:I / Process(416):发送信号。 PID:416 SIG:9
答案 0 :(得分:0)
{1}}是在API级别11(Android 3.0)中引入的。
将根布局中的android:background="?android:attr/activatedBackgroundIndicator"
替换为所有目标API级别上可用的内容,或使用activatedBackgroundIndicator
attribute机制在11之前的布局和其他布局之间进行变换。
请注意,堆栈跟踪提到list_selector_background.xml
- 您确定这是导致此堆栈跟踪的确切代码吗?
答案 1 :(得分:0)
// try this way
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical|left"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
style="?android:spinnerDropDownItemStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true" />
<TextView
android:id="@+id/subtitle"
style="?android:spinnerDropDownItemStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textAppearance="?android:textAppearanceSmall" />
</LinearLayout>
</LinearLayout>