Should i declare the MainFragmentDemoActivity in manifest?

后端 未结 3 481
别那么骄傲
别那么骄傲 2021-01-28 10:34

I am trying to learn fragment.I am clicking a fragment class and my app crashes.I have declared it in the manifest..But why it is happening..My Menu class

public         


        
相关标签:
3条回答
  • 2021-01-28 10:59

    Any Class that extends Activity in your application must be declared in the manifest, however, that does not appear to be your problem. Technically, this does answer your question though ;)

    This line:

    Caused by: java.lang.ClassCastException: com.example.practise.ListFragmentDemo cannot be cast to android.app.Fragment
    

    Does "ListFragmentDemo" extend Fragment?

    0 讨论(0)
  • 2021-01-28 11:03

    Your ListFragmentDemo extends from android.support.v4.app.Fragment (the support version of the Fragment class) and not android.app.Fragment (the version which is inside Android)

    Change

    import android.support.v4.app.Fragment;
    

    to

    import android.app.Fragment;
    

    in the ListFragmentDemo class

    0 讨论(0)
  • 2021-01-28 11:10

    I think the problem is that you're Activity(Main Activity/ Activity that hosts the Fragments) doesn't extend the FragmentActivity class.

    If you're using the Support Library for ActionBar, the just make sure your Activity class extends the ActionBarActivity class.

    That's why your Fragments can't be cast into the Main Activity.(ClassCastException)

    So it should be:

    public class MainFragmentDemoActivity extends FragmentActivity .... {
    

    or:

    public class MainFragmentDemoActivity extends ActionBarActivity .... {
    

    Open for correction, as always! Regards, Edward Quixote.

    0 讨论(0)
提交回复
热议问题