底部导航在安卓喷气背包导航图中不显示片段



我有两个活动的应用程序,一个是登录,注册和...片段,如果用户注册,它会转到另一个活动,该活动包含底部导航和 5 个片段进行切换。问题是第一个活动工作正常,但是当我午餐第二个活动时,底部导航显示并工作,但片段的内容没有显示! 这是我的导航图:

<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/main_nav_graph"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.prototype.nyx.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" >
<action
android:id="@+id/action_homeFragment_to_walletFragment"
app:destination="@id/walletFragment" />
</fragment>
<fragment
android:id="@+id/walletFragment"
android:name="com.prototype.nyx.WalletFragment"
android:label="fragment_wallet"
tools:layout="@layout/fragment_wallet" >
<action
android:id="@+id/action_walletFragment_to_addProductFragment"
app:destination="@id/addProductFragment" />
</fragment>
<fragment
android:id="@+id/addProductFragment"
android:name="com.prototype.nyx.AddProductFragment"
android:label="fragment_add_product"
tools:layout="@layout/fragment_add_product" >
<action
android:id="@+id/action_addProductFragment_to_settingsFragment"
app:destination="@id/settingsFragment" />
</fragment>
<fragment
android:id="@+id/settingsFragment"
android:name="com.prototype.nyx.SettingsFragment"
android:label="fragment_settings"
tools:layout="@layout/fragment_settings" >
<action
android:id="@+id/action_settingsFragment_to_profileFragment"
app:destination="@id/profileFragment" />
</fragment>
<fragment
android:id="@+id/profileFragment"
android:name="com.prototype.nyx.ProfileFragment"
android:label="fragment_profile"
tools:layout="@layout/fragment_profile" />
</navigation>

这是我的主要活动及其布局:

package com.prototype.nyx;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.NavigationUI;
import android.os.Bundle;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpNavigation();
}
public void setUpNavigation() {
bottomNavigationView = findViewById(R.id.bottom_nav);
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager()
.findFragmentById(R.id.main_nav_host_fragment);
System.out.println("number of fragments is:"+navHostFragment.getChildFragmentManager()
.getFragments().size());
NavigationUI.setupWithNavController(bottomNavigationView,
navHostFragment.getNavController());
}
}
<?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"
android:background="@color/white"
tools:context=".MainActivity">
<fragment
android:id="@+id/main_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:navGraph="@navigation/main_nav_graph" />

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>

和菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@id/homeFragment"
android:icon="@drawable/ic_home"
app:showAsAction="always"
android:title="">
</item>

<item
android:id="@id/walletFragment"
android:icon="@drawable/ic_wallet"
app:showAsAction="always"
android:title="">
</item>
<item
android:id="@id/addProductFragment"
android:icon="@drawable/ic_add"
app:showAsAction="always"
android:title="">
</item>
<item
android:id="@+id/settingsFragment"
android:icon="@drawable/ic_settings"
app:showAsAction="always"
android:title="">
</item>
<item
android:id="@id/profileFragment"
android:icon="@drawable/ic_person_black_24dp"
app:showAsAction="always"
android:title="">
</item>
</menu>

起初我认为这是主要的活动布局,所以我也尝试了框架布局和协调器布局。 但没有任何效果。 我的片段之一:

<?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=".HomeFragment">
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="256dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.063"
tools:src="@tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>

问题出在哪里?

尝试在导航图中检查片段的包名和片段名称attr

相关内容

  • 没有找到相关文章

最新更新