打开 AAR 时,您需要在 Flutter 中使用 Theme.AppCompat 主题(或后代)与此 Activity

问题描述 投票:0回答:1

当我尝试从 Flutter 端打开 AAR 模块时遇到问题。我已经实现了方法通道函数来打开 AAR 的 StartActivity,但是当我尝试这样做时,它给了我这个错误:

E/AndroidRuntime(32503): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.appName.devFlavor/com.example.app.StartPageActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
E/AndroidRuntime(32503):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3782)
E/AndroidRuntime(32503):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3922)
E/AndroidRuntime(32503):        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
E/AndroidRuntime(32503):        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139)
E/AndroidRuntime(32503):        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96)
E/AndroidRuntime(32503):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443)
E/AndroidRuntime(32503):        at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime(32503):        at android.os.Looper.loopOnce(Looper.java:205)
E/AndroidRuntime(32503):        at android.os.Looper.loop(Looper.java:294)
E/AndroidRuntime(32503):        at android.app.ActivityThread.main(ActivityThread.java:8177)
E/AndroidRuntime(32503):        at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(32503):        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
E/AndroidRuntime(32503):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
E/AndroidRuntime(32503): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
E/AndroidRuntime(32503):        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:852)
E/AndroidRuntime(32503):        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:815)
E/AndroidRuntime(32503):        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:703)
E/AndroidRuntime(32503):        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
E/AndroidRuntime(32503):        at com.example.app.StartPageActivity.onCreate(StartPageActivity.java:17)
E/AndroidRuntime(32503):        at android.app.Activity.performCreate(Activity.java:8595)
E/AndroidRuntime(32503):        at android.app.Activity.performCreate(Activity.java:8573)
E/AndroidRuntime(32503):        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1456)
E/AndroidRuntime(32503):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3764)

所以我确实注意到 Flutter 在 styles.xm 中使用了不同的主题,我决定使用 AppCompat,但我仍然无法打开 AAR 的 StartActivity 意图。

这是我的值/styles.xml

    <style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
        <item name="android:forceDarkAllowed">false</item>
        <item name="android:windowFullscreen">false</item>
        <item name="android:windowDrawsSystemBarBackgrounds">false</item>
        <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
    </style> 

    <style name="AppFullScreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>

这就是我从方法通道打开意图的方式 我的 MainActivity.kt 文件:

package com.company.appName

import android.content.Intent
import io.flutter.embedding.android.FlutterActivity
import androidx.annotation.NonNull
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import android.content.Context

class MainActivity: FlutterActivity() {
     private val CHANNEL = "com.company.appName/aarchannel"
     private val AARMETHOD = "aarcall"

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(
            flutterEngine.dartExecutor.binaryMessenger,
            CHANNEL
        ).setMethodCallHandler { call, result ->
            if (call.method == AARMETHOD) {
                openAAR(context,result)
            } else {
                result.notImplemented()
            }
        }
}

 private fun openAAR(context: Context, result : MethodChannel.Result){
                val intent = Intent(context, com.example.app.StartPageActivity::class.java)
                startActivity(intent)
                result.success(true)
  }

}

com.example.app 是 AAR 的包名称。它实际上找到了包,但是当我尝试从 Flutter 端作为意图打开时,它给出了我上面提到的错误。

我可以做些什么来解决 Appcompat 问题吗?

android flutter aar
1个回答
0
投票

Theme.AppCompat.Light.NoActionBar、Theme.AppCompat.DayNight.NoActionBarTheme.MaterialComponentsAndroidX 或 Material Components 库的一部分。您需要确保这些库包含在您的 android/app/build.gradle 文件中。

在依赖项块下添加或更新依赖项:

dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1' // Ensure you have AppCompat dependency
implementation 'com.google.android.material:material:1.9.0' // Material Components dependency

}

© www.soinside.com 2019 - 2024. All rights reserved.