Hi i am new to android development.
I am using fragments – but i dont entirely understand how it works.
When i launch the app it crashes.
I have googled a lot but cannon solve this 🙁
I have an Activity(MainActivity) that must use a fragment. MainActivity looks like:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
And the main.xml contains a ListView and a Fragment:
<ListView
android:id="@+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment class="com.todolist.NewItemFragment"
android:id="@+id/titles_fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
My fragment extends ListFragment and looks like:
public class NewItemFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.new_item_fragment, container, false);
}
}
And the new_item_fragment.xml looks like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
I Dont know why it crashes. I must have misunderstood something.
I hope someone out there can help – dont know what to do 🙂
THX
I have imported the library:
android.support.v4.app.FragmentActivity
And made the MainActivity class extend FragmentActivity
It gives me the following error:

Modify the
new_item_fragment.xmllayout file like this:to solve the most obvious error. If the app still crashes then post the
Logcatwith the exception.Also, as you’re extending the
Activityclass make sure you only use theFragmentrelated classes from the SDK(not the compatibility package if you have it registered in your app). This classes are available starting fromHoneycomb.If you’re going to use
FragmentActivitythen make sure you use theFragmentclass from the compatibility package(from where theFragmentActivitycomes).