Problem with the fragments in android

0

Good afternoon friends, get to the point:

public class MainActivity extends AppCompatActivity {


 Fragment currentFragment;

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu,menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

      switch (item.getItemId()){

          case R.id.mwelcome:
                  this.currentFragment = new  WelcomeFragment();
              break;
                  this.currentFragment = new MapFragment();
          case R.id.map:

              break;

      }

    return super.onOptionsItemSelected(item);
}

}

Well I tell you I get an error in the following two lines:

  this.currentFragment = new  WelcomeFragment();

and also in

this.currentFragment = new MapFragment();

Just in case MapFragment and WelcomeFragment have them there are these codes:

MapFragment:

public class MapFragment extends Fragment {


public MapFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_map, container, false);
}

}

WelcomeFragment:

public class WelcomeFragment extends Fragment {


public WelcomeFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_welcome, container, false);
    }

 }

Please help me the error that comes to me is:

Incompatible types.
Required:
android.support.v4.app.Fragment
Found:
com.example.david.udemy_09_maps.fragments.MapFragment

Thank you very much friends I hope your help:).

    
asked by Ijsud 16.09.2018 в 00:40
source

1 answer

0

Fragment currentFragment is a android.support.v4.app.Fragment but the fragments that you want to assign to it are of type com.example.david.udemy_09_maps.fragments.MapFragment are different classes use android.support.v4.app.Fragment for all your fragments and you will not have problem

    
answered by 16.09.2018 / 21:54
source