空 MainActivity ,导航不工作?
Empty MainActivity , Navigation not working?
我已阅读文档、关注 CodeLab 并观看了 youtube 教程,但每当我自己尝试 导航 时,都会出现一些或其他问题,
这次 我的 HomeFragment 没有托管在 NavHost 中,我在启动应用程序时看到一个空白屏幕。
我相信我有一个正确的 NavGraph
,NavHost
但我还没有导航,所以没有 NavController
。
这是我的文件和 类:-
MainActivity.kt
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
}
activity_main.xml
<LinearLayout 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">
<fragment
android:id="@+id/myNavHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation" />
</LinearLayout>
navigation.xml
<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/navigation"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/appInfoFragment"
android:name="com.example.themaze.AppInfoFragment"
android:label="fragment_app_info"
tools:layout="@layout/fragment_app_info" />
<fragment
android:id="@+id/homeFragment"
android:name="com.example.themaze.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" >
<action
android:id="@+id/action_homeFragment_to_appInfoFragment"
app:destination="@id/appInfoFragment" />
</fragment>
</navigation>
fragment_home.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeFragment"
android:background="@color/teal_200">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/size"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Home"
android:textSize="34sp" />
</FrameLayout>
如果我得到答案,那将是一个很大的帮助,因为我花了一个多小时来解决我认为我知道的主题的问题...
我觉得是你的问题activity_main.xml。请使用 FragmentContainerView 来托管您的片段,而不是使用片段。像这样,
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
有关详细信息,请查看 https://developer.android.com/guide/navigation/navigation-getting-started#add-navhost
我已阅读文档、关注 CodeLab 并观看了 youtube 教程,但每当我自己尝试 导航 时,都会出现一些或其他问题,
这次 我的 HomeFragment 没有托管在 NavHost 中,我在启动应用程序时看到一个空白屏幕。
我相信我有一个正确的 NavGraph
,NavHost
但我还没有导航,所以没有 NavController
。
这是我的文件和 类:-
MainActivity.kt
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
}
activity_main.xml
<LinearLayout 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">
<fragment
android:id="@+id/myNavHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation" />
</LinearLayout>
navigation.xml
<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/navigation"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/appInfoFragment"
android:name="com.example.themaze.AppInfoFragment"
android:label="fragment_app_info"
tools:layout="@layout/fragment_app_info" />
<fragment
android:id="@+id/homeFragment"
android:name="com.example.themaze.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" >
<action
android:id="@+id/action_homeFragment_to_appInfoFragment"
app:destination="@id/appInfoFragment" />
</fragment>
</navigation>
fragment_home.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeFragment"
android:background="@color/teal_200">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/size"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Home"
android:textSize="34sp" />
</FrameLayout>
如果我得到答案,那将是一个很大的帮助,因为我花了一个多小时来解决我认为我知道的主题的问题...
我觉得是你的问题activity_main.xml。请使用 FragmentContainerView 来托管您的片段,而不是使用片段。像这样,
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
有关详细信息,请查看 https://developer.android.com/guide/navigation/navigation-getting-started#add-navhost