android – Using include as root layout node throws "Error inflating class include" exception-ThrowExceptions

Exception or error:

I try to use different list item layout depending on the OS version.

So, I created different layouts associated with conditions.
One of them is (in layout/search_result_list_item.xml)

<?xml version="1.0" encoding="utf-8"?>
<include xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    layout="@android:layout/simple_list_item_1">
</include>

it includes the standard “simple_list_item_1”.

In my Java code, the layout is associated to the list like this :

    adapter = new SimpleCursorAdapter(getActivity(),
                                      R.layout.search_results_list_item,
                                      null,
                                      from,
                                      to,
                                      0);

When a list item is displayed, the following exception is thrown :

android.view.InflateException: Binary XML file line #2: Error inflating class include
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
   at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
   at android.support.v4.widget.ResourceCursorAdapter.newView(ResourceCursorAdapt

What is wrong ? can’t be used as a root item ? despite ADT allows it.

How to solve:

If anyone else is wondering this is the answer:

<?xml version="1.0" encoding="utf-8"?>
<merge>
    <include xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        layout="@android:layout/simple_list_item_1">
    </include>
</merge>

###

You can provide Android OS verion specific resources using qualifiers on the resource folders.

See: http://developer.android.com/guide/topics/resources/providing-resources.html

The last item in the list of possible qualifiers is version (API level).

Leave a Reply

Your email address will not be published. Required fields are marked *