• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Strange error with fragments

I had just finished getting all the fragments together for the drawer and I was trying to get them going to the right fragment locations. When I defined the fragment, it gave me a strange error.

Code:
public boolean onNavigationItemSelected(MenuItem item)
    {
        Fragment fragment=null;
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.oil_changes)
        {
            fragment=new OilFragment(); when I specified this it said it was an incompatible type. The example I went through didn't have the same issue. The only way to make the compiler happy is to OilFragment oilFragment=new OilFragment(); Not sure if it would work if converted.
the error said it required android.support.v4.app.fragment, but found com.domain.appname.OilFragment

The package section is correct.

The import section:
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

        } else if (id == R.id.battery)
        {

        } else if (id == R.id.brakes)
        {

        } else if (id == R.id.muffler)
        {

        } else if (id == R.id.plugs_wires)
        {

        } else if (id == R.id.tires)
        {

        }
        else if (id == R.id.showCustom)
        {

        }else if (id == R.id.eos)
        {

        }

        if(fragment != null)
        {
            FragmentManager fragmentManager=getSupportFragmentManager();
            FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.screen_area, fragment);
            fragmentTransaction.commit();
        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;

Thanks for the help!

    }
 
How have you defined class OilFragment?

You can't declare variable 'fragment' as

Code:
Fragment fragment;

but then assign it to a different type

Code:
fragment = new OilFragment();

Unless OilFragment class extends Fragment

Code:
import android.support.v4.app.Fragment;
...
public class OilFragment extends Fragment 
{
 ...
}
 
How have you defined class OilFragment?

You can't declare variable 'fragment' as

Code:
Fragment fragment;

but then assign it to a different type

Code:
fragment = new OilFragment();

Unless OilFragment class extends Fragment

Code:
import android.support.v4.app.Fragment;
...
public class OilFragment extends Fragment
{
 ...
}
How have you defined class OilFragment?

You can't declare variable 'fragment' as

Code:
Fragment fragment;

but then assign it to a different type

Code:
fragment = new OilFragment();

Unless OilFragment class extends Fragment

Code:
import android.support.v4.app.Fragment;
...
public class OilFragment extends Fragment
{
 ...
}
There was a mismatch of the libraries imported. I was using
android.app.Fragment on a bunch of them rather than
android.support.v4.app.Fragment; Thanks!
 
Back
Top Bottom