If you want to use a fragment you should have a MainActivity
with something like this:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new MyFragment())
.commit();
}
}
}
Then, a class for the Fragment
, in this case a ListFragment
:
public class MyFragment extends ListFragment {
public MyFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ArrayList<String> la = new ArrayList<String>();
la.add("Item1");
la.add("Item2");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, la);
setListAdapter(adapter);
return rootView;
}
fragment_main
Should look something like this:
<LinearLayout
// ....>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>