Inconvertible type error when creating fragments

0 Julian Gaines · February 19, 2015
I am trying to simulate my fragment code based off bucky's video.  I followed his steps in creating the topsectionfragment and the bottompicture fragment.  In the MainActvity.java when I created the createMeMe method and tried to to import the bottompicture fragment into java it gives me an error saying that: inconvertible types: cannot cast 'android.support.v4.app.Fragment' to 'com......BottomPictureFragment'.  I not sure what this message means.  Can someone help please.  Here is my MainActivity code below.

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity implements TopSectionFragment.TopSectionListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    //this gets called by the Top Fragment when the user clicks the button
    @Override
    public void createMeme(String top,String bottom) {
        BottomPictureFragment bottomFragment = (BottomPictureFragment) getSupportFragmentManager().findFragmentById(R.id.fragment2);
        //bottomFragment.setMemeText(top, bottom);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Dol Lod · February 19, 2015
I was just having a similar problem about an hour ago. However what I understand is that android.app.fragment and android.support.v4.app.Fragment; are two very different types of fragments. Think of the v4 fragments as basic fragments that are compatible with API's  of level 11 and onward and only supports a subset of features provided by android.app.fragment. Android.app.fragment is when fragment support is guaranteed for higher level API's and consequently has many subclasses of particular kinds of fragments such as the one you are trying to use that v4 fragments cannot support. 

I would first check for consistency in the import of fragment you are using. At least on Windows, when I extended Fragment by default, it use android.app.fragment instead of android.support.v4.app.Fragment.

However, my guess is that you are probably using appv4 fragments, but the feature you are referring to only belongs to android.app.fragment and so you need to decide whether you want to use v4 fragment or android.app.fragment. 

I am not that familiar with fragments either, but this like I said is what I just googled to solve my own issue. I hope it helps. 
0 Mike portelli_mike@live.com · March 29, 2015
hi guys what can I do to solve this problem?
+2 Bartosz MaciÄ™ga · March 29, 2015
I think this problem is very simple, you must  check if when you`re creating BottomFragment you`re using android.support.v4.app.Fragment because you when you`re using  "getSupportFragmentManager", if you use in BottomFragment android.app.Fragment you must use "getFragmentManager" .

Just check your imports in BottomFragment and use proper FragmentManager.
0 manoj kumar · June 5, 2015
" getFragmentManager " solved the problem !!! Thanks dude !!
0 Rip Van Winkle · August 7, 2015
Thanks, Bartosz! That's the missing information I needed to get the MemeCreator example (from android tutorial lessons 22-30) to work.  After changing  "import android.support.v4.app.Fragment" to "import android.app.Fragment" in TopSectionFragment.java and BottomPictureFragment.java, I just needed to change "getSupportFragmentManager" to "getFragmentManager" and then it finally ran successfully.
0 Ghanshyam doifode · September 24, 2015
thanx dude..
Bartosz Maci?ga 
0 Zahid Khan · October 2, 2015
Thanx bro. it solved my problem too..
  • 1

Java / Android Development

107,033 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator