Why is the error, in this fragment?

1

Good morning classmates, I am working with fragments because I am implementing a TabHost, but it seems that many things could fit when working with the fragments, I got the error that appears in the following image

implement the fragment as follows

because it marked me error in the class where I call this fragment I thank you in advance for your help.

Description of the error

Description 2

public class MainActivity extends AppCompatActivity {


private SectionsPagerAdapter mSectionsPagerAdapter;


private ViewPager mViewPager;

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu_inicio, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.ContextMenuLogout:
            Intent i = new Intent(MainActivity.this, Login.class);
            SharedPreferences preferences = getSharedPreferences("DatosDelLogin", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.clear();
            editor.commit();

            startActivity(i);
            finish();
    }

    return super.onOptionsItemSelected(item);
}


public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        switch (position){
            case 0:
                Concesionarios Tab1 = new Concesionarios();
                return Tab1;

            default:
                return null;
        }///poner los vinculos a las clases//------------------------------------
    }

New error

Error resolved

    
asked by Alexis Caballero 22.03.2017 в 17:33
source

2 answers

2

The class Concesionarios is a Fragment therefore the context is obtained by getActivity() which is the Activity containing the Fragment , in fact it is what the message indicates, the first parameter must be the Activity, the second an ArrayList of ItemCompanies:

AdapterEmpresas(AppCompatActivity, ArrayList<ItemEmpresas>)

you must ensure using the context of the Activity:

//myAdapterEmp = new AdapterEmpresas(this, myItemEmp);
myAdapterEmp = new AdapterEmpresas(getActivity(), myItemEmp);
    
answered by 22.03.2017 / 17:47
source
0

Do it this way:

In your Dealer class:

public class Concesionarios extends fragment{
}

And you import in that class android.support.v4.app.Fragment

You have to use the same import fragment, in all classes that inherit from Fragment

To call the Fragment you have to use:

getActivity().getSupportFragmentManager();

If your imported class was android.app.Fragment You would not need it:

getActivity().getSupportFragmentManager()

You could directly with a

getActivity().getFragmentManager()

I hope it serves you!

    
answered by 22.03.2017 в 17:46