I have a very basic application in which I am trying to create ListFragment on the MainActivity.
I keep on getting the following runtime error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.proto1.listfragment/com.proto1.listfragment.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
I have two classes.
The first is MainActivity.java as shown below:
package com.proto1.listfragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
The second is the class relating to the listfragment SectionsList as shown below:
package com.proto1.listfragment;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
public class SectionsList extends ListFragment {
//Create an array to hold the sections
String sections [] = new String []{
"Starters", "Mains", "Sides", "Desserts", "Drinks"
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
//Create array adapter to store list of menu sections
ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(), android.R.layout.simple_list_item_1,sections);
//Set list adapter of fragment
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
}
I then have one XML layout file called activity_main as shown below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:orientation = "horizontal"
android:layout_height="fill_parent" >
<fragment
android:name = "com.proto1.listfragment.MainActivity"
android:id="@+id/sectionFrag"
android:layout_width = "wrap_content"
android:layout_height = "fill_parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
tools:context=".MainActivity" />
I am unable to see why this error is being generated. Can someone please shed some light on the issue? Many thanks!
A FragmentActivity is not a Fragment in itself. But rather, it is a way to easily interact with Fragments. The Fragment you call in your xml should be SectionList.