不兼容的类型无法转换为 Fragment
Incompatible types cannot be converted to Fragment
所以我正在尝试制作一个带有底部导航的简单应用,但我有点受困于 Fragment。
我这些错误:
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();
};}
我是初学者,如果发现解决方案很简单,请见谅。我实际上是按照一个教程来做这个的,我也跟着每一步做了,但是我遇到了那些错误,所以我很困惑。
先谢谢你了
这可能是因为您导入了 fragment 的应用版本,而不是框架或 v4 版本。您已将 Activity 定义为扩展 AppCompat
,这是支持框架的一部分。因此,此 Activity 托管的所有片段都必须来自 android.support.v4.app.Fragment
的支持框架。您可以使 activity 扩展简单 Activity
或将您的片段转换为 v4 类型。此决定将取决于您的目标设备级别。
本文详细讨论了支持框架:
http://martiancraft.com/blog/2015/06/android-support-library/
所以我正在尝试制作一个带有底部导航的简单应用,但我有点受困于 Fragment。
我这些错误:
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();
};}
我是初学者,如果发现解决方案很简单,请见谅。我实际上是按照一个教程来做这个的,我也跟着每一步做了,但是我遇到了那些错误,所以我很困惑。
先谢谢你了
这可能是因为您导入了 fragment 的应用版本,而不是框架或 v4 版本。您已将 Activity 定义为扩展 AppCompat
,这是支持框架的一部分。因此,此 Activity 托管的所有片段都必须来自 android.support.v4.app.Fragment
的支持框架。您可以使 activity 扩展简单 Activity
或将您的片段转换为 v4 类型。此决定将取决于您的目标设备级别。
本文详细讨论了支持框架:
http://martiancraft.com/blog/2015/06/android-support-library/