Swift dodger
Swift dodger

Reputation: 21

Runtime error inflating fragment

Error: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.todolist/com.example.todolist.ToDoListActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment

I know this question has been asked before but i have searched through them and could not find a solution. I get this error a few seconds after the app is launched without touching anything, the fragments do not load and then it crashes.

I have one FragmentActivity class with two Fragments in it(ListFragment and EditText).My min sdk support is 7 so i am using support library imports.

This is the code for the 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();

}

This is the layout for the 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>

This is the EditText Fragment code:

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;
}

}

This is the EditText Fragment Layout:

<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" />

My ListFragment Class uses the default layout so i only created a .java file with the class declaration(the class name is the same as the name written in the fragment activity XML)

This the remaining of the 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)

Upvotes: 2

Views: 1220

Answers (1)

oli
oli

Reputation: 4934

The actual error is Fragment com.example.todolist.NewItemFragment did not create a view.

This is happening because you have a method named OnCreateView instead of onCreateView. The method should start with a small letter. If you marked the method as @Override it would have shown up this error.

Upvotes: 2

Related Questions