问题
I recently started with android and I encountered an issue that I can't find what is causing it. I am using the standard template for Master/Slave flow.
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle"
tools:context=".TasksListActivity" >
<fragment
android:id="@+id/tasks_list"
android:name="bg.pandasoft.task_2_do.TasksListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@android:layout/list_content" />
<FrameLayout
android:id="@+id/tasks_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>
The tasks_list fragment is holding my categories and I tried do this in its fragment handler:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sql = new SQLCat(getActivity());
BuildList();
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
Categories item = _CatAdapter.getItem(position);
CAT_KEY = item.GetKey();
if (item.GetDeletable() == 0) {
Toast.makeText(getActivity(), "Category '" + item.GetName()
+ "' cannot be edited or deleted.", GLOBAL.ToastDuration);
} else {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
getActivity());
alertDialog.setTitle("Actions");
alertDialog.setSingleChoiceItems(array, -1,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
if (which == 0) {
deleteEntry();
BuildList();
} else if (which == 1) {
Categories item = _CatAdapter
.getItemById(CAT_KEY);
AccessEditScreen(item.GetKey(), false);
}
dialog.cancel();
}
});
alertDialog.show();
}
return true;
}
});
The problem is that when the debugger hits this row it all blows up with an Inflate Exception. Is what I am trying to do possible? Or have I taken a wrong turn somewhere?
-------EDIT------- The error is as follows:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{bg.pandasoft.task_2_do/bg.pandasoft.task_2_do.TasksListActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:350)
at android.app.Activity.setContentView(Activity.java:1930)
at bg.pandasoft.task_2_do.TasksListActivity.onCreate(TasksListActivity.java:20)
at android.app.Activity.performCreate(Activity.java:5283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
... 11 more
Caused by: java.lang.IllegalStateException: Content view not yet created
at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
at bg.pandasoft.task_2_do.TasksListFragment.onCreate(TasksListFragment.java:62)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1477)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:682)
... 21 more
回答1:
The problem is that getListView() cannot return list until fragments View created. Refer to Fragment life-circle:
As You can see onCreate() callback get called before onCreateView(), which means that fragments view (in your case it's list) haven't been inflated yet.
So, then activity starts and tries to create fragment - it get exception due to check inside getListView() for list ready state.
You need to call getListView() and realted code in onCreateView(). Read more about fragments on d.android.com.
回答2:
I had this problem because I define the font family in style XML. Verify and remove it if you do too.
<item name="android:fontFamily">sans-serif</item> - Remove this
来源:https://stackoverflow.com/questions/24083744/inflate-exception-on-setting-up-a-long-click-event-listener