0

I could not find a solution with other errors like this on SO. I get this error:

android.view.InflateException Binary xml file line #11: error inflating class fragment

then see this in the caused by:

Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment 12-26 23:44:06.078: E/AndroidRuntime(13191): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)

In my code I am trying to get Android phone Contacts populated into a ListView, with a button click. So I have an AddressBook.java with simply an xml with fragment tags, then I have the fragment class, called ContactsFragment.java. The ListView has its own xml (fragment_list_view.xml) and each row in the list has its own xml (fragment_list_item.xml). The button is in a MainActivity that says, View Address Book and then goes to AddressBook.java upon clicking it.

But the error says it can't inflate the view. Why? Thanks.

AddressBook.java

package org.azurespot.practiceapp.addressbook;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;

import com.example.practiceapp.R;

public class AddressBook extends FragmentActivity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_book);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.address_book, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

ContactsFragment.java

package org.azurespot.practiceapp.addressbook;

import android.app.ListFragment;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.ContentUris;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.example.practiceapp.R;

/*
 * Partially from http://stackoverflow.com/questions/18199359/how-to-display-contacts-in-a-listview-in-android-for-android-api-11
 */

public class ContactsFragment extends ListFragment implements 
                                LoaderCallbacks<Cursor>{

    private CursorAdapter mAdapter;
    public ListView listView;
    public Cursor cursor;
    private android.content.Context context;
    public View view;
    public static Uri uri;

    public static final String[] FROM = { 
            ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.HAS_PHONE_NUMBER };

    private static final int[] TO = { R.id.contact_thumbnail, 
        R.id.contact_name, R.id.contact_number };

    // columns requested from the database
    private static final String[] PROJECTION = {
            Contacts.LOOKUP_KEY,
            ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.HAS_PHONE_NUMBER,
    };

    // this goes in the CursorLoader parameter list, it filters
    // out only those contacts who have a phone number
    private static final String FILTER = 
            ContactsContract.Contacts.PHOTO_THUMBNAIL_URI + "=?" +
            ContactsContract.Contacts.DISPLAY_NAME + "=?" +
            ContactsContract.Contacts.HAS_PHONE_NUMBER + "=?";

    private static final String[] SELECTION_ARGS = {
            ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.HAS_PHONE_NUMBER
    };

    private static final int URL_LOADER = 0;

    @Override
    public View onCreateView(LayoutInflater inflater,
             ViewGroup viewGroup,Bundle bundle) {
        // delete list if already there (old version)
        if (!(listView == null)){
            listView.setAdapter(null);
        }

        // Initializes the CursorLoader. The URL_LOADER value is 
        // eventually passed to onCreateLoader().
        getLoaderManager().initLoader(URL_LOADER, null, this);

        listView = (ListView) view.findViewById(R.id.contact_list); 

        // create adapter once
        context = getActivity();
        Cursor c = null; // there is no cursor yet
        int flags = 0; // no auto-requery! Loader requeries.
        // put List in adapter
        mAdapter = new SimpleCursorAdapter(context, 
                R.layout.fragment_list_item, c, FROM, TO, flags);
        // every time we start, use a list adapter
        listView.setAdapter(mAdapter);

        return viewGroup;

    } // end onCreateView 


    // Empty public constructor, required by the system
    public ContactsFragment() {}

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

//        // scroll faster
//        listView.setFastScrollEnabled(true);

    }

    // a CursorLoader does a query in the background
    // once it gets initialized
    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        // load from the "Contacts table"
        Uri contentUri = Contacts.CONTENT_URI;

        // no sub-selection, no sort order, simply every row
        // projection says we want just the _id and the name column
        return new CursorLoader(getActivity(),
                contentUri,
                PROJECTION,
                FILTER,
                SELECTION_ARGS,
                ContactsContract.Contacts.DISPLAY_NAME + " ASC");
    }

    // required with the LoaderCallbacks<Cursor> interface
    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
         // Once cursor is loaded, give it to adapter
        mAdapter.swapCursor(data);
    }
    // required with the LoaderCallbacks<Cursor> interface
    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        // Delete the reference to the existing Cursor,
        // so it can recycle it
        mAdapter.swapCursor(null);

    }
}

Logcat

12-26 23:44:04.498: I/PersonaManager(13191): getPersonaService() name persona_policy
12-26 23:44:04.558: D/skia(13191): GFXPNG PNG bitmap created width:48 height:48 bitmap id is 270 
12-26 23:44:04.568: E/MoreInfoHPW_ViewGroup(13191): Parent view is not a TextView
12-26 23:44:04.578: D/skia(13191): GFXPNG PNG bitmap created width:72 height:72 bitmap id is 271 
12-26 23:44:04.578: D/skia(13191): GFXPNG PNG bitmap created width:144 height:144 bitmap id is 272 
12-26 23:44:04.588: D/skia(13191): GFXPNG PNG bitmap created width:144 height:144 bitmap id is 273 
12-26 23:44:04.678: I/Adreno-EGL(13191): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build:  ()
12-26 23:44:04.678: I/Adreno-EGL(13191): OpenGL ES Shader Compiler Version: E031.24.00.08+13
12-26 23:44:04.678: I/Adreno-EGL(13191): Build Date: 03/20/14 Thu
12-26 23:44:04.678: I/Adreno-EGL(13191): Local Branch: 0320_AU200_patches
12-26 23:44:04.678: I/Adreno-EGL(13191): Remote Branch: 
12-26 23:44:04.678: I/Adreno-EGL(13191): Local Patches: 
12-26 23:44:04.678: I/Adreno-EGL(13191): Reconstruct Branch: 
12-26 23:44:04.728: D/OpenGLRenderer(13191): Enabling debug mode 0
12-26 23:44:06.048: I/PersonaManager(13191): getPersonaService() name persona_policy
12-26 23:44:06.058: E/MoreInfoHPW_ViewGroup(13191): Parent view is not a TextView
12-26 23:44:06.078: D/AndroidRuntime(13191): Shutting down VM
12-26 23:44:06.078: W/dalvikvm(13191): threadid=1: thread exiting with uncaught exception (group=0x41737da0)
12-26 23:44:06.078: E/AndroidRuntime(13191): FATAL EXCEPTION: main
12-26 23:44:06.078: E/AndroidRuntime(13191): Process: com.example.practiceapp, PID: 13191
12-26 23:44:06.078: E/AndroidRuntime(13191): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.practiceapp/org.azurespot.practiceapp.addressbook.AddressBook}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
12-26 23:44:06.078: E/AndroidRuntime(13191):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2395)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2453)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at android.app.ActivityThread.access$900(ActivityThread.java:173)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at android.os.Handler.dispatchMessage(Handler.java:102)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at android.os.Looper.loop(Looper.java:136)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at android.app.ActivityThread.main(ActivityThread.java:5579)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at java.lang.reflect.Method.invokeNative(Native Method)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at java.lang.reflect.Method.invoke(Method.java:515)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at dalvik.system.NativeStart.main(Native Method)
12-26 23:44:06.078: E/AndroidRuntime(13191): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
12-26 23:44:06.078: E/AndroidRuntime(13191):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:366)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at android.app.Activity.setContentView(Activity.java:2031)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at org.azurespot.practiceapp.addressbook.AddressBook.onCreate(AddressBook.java:16)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at android.app.Activity.performCreate(Activity.java:5451)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2359)
12-26 23:44:06.078: E/AndroidRuntime(13191):    ... 11 more
12-26 23:44:06.078: E/AndroidRuntime(13191): Caused by: java.lang.NullPointerException
12-26 23:44:06.078: E/AndroidRuntime(13191):    at org.azurespot.practiceapp.addressbook.ContactsFragment.onCreateView(ContactsFragment.java:80)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at android.app.Fragment.performCreateView(Fragment.java:1700)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:866)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at android.app.Activity.onCreateView(Activity.java:4997)
12-26 23:44:06.078: E/AndroidRuntime(13191):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
12-26 23:44:06.078: E/AndroidRuntime(13191):    ... 21 more
12-26 23:49:06.108: I/Process(13191): Sending signal. PID: 13191 SIG: 9
marmor
  • 27,641
  • 11
  • 107
  • 150
Azurespot
  • 3,066
  • 3
  • 45
  • 73

1 Answers1

0

It turns out there were a few problems. To inflate a view using a ListFragment, I had to use something special: I had to grab my entire fragment layout and put it into a View first, then cast that to my ListView, using the id in my xml, which MUST have the id exactly named as android.R.id.list (according to the ListFragment docs). Then make sure to return listView (instead of the default ViewGroup as shown in the onCreateView() parameter). Also for ListFragment, you must instantiate first, then setListAdapter() to it, as shown below. You can't use setAdapter on a ListView (tried it, it was wrong). The adapter has to be set to an instance of ListFragment.

This code got rid of my InflateException error, but now it presents a blank list (page is white). So I will have to explore further, probably something to do with my queries.

@Override
    public View onCreateView(LayoutInflater inflater,
             ViewGroup container,Bundle bundle) {

        // since using ListFragment, you must inflate your view this way
        View rootView = inflater.inflate(R.layout.fragment_list_view,
                container, false);
        ListView listView = (ListView) rootView
                                .findViewById(android.R.id.list);

        // Initializes the CursorLoader. The URL_LOADER value is 
        // eventually passed to onCreateLoader().
        getLoaderManager().initLoader(URL_LOADER, null, this);

        // create adapter once
        context = getActivity();
        Cursor c = null; // there is no cursor yet
        int flags = 0; // no auto-requery! Loader requeries.
        // put List in adapter
        listAdapter = new SimpleCursorAdapter(context, 
                R.layout.fragment_list_item, c, FROM, TO, flags);
        // every time we start, use a list adapter
        ListFragment listFrag = new ListFragment();
        listFrag.setListAdapter(listAdapter);

        return listView;

    } // end onCreateView 
Azurespot
  • 3,066
  • 3
  • 45
  • 73