首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

Android Manifest 拼寫錯誤導致 ActivityNotFoundException 完整排查

     編輯:程式設計疑難解答

錯誤現象

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" ... />

完整修正後 Manifest

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

DrawerLayout ListView 無法滑動問題

原代碼中 ListView 直接放在 DrawerLayout 內,未包裹在 ScrollView 或設定正確佈局參數。

修正佈局 XML

<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, acitivityXML 格式化後逐行核對
Package 名稱一致性Java 包名與 Manifest package 不符用 . 相對路徑簡化
Activity 類別存在類別重命名後 Manifest 未同步Android Studio 重構自動同步
Intent 呼叫正確setClass(this, Activity1.class) 正確檢查 import 是否正確

進階:使用 Navigation Component 避免手寫 Intent

// navigation_graph.xml
<fragment android:id="@+id/activity1Fragment" ... />

-- 程式碼 --
findNavController().navigate(R.id.activity1Fragment)
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved