0

I've used layout inflater in my program. Everything is good in Android 4 but when use in android 2.2, the program terminates with a runtime error (force close).

Where is the problem?

Part of the code:

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");

Layout code:

<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 messages:

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): Shutting down VM

09-30 11:26:32.974: W/dalvikvm(416): threadid=1: thread exiting with uncaught exception (group=0x4001d800)

09-30 11:26:33.034: E/AndroidRuntime(416): FATAL EXCEPTION: main

09-30 11:26:33.034: E/AndroidRuntime(416): android.view.InflateException: Binary XML file line #1: Error inflating class

09-30 11:26:33.034: E/AndroidRuntime(416): at 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): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.view.LayoutInflater.inflate(LayoutInflater.java:385)

09-30 11:26:33.034: E/AndroidRuntime(416): at 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): at android.widget.AbsListView.obtainView(AbsListView.java:1315)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.widget.ListView.makeAndAddView(ListView.java:1727)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.widget.ListView.fillDown(ListView.java:652)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.widget.ListView.fillFromTop(ListView.java:709)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.widget.ListView.layoutChildren(ListView.java:1580)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.widget.AbsListView.onLayout(AbsListView.java:1147)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.view.View.layout(View.java:7035)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:702)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.view.View.layout(View.java:7035)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.view.View.layout(View.java:7035)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.view.View.layout(View.java:7035)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.view.View.layout(View.java:7035)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.view.View.layout(View.java:7035)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.os.Handler.dispatchMessage(Handler.java:99)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.os.Looper.loop(Looper.java:123)

09-30 11:26:33.034: E/AndroidRuntime(416): at 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): Caused by: java.lang.reflect.InvocationTargetException

09-30 11:26:33.034: E/AndroidRuntime(416): at 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): at android.view.LayoutInflater.createView(LayoutInflater.java:500)

09-30 11:26:33.034: E/AndroidRuntime(416): ... 34 more

09-30 11:26:33.034: E/AndroidRuntime(416): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/list_selector_background.xml from drawable resource ID #0x0

09-30 11:26:33.034: E/AndroidRuntime(416): at android.content.res.Resources.loadDrawable(Resources.java:1697)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.view.View.(View.java:1885)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.view.View.(View.java:1834)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.view.ViewGroup.(ViewGroup.java:285)

09-30 11:26:33.034: E/AndroidRuntime(416): ... 38 more

09-30 11:26:33.034: E/AndroidRuntime(416): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/list_selector_background.xml from xml type drawable resource ID #0x0

09-30 11:26:33.034: E/AndroidRuntime(416): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1924)

09-30 11:26:33.034: E/AndroidRuntime(416): at android.content.res.Resources.loadDrawable(Resources.java:1692)

09-30 11:26:33.034: E/AndroidRuntime(416): ... 42 more

09-30 11:26:33.143: D/dalvikvm(416): GC_FOR_MALLOC freed 3421 objects / 286952 bytes in 102ms

09-30 11:26:35.735: I/Process(416): Sending signal. PID: 416 SIG: 9

4

2 回答 2

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>
于 2013-09-30T09:53:09.117 回答
0

activatedBackgroundIndicator属性是在 API 级别 11 (Android 3.0) 中引入的。

android:background="?android:attr/activatedBackgroundIndicator"根布局中的 替换为所有目标 API 级别上可用的内容,或使用替代资源机制在 11 之前的布局和其他布局之间变化。

请注意堆栈跟踪提到list_selector_background.xml- 您确定这是导致此堆栈跟踪的确切代码吗?

于 2013-09-30T09:39:47.103 回答