android.view.InflateException Binary xml 文件第 11 行:错误膨胀类片段
Posted
技术标签:
【中文标题】android.view.InflateException Binary xml 文件第 11 行:错误膨胀类片段【英文标题】:android.view.InflateException Binary xml file line #11: error inflating class fragment 【发布时间】:2015-02-24 06:09:37 【问题描述】:我在 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 中。所以我有一个AddressBook.java
,只有一个带有片段标签的xml,然后我有一个片段类,称为ContactsFragment.java.
ListView 有自己的xml(fragment_list_view.xml
),列表中的每一行都有自己的xml(@987654326 @)。该按钮位于MainActivity
中,上面写着查看地址簿,然后单击它会转到AddressBook.java。
但错误表明它无法放大视图。为什么?谢谢。
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://***.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 ComponentInfocom.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
【问题讨论】:
【参考方案1】:事实证明存在一些问题。要使用ListFragment
为视图膨胀,我必须使用一些特殊的东西:我必须先抓取整个片段布局并将其放入视图中,然后使用我的 xml 中的id
将其投射到我的ListView
,其中必须有 id
完全命名为 android.R.id.list
(根据 ListFragment docs)。然后确保return listView
(而不是onCreateView()
参数中显示的默认ViewGroup
)。同样对于ListFragment
,必须先实例化,再将setListAdapter()
实例化给它,如下图。你不能在ListView
上使用setAdapter
(试过了,这是错误的)。适配器必须设置为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
【讨论】:
以上是关于android.view.InflateException Binary xml 文件第 11 行:错误膨胀类片段的主要内容,如果未能解决你的问题,请参考以下文章