我从启动器中打开应用程序(使用导航组件),它(正确)在活动NavHost中显示了“开始目标”片段。当我按下Android的“后退”按钮时,将按预期返回到启动器。
但是,如果我随后打开应用程序切换器用户界面,尽管我可以在列表中看到该应用程序的任务预览,但是如果我尝试选择它,则会看到错误(举杯)“应用程序不可用”。因此,如果我使用“后退”键离开应用程序的“开始目标”,然后尝试返回到该应用程序,则会看到此错误。
活动分类:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val nav = findNavController(R.id.nav_host)
NavigationUI.setupActionBarWithNavController(this, nav)
}
override fun onSupportNavigateUp(): Boolean {
return Navigation.findNavController(this, R.id.nav_host).navigateUp()
}
活动布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
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=".ui.MainActivity">
<fragment
android:id="@+id/nav_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
导航图
<?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/nav_graph.xml"
app:startDestination="@id/dashboardFragment"
>
<fragment
android:id="@+id/dashboardFragment"
android:name="com.blah.DashboardFragment"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_dashboard"
>
<action
android:id="@+id/action_Dashboard_to_settingsFragment"
app:destination="@id/settingsFragment"
/>
<action
android:id="@+id/action_dashboardFragment_to_attendFragment"
app:destination="@id/attendFragment"
/>
<action
android:id="@+id/action_dashboardFragment_to_tutorialFragment"
app:destination="@id/tutorialFragment"
>
<argument
android:name="showSkipOption"
android:defaultValue="true"
app:argType="boolean"
/>
</action>
</fragment>
...
更新:我最初认为此错误与导航组件有关,但事实并非如此。在伊恩·莱克(Ian Lake)的评论的提示下,我完全删除了导航组件,并且错误仍然存在。
该应用在清单中使用“活动”别名:
<activity-alias
android:name=".Launcher"
android:targetActivity=".ui.MainActivity"
>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity-alias>
删除“活动”别名即可解决此问题。我应该提到,通过ADB从Android Studio启动应用程序时会发生该错误,因此看起来这可能是Android Studio v3.3的问题,该错误在于其处理活动别名的方式,因此我将报告一个错误。
在此错误的两个实例中,我看到了logcat条目不同,但显然是在错误发生时产生的:
场景1
W/ziparchive: Unable to open '/data/app/com.blah.debug-zJeRYvYpxcoUYWHwCip1sg==/base.apk': No such file or directory
E/s.nexuslaunche: Failed to open APK '/data/app/com.blah.debug-zJeRYvYpxcoUYWHwCip1sg==/base.apk' I/O error
E/ResourcesManager: failed to add asset path /data/app/com.blah.debug-zJeRYvYpxcoUYWHwCip1sg==/base.apk
W/PackageManager: Failure retrieving resources for com.blah.debug
场景2
W/SurfaceFlinger: Attempting to destroy on removed layer: AppWindowToken{6e5b26b token=Token{93243ba ActivityRecord{ee21ce5 u0 com.blah.debug/com.blah.Launcher t5941}}}#0
I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10100000 cmp=com.blah.debug/com.blah.ui.MainActivity} from uid 2000
W/ActivityManager: Permission Denial: starting Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10100000 cmp=com.blah.debug/com.blah.ui.MainActivity } from null (pid=-1, uid=2000) not exported from uid 10223
W/QuickScrubController: Failed to launch task (task=Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.blah.debug/com.blah.ui.MainActivity } userId=0)
我已经重命名了这个问题,并更新了标签以删除导航组件,因为那不是原因。