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

Android Studio 執行錯誤:Intent-filter 重複導致啟動失敗

     編輯:程式設計疑難解答

問題描述

Android 開發新手在嘗試頁面跳轉時遭遇執行錯誤,Logcat 顯示活動啟動異常。專案中定義了兩個 Activity,且兩者在 AndroidManifest.xml 中都宣告了 <intent-filter> 包含 MAIN 與 LAUNCHER 動作。

錯誤原因

<intent-filter> 標籤僅能存在於**單一**作為應用程式進入點的 Activity 中。若多個 Activity 同時宣告 android.intent.action.MAIN 與 android.intent.category.LAUNCHER,系統無法判斷真正的啟動頁面,導致編譯或執行階段失敗。

解決方法

  1. 打開 AndroidManifest.xml。
  2. 檢查所有 <activity> 標籤,確保**僅有主啟動 Activity**(通常是 MainActivity)包含以下區塊:
<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
  1. 移除其他 Activity 中相同的 <intent-filter> 區塊。
  2. 重新建構專案 (Build → Rebuild Project) 並執行。

補充說明

  • 次要 Activity 若需從主頁面跳轉,僅需在程式碼中使用 Intent 明確指定目標類別即可,無需在 Manifest 宣告啟動過濾器。
  • 若專案有多個模組,請確認各模組 Manifest 合併後不會產生重複的 LAUNCHER 宣告。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved