gpt4 book ai didi

java - 将底部导航 Activity 转换为 Fragment

转载 作者:行者123 更新时间:2023-12-02 00:51:35 24 4
gpt4 key购买 nike

我尝试使用 android 底部导航 Activity 作为 fragment ,但 Android Studio 3.5 使用新的 NavController 和 NavigationUI,这让我很难弄清楚它是如何工作的并返回运行时错误。

public class BottomFragment extends Fragment {

Activity activity;

public BottomFragment(Activity activity) {
this.activity = activity;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.activity_bottom_fragment, container, false);
BottomNavigationView navView = root.findViewById(R.id.nav_view);

AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
.build();

NavController navController = Navigation.findNavController(activity, R.id.nav_host_fragment);//error here
NavigationUI.setupActionBarWithNavController((AppCompatActivity) activity, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);


return root;
}}

主类

public class MainActivity extends AppCompatActivity {

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


getSupportFragmentManager().beginTransaction()
.replace(R.id.mainContainer,new BottomFragment(this)).commit();
}

主要 Activity 布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<FrameLayout
android:id="@+id/mainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

运行时错误

java.lang.IllegalArgumentException: ID does not reference a View inside this Activity
at android.app.Activity.requireViewById(Activity.java:2678)
at androidx.core.app.ActivityCompat.requireViewById(ActivityCompat.java:363)
at androidx.navigation.Navigation.findNavController(Navigation.java:58)
at com.h_byk.test000.BottomFragment.onCreateView(BottomFragment.java:36)

最佳答案

您应该将公共(public) View onCreateView 中的一些代码放置到 onActivityCreated 函数中。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.activity_bottom_fragment, container, false);
return root;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

BottomNavigationView navView = root.findViewById(R.id.nav_view);

AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
.build();

NavController navController = Navigation.findNavController(activity, R.id.nav_host_fragment);//error here
NavigationUI.setupActionBarWithNavController((AppCompatActivity) activity, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}

试试这个。无论如何,您不应该避免使用新的谷歌导航组件,请尝试遵循谷歌文档。阅读 this .

关于java - 将底部导航 Activity 转换为 Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57853628/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com