簡體   English   中英

Flutter : local_auth: ^0.6.1 : PlatformException(error, You need to use a Theme.AppCompat theme (or后代) with this Activity., null)

[英]Flutter : local_auth: ^0.6.1 : PlatformException(error, You need to use a Theme.AppCompat theme (or descendant) with this activity., null)

我在使用包Local Auth集成我的應用程序時遇到問題,我已經按照有關配置包到我的應用程序的文檔進行操作。 在文檔中它說:

請注意, local_auth插件需要使用 FragmentActivity 而不是 Activity。 這可以通過切換到在清單中使用 FlutterFragmentActivity 而不是 FlutterActivity 來輕松完成(如果您要擴展基類,則使用您自己的 Activity 類)。

但是在我編輯MainActivity.kt后問題出現了:

package id.zeffry.debt_diary

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

import android.os.Build
import android.view.ViewTreeObserver
import android.view.WindowManager
class MainActivity: FlutterFragmentActivity() {
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }
}

日志

I/flutter (22608): PlatformException(error, You need to use a Theme.AppCompat theme (or descendant) with this activity., null)

來自文檔的源代碼:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:local_auth/local_auth.dart';

//? Testing Local Auth( Finger Print , Face Recognition, etc)
class TestingPage extends StatefulWidget {
  static const routeNamed = "/testing-page";

  @override
  _TestingPageState createState() => _TestingPageState();
}

class _TestingPageState extends State<TestingPage> {
  final LocalAuthentication auth = LocalAuthentication();

  bool _canCheckBiometrics;

  List<BiometricType> _availableBiometrics;

  String _authorized = 'Not Authorized';

  bool _isAuthenticating = false;

  Future<void> _checkBiometrics() async {
    bool canCheckBiometrics;
    try {
      canCheckBiometrics = await auth.canCheckBiometrics;
    } on PlatformException catch (e) {
      print(e);
    }
    if (!mounted) return;

    setState(() {
      _canCheckBiometrics = canCheckBiometrics;
    });
  }

  Future<void> _getAvailableBiometrics() async {
    List<BiometricType> availableBiometrics;
    try {
      availableBiometrics = await auth.getAvailableBiometrics();
    } on PlatformException catch (e) {
      print(e);
    }
    if (!mounted) return;

    setState(() {
      _availableBiometrics = availableBiometrics;
    });
  }

  Future<void> _authenticate() async {
    bool authenticated = false;
    try {
      setState(() {
        _isAuthenticating = true;
        _authorized = 'Authenticating';
      });
      authenticated = await auth.authenticateWithBiometrics(
          localizedReason: 'Scan your fingerprint to authenticate',
          useErrorDialogs: true,
          stickyAuth: true);
      setState(() {
        _isAuthenticating = false;
        _authorized = 'Authenticating';
      });
    } on PlatformException catch (e) {
      print(e);
    }
    if (!mounted) return;

    final String message = authenticated ? 'Authorized' : 'Not Authorized';
    setState(() {
      _authorized = message;
    });
  }

  void _cancelAuthentication() {
    auth.stopAuthentication();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ConstrainedBox(
        constraints: const BoxConstraints.expand(),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            Text('Can check biometrics: $_canCheckBiometrics\n'),
            RaisedButton(
              child: const Text('Check biometrics'),
              onPressed: _checkBiometrics,
            ),
            Text('Available biometrics: $_availableBiometrics\n'),
            RaisedButton(
              child: const Text('Get available biometrics'),
              onPressed: _getAvailableBiometrics,
            ),
            Text('Current State: $_authorized\n'),
            RaisedButton(
              child: Text(_isAuthenticating ? 'Cancel' : 'Authenticate'),
              onPressed:
                  _isAuthenticating ? _cancelAuthentication : _authenticate,
            )
          ],
        ),
      ),
    );
  }
}

來自真實設備的更新錯誤:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
    at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:686)
    at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:649)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:542)
    at androidx.appcompat.app.AppCompatDialog.setContentView(AppCompatDialog.java:95)
    at androidx.appcompat.app.AlertController.installContent(AlertController.java:232)
    at androidx.appcompat.app.AlertDialog.onCreate(AlertDialog.java:279)
    at android.app.Dialog.dispatchOnCreate(Dialog.java:389)
    at android.app.Dialog.show(Dialog.java:293)
    at androidx.fragment.app.DialogFragment.onStart(DialogFragment.java:486)
    at androidx.fragment.app.Fragment.performStart(Fragment.java:2627)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:915)
    at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
    at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
    at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2076)
    at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1866)
    at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1821)
    at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
    at androidx.fragment.app.FragmentManagerImpl.executePendingTransactions(FragmentManagerImpl.java:183)
    at androidx.biometric.BiometricPrompt.authenticateInternal(BiometricPrompt.java:749)
    at androidx.biometric.BiometricPrompt.authenticate(BiometricPrompt.java:658)
    at io.flutter.plugins.localauth.AuthenticationHelper$1.run(AuthenticationHelper.java:182)
    at android.os.Handler.handleCallback(Handler.java:754)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:165)
    at android.app.ActivityThread.main(ActivityThread.java:6375)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)

如果您收到此錯誤:

Exception has occurred. PlatformException (PlatformException(error, You need to use a Theme.AppCompat theme (or descendant) with this activity., null))

然后你需要這樣做:

  1. 轉到android > app > src > main > res > values > style.xml

  2. 改變

<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">

<style name="LaunchTheme" parent="Theme.AppCompat.Light.NoActionBar">

有關在 Kotlin 中設置MainActivity.kt更多信息; 訪問這里: https : //gist.github.com/akifarhan/f70a2c777651f2ea61a15eb92a5939c1

改變

<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">

在文件res/style.xml中的 android 文件夾中

<style name="LaunchTheme" parent="@android:style/Theme.AppCompat.Black.NoTitleBar">

將主題更改為 AppCompat 后,顯示的視圖會出現問題 btw 啟動畫面和第一個小部件(通常與啟動畫面相同)。 現在有一個小的彈出 btw 兩個圖像,這非常煩人,我不得不刪除這個超級功能並保持與 iOS 上相同的丑陋邏輯,並看到白屏 btw 啟動主題和第一個小部件:(

 <meta-data
            android:name="io.flutter.embedding.android.SplashScreenDrawable"
            android:resource="@drawable/launch_background"
            />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM