android.view.InflateException二进制xml文件行#11:错误膨胀类片段

问题描述 投票:0回答:1

在SO上我找不到像这样的其他错误的解决方案。我收到此错误:

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

然后看到这导致:

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)

在我的代码中,我试图通过单击按钮将Android手机联系人填充到ListView中。所以我有一个带有片段标签的xml的AddressBook.java,然后我有片段类,叫做ContactsFragment.java. ListView有自己的xml(fragment_list_view.xml),列表中的每一行都有自己的xml(fragment_list_item.xml)。该按钮位于MainActivity中,表示查看地址簿,然后单击它后转到AddressBook.java。

但错误表明它无法使视图膨胀。为什么?谢谢。

address book.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);
    }
}

contacts fragment.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
android listview android-listfragment contactscontract android-cursorloader
1个回答
0
投票

事实证明有一些问题。要使用ListFragment来膨胀视图,我必须使用一些特殊的东西:我必须抓住我的整个片段布局并首先将其放入View中,然后使用我的xml中的ListView将其转换为我的id,其必须具有id完全命名为android.R.id.list(根据ListFragment docs)。然后确保return listView(而不是ViewGroup参数中显示的默认onCreateView())。同样对于ListFragment,你必须首先实例化,然后setListAdapter(),如下所示。你不能在setAdapter上使用ListView(尝试过,这是错误的)。必须将适配器设置为ListFragment的实例。

此代码摆脱了我的InflateException错误,但现在它呈现一个空白列表(页面为白色)。所以我将不得不进一步探索,可能与我的查询有关。

@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 
© www.soinside.com 2019 - 2024. All rights reserved.