Logcat 顯示 ActivityNotFoundException: Unable to find explicit activity class,但 Manifest 明明已宣告該 Activity。
<!-- 錯誤:actiivty (多一個 i) -->
<actiivty android:name="com.example.dell_pc.bigtest1.Activity1" ... />
<actiivty android:name="com.example.dell_pc.bigtest1.Activity2" ... />
<actiivty android:name="com.example.dell_pc.bigtest1.Activity3" ... />
<!-- 正確:activity -->
<activity android:name="com.example.dell_pc.bigtest1.Activity4" ... />
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.dell_pc.bigtest1">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 統一修正為 activity -->
<activity android:name=".Activity1" android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".Activity2" android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".Activity3" android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".Activity4" android:theme="@style/AppTheme.NoActionBar" />
</application>
</manifest>
原代碼中 ListView 直接放在 DrawerLayout 內,未包裹在 ScrollView 或設定正確佈局參數。
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 主內容 -->
<FrameLayout android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- 側邊欄:包裹在 ScrollView 中 -->
<ScrollView
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start">
<ListView
android:id="@+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</ScrollView>
</androidx.drawerlayout.widget.DrawerLayout>
| 檢查項目 | 常見錯誤 | 驗證方式 |
|---|---|---|
| Manifest 標籤拼寫 | actiivty, acitivity | XML 格式化後逐行核對 |
| Package 名稱一致性 | Java 包名與 Manifest package 不符 | 用 . 相對路徑簡化 |
| Activity 類別存在 | 類別重命名後 Manifest 未同步 | Android Studio 重構自動同步 |
| Intent 呼叫正確 | setClass(this, Activity1.class) 正確 | 檢查 import 是否正確 |
// navigation_graph.xml
<fragment android:id="@+id/activity1Fragment" ... />
-- 程式碼 --
findNavController().navigate(R.id.activity1Fragment)