Android:fragment (Trying to instantiate a class that's not a Fragment)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.