错误: java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.todolist/com.example.todolist.ToDoListActivity}:android.view.InflateException:二进制 XML 文件第 6 行:膨胀类片段时出错
我知道以前有人问过这个问题,但我已经搜索过它们并且找不到解决方案。在应用程序启动几秒钟后我收到此错误而没有触摸任何东西,片段不加载然后它崩溃。
我有一个带有两个 Fragment 的 FragmentActivity 类(ListFragment 和 EditText)。我的最小 sdk 支持是 7,所以我正在使用支持库导入。
这是 FragmentActivity 的代码:
public class ToDoListActivity extends FragmentActivity
implements NewItemFragment.OnNewItemAddedListener{
private ArrayList<String> todoItems;
private ArrayAdapter<String> aa;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do_list);
FragmentManager fm = getSupportFragmentManager();
ToDoListFragment toDoListFragment = (ToDoListFragment)
fm.findFragmentById(R.id.ToDoListFragment);
aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
todoItems);
toDoListFragment.setListAdapter(aa);
}
public void onNewItemAdded(String newItem){
todoItems.add(newItem);
aa.notifyDataSetChanged();
}
这是 FragmentActivity 的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/NewItemFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.todolist.NewItemFragment"/>
<fragment
android:id="@+id/ToDoListFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.todolist.ToDoListFragment" />
</LinearLayout>
这是 EditText 片段代码:
public class NewItemFragment extends Fragment {
private OnNewItemAddedListener onNewItemAddedListener;
public interface OnNewItemAddedListener {
public void onNewItemAdded(String newItem);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
onNewItemAddedListener = (OnNewItemAddedListener)activity;
}catch (ClassCastException e) {
throw new ClassCastException(activity.toString() +
"must implement OnNewItemAddedListener");
}
}
public View OnCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View view = inflater.inflate(R.layout.new_item_fragment, container
, false);
final EditText myEditText = (EditText)view.findViewById(R.id.myEditText);
myEditText.setOnKeyListener(new View.OnKeyListener() {
...
});
return view;
}
}
这是 EditText 片段布局:
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/addItemHint"
android:contentDescription="@string/addItemContentDescription" />
我的 ListFragment 类使用默认布局,所以我只创建了一个带有类声明的 .java 文件(类名与片段活动 XML 中写入的名称相同)
这是 LogCat 的剩余部分:
02-17 17:33:25.135: E/AndroidRuntime(23262): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
02-17 17:33:25.135: E/AndroidRuntime(23262): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
02-17 17:33:25.135: E/AndroidRuntime(23262): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-17 17:33:25.135: E/AndroidRuntime(23262): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
02-17 17:33:25.135: E/AndroidRuntime(23262): at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 17:33:25.135: E/AndroidRuntime(23262): at android.os.Looper.loop(Looper.java:123)
02-17 17:33:25.135: E/AndroidRuntime(23262): at android.app.ActivityThread.main(ActivityThread.java:3691)
02-17 17:33:25.135: E/AndroidRuntime(23262): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 17:33:25.135: E/AndroidRuntime(23262): at java.lang.reflect.Method.invoke(Method.java:507)
02-17 17:33:25.135: E/AndroidRuntime(23262): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
02-17 17:33:25.135: E/AndroidRuntime(23262): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
02-17 17:33:25.135: E/AndroidRuntime(23262): at dalvik.system.NativeStart.main(Native Method)
02-17 17:33:25.135: E/AndroidRuntime(23262): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
02-17 17:33:25.135: E/AndroidRuntime(23262): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
02-17 17:33:25.135: E/AndroidRuntime(23262): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
02-17 17:33:25.135: E/AndroidRuntime(23262): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
02-17 17:33:25.135: E/AndroidRuntime(23262): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-17 17:33:25.135: E/AndroidRuntime(23262): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
02-17 17:33:25.135: E/AndroidRuntime(23262): at android.app.Activity.setContentView(Activity.java:1663)
02-17 17:33:25.135: E/AndroidRuntime(23262): at com.example.todolist.ToDoListActivity.onCreate(ToDoListActivity.java:18)
02-17 17:33:25.135: E/AndroidRuntime(23262): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
02-17 17:33:25.135: E/AndroidRuntime(23262): ... 11 more
02-17 17:33:25.135: E/AndroidRuntime(23262): Caused by: java.lang.IllegalStateException: Fragment com.example.todolist.NewItemFragment did not create a view.
02-17 17:33:25.135: E/AndroidRuntime(23262): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:303)
02-17 17:33:25.135: E/AndroidRuntime(23262): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)