Type missmatch cannot convert from Fragment to fragment

user3584935

I'm getting this error at the moment:

"type missmatch cannot convert from FragmentA to fragment". 

Under each If statement

    if(arg0==0)
    {
        fragment=new FragmentA(); 
    }

if(arg0==0)
    {
        fragment=new FragmentB(); 
    }

if(arg0==0)
    {
        fragment=new FragmentC(); 
    }

So I'm wondering if it's something to do with my import files? As I keep having trouble with those. I'll list the Import files and then the relevant code. Could be either the method or the imports not to sure which.

Ok so here is the full code:

    import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBar.TabListener;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class Main_activity extends ActionBarActivity implements TabListener {



    public static final String PREFS_NAME = "MyPreferncesfile";

    ViewPager viewPager; 
    ActionBar actionBar; 
    @Override
    public void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_main);

        viewPager=(ViewPager) findViewById(R.id.pager);
        actionBar=getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab tab1=actionBar.newTab();
        tab1.setText("Tab 1");
        tab1.setTabListener(this); 

        ActionBar.Tab tab2=actionBar.newTab();
        tab2.setText("Tab 2");
        tab2.setTabListener(this); 

        ActionBar.Tab tab3=actionBar.newTab();
        tab3.setText("Tab 3");
        tab3.setTabListener(this); 

        actionBar.addTab(tab1);
        actionBar.addTab(tab2);
        actionBar.addTab(tab3);
     }
    public void onTabReselected1(Tab tab, FragmentTransaction ft) {

        Log.d("VIVZ", "onTabReselected at "+" position "+tab.getPosition()+" name "+tab.getText());


    }

    public void onTabSelected1(Tab tab, FragmentTransaction ft) {
        Log.d("VIVZ", "onTabSelected at "+" position "+tab.getPosition()+" name "+tab.getText());

    }

    public void onTabUnselected1(Tab tab, FragmentTransaction ft) {
        Log.d("VIVZ", "onTabUnselected at "+" position "+tab.getPosition()+" name "+tab.getText());

    }



    class MyAdapter extends FragmentPagerAdapter 
    {

        public MyAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }

    public Fragment getItem(int arg0) {
        Fragment fragment=null; 
        if(arg0==0)
        {
            fragment=new FragmentA(); 
        }
        if(arg0==1)
        {
            fragment=new FragmentB(); 
        }
        if(arg0==2) 
        {
            fragment=new FragmentA(); 
        }
        return fragment;
    }


        @Override
        public int getCount() {
            return 3;
        } 
    }
nobalG

Check the imports in all the classes in which you are extending fragments, as android.app.Fragment is different from android.support.v4.app.Fragment and hence the conflict

It may be happening because your FragmentA class may be extending Fragment (i.e may be importing) old android.app.Fragment and the one you posted is extending the fragment class from import android.support.v4.app.Fragment;


See this : Difference between android.app.Fragment and android.support.v4.app.Fragment

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Type mismatch: cannot convert from ListFragment to Fragment

From Dev

Type Type mismatch: cannot convert from RegisterFragment to Fragment

From Dev

Type missmatch: cannot convert from java.lang.String to String

From Dev

Type mismatch: cannot convert from FragmentOne to Fragment when create FragmentOne in seperate class?

From Dev

Cannot cast from Fragment to SupportMapFragment

From Dev

Cannot convert type 'Android.Support.V4.App.Fragment' to 'Android.Gms.Maps.MapFragment'

From Dev

Cannot convert type 'Android.Support.V4.App.Fragment' to 'Android.Gms.Maps.MapFragment'

From Dev

OnItemClickListener cannot be resolved to a type (inside Fragment)

From Dev

Error:Cannot cast from Fragment to FragmentB

From Dev

Saving Fragment State/Data from Fragment to Fragment

From Dev

Return variable From Fragment to Fragment

From Dev

Open Fragment from fragment error

From Dev

Fragment not launched from another fragment

From Dev

passing objects from Fragment A to Fragment B, then to Fragment C and then back to Fragment A

From Dev

Convert Activity to Fragment

From Dev

Tried to convert Activity to fragment

From Dev

How to convert this class to fragment

From Dev

Cannot cast View to <fragment>

From Dev

Cannot resolve "setVolumeControlStream" in a fragment

From Dev

CustomListViewAdapter cannot be applied in fragment

From Dev

Cannot add Fragment on android

From Dev

fragment transaction cannot be applied to

From Dev

OnClickListener cannot be called in Fragment

From Dev

Cannot reslove setDistributeEvenly() in Fragment

From Dev

Fragment cannot be converted to Context

From Dev

cannot be converted to Fragment

From Dev

HomeFragment cannot be converted to Fragment

From Dev

Cannot remove fragment

From Dev

CustomListViewAdapter cannot be applied in fragment

Related Related

  1. 1

    Type mismatch: cannot convert from ListFragment to Fragment

  2. 2

    Type Type mismatch: cannot convert from RegisterFragment to Fragment

  3. 3

    Type missmatch: cannot convert from java.lang.String to String

  4. 4

    Type mismatch: cannot convert from FragmentOne to Fragment when create FragmentOne in seperate class?

  5. 5

    Cannot cast from Fragment to SupportMapFragment

  6. 6

    Cannot convert type 'Android.Support.V4.App.Fragment' to 'Android.Gms.Maps.MapFragment'

  7. 7

    Cannot convert type 'Android.Support.V4.App.Fragment' to 'Android.Gms.Maps.MapFragment'

  8. 8

    OnItemClickListener cannot be resolved to a type (inside Fragment)

  9. 9

    Error:Cannot cast from Fragment to FragmentB

  10. 10

    Saving Fragment State/Data from Fragment to Fragment

  11. 11

    Return variable From Fragment to Fragment

  12. 12

    Open Fragment from fragment error

  13. 13

    Fragment not launched from another fragment

  14. 14

    passing objects from Fragment A to Fragment B, then to Fragment C and then back to Fragment A

  15. 15

    Convert Activity to Fragment

  16. 16

    Tried to convert Activity to fragment

  17. 17

    How to convert this class to fragment

  18. 18

    Cannot cast View to <fragment>

  19. 19

    Cannot resolve "setVolumeControlStream" in a fragment

  20. 20

    CustomListViewAdapter cannot be applied in fragment

  21. 21

    Cannot add Fragment on android

  22. 22

    fragment transaction cannot be applied to

  23. 23

    OnClickListener cannot be called in Fragment

  24. 24

    Cannot reslove setDistributeEvenly() in Fragment

  25. 25

    Fragment cannot be converted to Context

  26. 26

    cannot be converted to Fragment

  27. 27

    HomeFragment cannot be converted to Fragment

  28. 28

    Cannot remove fragment

  29. 29

    CustomListViewAdapter cannot be applied in fragment

HotTag

Archive