Android Studio添加Deep Link后出现Activity相关报错求助
问题描述
在Android Studio 2022.3.1中开发了一个仅包含WebView的URL处理应用,原本运行正常,添加基于Intent的Deep Link后出现问题:
- 首次报错:
Unable to determine activity name - 给AndroidManifest.xml添加LAUNCHER intent-filter后,新报错:
Activity class {com.app.myapp/com.app.myapp.MainActivity} does not exist
多次重新搭建项目并参考示例仍未解决。
相关代码
AndroidManifest.xml(初始版本)
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" > <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.FullscreenDark" tools:targetApi="31"> <activity android:name=".MainActivity" android:exported="true" android:theme="@style/Theme.FullscreenDark"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:host="@string/app_host" /> <data android:scheme="http" /> <data android:scheme="https" /> </intent-filter> </activity> </application> </manifest>
MainActivity.kt
package com.app.myapp import android.annotation.SuppressLint import android.os.Bundle import android.webkit.WebSettings import android.webkit.WebView import android.webkit.WebViewClient import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { private lateinit var webView: WebView @SuppressLint("SetJavaScriptEnabled") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val host = getString(R.string.app_host) webView = findViewById(R.id.webview) val webSettings: WebSettings = webView.settings // Configure settings webSettings.javaScriptEnabled = true // Enable JavaScript webView.webViewClient = MyWebViewClient() // webView.webViewClient = WebViewClient() // Check for deep link val intentData = intent.data if (intentData != null) { val deepLink = intentData.toString() webView.loadUrl(deepLink) } else { // Load your default URL webView.loadUrl("https://$host") } } @Deprecated("Deprecated in Java") override fun onBackPressed() { if (webView.canGoBack()) { webView.goBack() } else { super.onBackPressedDispatcher.onBackPressed() } } private inner class MyWebViewClient : WebViewClient() { @Deprecated("Deprecated in Java") override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean { val host = getString(R.string.app_host) // Load URLs from the same domain within the WebView if (url != null && url.startsWith("https://$host")) { view?.loadUrl(url) return true } // Load all other URLs in the default browser return false } } }
修改后的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" > <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.FullscreenDark" tools:targetApi="31"> <activity android:name=".MainActivity" android:exported="true" android:theme="@style/Theme.FullscreenDark"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:host="@string/app_host" /> <data android:scheme="http" /> <data android:scheme="https" /> </intent-filter> </activity> </application> </manifest>
内容的提问来源于stack exchange,提问作者WebDevGuy
相关产品推荐
相关产品推荐

