所以我试图用底部导航制作一个简单的应用程序,但我有点卡在片段的东西上。
我这些错误:
error: incompatible types: berandaFragment cannot be converted to Fragment
error: incompatible types: beritaFragment cannot be converted to Fragment
error: incompatible types: radioFragment cannot be converted to Fragment
error: incompatible types: kegiatanFragment cannot be converted to Fragment
这是我的 MainActivity.java
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.FrameLayout;
public class MainActivity extends AppCompatActivity {
private BottomNavigationView mMenuUtama;
private FrameLayout mFrameUtama;
private berandaFragment berandaFragmentMenu;
private beritaFragment beritaFragmentMenu;
private radioFragment radioFragmentMenu;
private kegiatanFragment kegiatanFragmentMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFrameUtama = (FrameLayout) findViewById(R.id.frame_utama);
mMenuUtama = (BottomNavigationView) findViewById((R.id.menu));
berandaFragmentMenu = new berandaFragment();
beritaFragmentMenu = new beritaFragment();
radioFragmentMenu = new radioFragment();
kegiatanFragmentMenu = new kegiatanFragment();
mMenuUtama.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_beranda :
setFragment(berandaFragmentMenu);
return true;
case R.id.menu_berita :
setFragment(beritaFragmentMenu);
return true;
case R.id.menu_radio :
setFragment(radioFragmentMenu);
return true;
case R.id.menu_kegiatan :
setFragment(kegiatanFragmentMenu);
return true;
default:
return false;
}
}
});
}
public void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_utama, fragment);
fragmentTransaction.commit();
};}
我是初学者,如果发现解决方案非常简单,请见谅。我实际上是按照一个教程来做这个的,我遵循了每一步,但是我得到了这些错误,所以我很困惑。
之前谢谢你。