运行时错误膨胀片段

时间:2013-02-17 16:12:52

标签: android

错误: java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.todolist / com.example.todolist.ToDoListActivity}:android.view.InflateException:二进制XML文件行# 6:错误膨胀类片段

我知道之前已经问过这个问题,但是我搜索过它们并找不到解决方法。应用程序启动后几秒钟我没有碰到任何东西就会出现此错误,碎片无法加载然后崩溃。

我有一个FragmentActivity类,里面有两个片段(ListFragment和EditText)。我的min 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)

1 个答案:

答案 0 :(得分:2)

实际错误为Fragment com.example.todolist.NewItemFragment did not create a view.

这种情况正在发生,因为您有一个名为OnCreateView而非onCreateView的方法。该方法应以小写字母开头。如果您将方法标记为@Override,则会显示此错误。