我是kotlin的新手我正在我的应用程序中添加底栏
val bottomNavigationView = findViewById<View>(R.id.navigation) as BottomNavigationView
BottomNavigationViewHelper.removeShiftMode(bottomNavigationView)
bottomNavigationView.setOnNavigationItemSelectedListener { item ->
var selectedFragment: Fragment? = null
when (item.itemId) {
R.id.action_item1 -> selectedFragment = ItemOneFragment.newInstance()
R.id.action_item2 -> selectedFragment = ItemTwoFragment.newInstance()
R.id.action_item3 -> selectedFragment = ItemThreeFragment.newInstance()
R.id.action_item4 -> selectedFragment = ItemThreeFragment.newInstance()
}
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.frame_layout, selectedFragment)
transaction.commit()
true
}
//Manually displaying the first fragment - one time only
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.frame_layout, ItemOneFragment.newInstance())
transaction.commit()
ItemOneFragment.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ItemOneFragment extends Fragment {
public static ItemOneFragment newInstance() {
ItemOneFragment fragment = new ItemOneFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_item_one, container, false);
}}
但我收到错误: 类型不匹配必需:ItemOneFragment.newInstance()处的片段 我尝试了所有的东西 提前致谢。
答案 0 :(得分:1)
您似乎要在包含android.app.Fragment
代码的文件中导入bottomNavigationView
。 ItemOneFragment.newInstance()
会返回android.support.v4.app.Fragment
,这与android.app.Fragment
不兼容。
将导入更改为android.support.v4.app.Fragment
可以解决问题。