简体   繁体   English

由于插件未自动附加到活动,Flutter Image Picker 无法正常工作

[英]Flutter Image Picker not working due to plugins not automatically attaching to activity

I have added flutter in an existing Android app and I have been facing issues with Image picker library not working, showing the following error.我在现有的 Android 应用程序中添加了颤振,我一直面临图像选择器库无法正常工作的问题,显示以下错误。

Unhandled Exception: MissingPluginException(No implementation found for method pickImage on channel plugins.flutter.io/image_picker_android)

According to the error I understand that it's due to GeneratedPluginRegistrant not being registered and I have to override configureFlutterEngine method.根据错误,我知道这是由于 GeneratedPluginRegistrant 未注册,我必须重写configureFlutterEngine方法。 As I need to open the flutter module inside a fragment I cannot extend my activity using FlutterActivity because it does not support getSupportFragmentManager .由于我需要在片段中打开颤振模块,因此我无法使用FlutterActivity扩展我的活动,因为它不支持getSupportFragmentManager I tried extending my activity using FlutterFragmentActivity and was able to override configureFlutterEngine method but then faced the following error.我尝试使用FlutterFragmentActivity扩展我的活动,并且能够覆盖configureFlutterEngine方法,但随后遇到以下错误。

ava.lang.IllegalArgumentException: No view found for id 0x245a3c5c (unknown) for fragment FlutterFragment{647abea (0b17a247-4e8e-42bc-b5f3-6ab0796e458b) id=0x245a3c5c flutter_fragment}

Following are my MainActivity.java and activity_main.xml以下是我的 MainActivity.java 和 activity_main.xml

MainActivity.java MainActivity.java

package com.mch.testplugin;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.android.FlutterFragment;
import io.flutter.embedding.android.FlutterFragmentActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends AppCompatActivity {

Context mContext;
FlutterFragment flutterFragment;
private static final String TAG_FLUTTER_FRAGMENT = "flutter_fragment";
FlutterEngine mFlutterEngine;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = this;
    setContentView(R.layout.activity_main);

    Button myButton = findViewById(R.id.myButton);
    myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            flutterFragment = (FlutterFragment) getSupportFragmentManager()
                    .findFragmentByTag(TAG_FLUTTER_FRAGMENT);

            // Create and attach a FlutterFragment if one does not exist.
            if (flutterFragment == null) {
                flutterFragment = FlutterFragment.createDefault();

                flutterFragment = FlutterFragment.withNewEngine()
                        .initialRoute("/")
                        .build();

                getSupportFragmentManager()
                        .beginTransaction()
                        .add(
                                R.id.frame_layout,
                                flutterFragment,
                                TAG_FLUTTER_FRAGMENT
                        )
                        .commit();


            }
    });

}


//    @Override
//    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
////        super.configureFlutterEngine(flutterEngine);
//        mFlutterEngine = flutterEngine;
//        GeneratedPluginRegistrant.registerWith(new FlutterEngine(this));
//    }
}

activity_main.xml activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout       xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
    android:id="@+id/myButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />

<FrameLayout
    android:id="@+id/frame_layout"
    android:layout_width="match_parent"
    android:layout_height="200dp" />

</androidx.constraintlayout.widget.ConstraintLayout>'

Can someone please guide me the right way to do this?有人可以指导我正确的方法吗? Your help is much appreciated.非常感谢您的帮助。

类似问题的通常解决方案是简单地停止应用程序并再次按下运行按钮

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM