簡體   English   中英

將底部導航活動轉換為片段

[英]Convert Bottom Navigation Activity to Fragment

我試圖將android底部導航活動用作片段,但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();
}

主要活動布局

 <?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)

您應該將一些代碼從公共視圖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);
}

嘗試這個。 無論如何,您不應該避免使用新的Google導航組件,請嘗試遵循Google文檔。 閱讀

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM