Fragments api:https://developer.android.com/guide/components/fragments.html
The first time you encounter a problem with fragment is:
Trying to instantiate a class xxx that's not a Fragment)
The problem arises:
Fragment was introduced by Android3.0 (API level 11), and the previous API did not use Android.app.Fragment directly.
A compatible low version of the fragment that is placed in the Android.support.v4.jar Compatibility Pack appears.
However, by declaring fragment within the activity's layout file, the,<fragment> node defaults to using fragment in Android.app.Fragment.
If you want to use android.support.v4.app.Fragment, you need the activity to inherit android.support.v4.app.FragmentActivity.
Solution:
1. Custom Fragment need to inherit from android.app.Fragment instead of android.support.v4.app.Fragment
This requires the API minimum version also support fragment line;
Package Com.example.helloservice.fragments;import Android.app.activity;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.ViewGroup; Import com.example.helloservice.r;/** * Create an instance of this fragment. */public class Examplefragment extends Fragment { @Override public void OnCreate (Bundle savedinstancestate) { C2/>super.oncreate (savedinstancestate); } @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { //Inflate the layout for this fragment return Inflater.inflate (R.layout.fragment_example, container, false); c9/>} @Override public void Onattach (activity activity) { Super.onattach (activity); } @Override public void Ondetach () { super.ondetach (); }}
2. Activity inherits from fragmentactivity rather than activity
public class Examplefragment extends Android.support.v4.app.Fragment { ...} public class Mainactivity extends Android.support.v4.app.FragmentActivity { ...} XML: <fragment android:name= "com.example.helloservice.fragments.ExampleFragment" android:id= "@+ Id/examplefragment " android:tag=" @string/hello_blank_fragment " android:layout_width=" Wrap_content " android:layout_height= "wrap_content" android:layout_margintop= "300DP"/>
Android:fragment (Trying to instantiate a class that's not a Fragment)