I'm starting to learn Android and am having a problem when creating fragments. I've tried creating a fragment in code in just about every way every tutorial specifies but every time I do the program immediately crashes and I get a log error "no view found for [id of fragment for container]".
The activity I create the fragment from is:
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
FragmentTestActivity activity = new FragmentTestActivity();
ft.add(R.id.fragment_container, activity);
ft.commit();
}
}
The fragment itself if
public class FragmentTestActivity extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View vw = inflater.inflate(R.layout.fragment, parent, false);
return vw;
}
}
And the layout of the fragment is
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_container" >
</FrameLayout>
It doesn't do anything useful at the moment, I just want to see if I can make a fragment before moving on to do anything useful, but every time I try the program crashes.