英文:
Flutter - Native Android Component invoke with MethodChannel only works on first time then throws MissingPluginException
问题
I have a Flutter app with a native android component for scanning barcodes, on first run this native component runs fine and does what it needs to then returns to the Flutter section of app.
然后,任何时候,如果尝试再次打开本地 Android 组件,它总是会抛出错误:
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: MissingPluginException(No implementation found for method StartSecondActivity on channel com.example)
Only restarting the app seems to solve this problem, where it will run once with no error then repeat.
仅重新启动应用似乎可以解决这个问题,然后它会再次运行而不会出错。
Dart 代码看起来是这样的,第一次运行正常,但随后的尝试会报错,除非重新启动应用:
MethodChannel _channel = MethodChannel('com.example');
await _channel.invokeMethod('StartSecondActivity');
MainActivity.java 类:
import android.content.Intent;
import android.widget.Toast;
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "com.example";
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
GeneratedPluginRegistrant.registerWith(flutterEngine);
flutterEngine.getNavigationChannel().setInitialRoute("/waybill");
FlutterEngineCache.getInstance().put("bac_flutter_id", flutterEngine);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
.setMethodCallHandler((call, result) -> {
if(call.method.equals("StartSecondActivity")){
Intent intent = new Intent(MainActivity.this, IntentWedgeSample.class);
startActivity(intent);
//result.success("ActivityStarted");
}else{
//result.notImplemented();
}
});
}
}
Fairly new to Flutter and did not originally write this app so any hints on how to go about solving this would be much appreciated.
对Flutter相对较新,原本并不是这个应用的作者,所以对如何解决这个问题的任何提示将不胜感激。
英文:
I have a Flutter app with a native android component for scanning barcodes, on first run this native component runs fine and does what it needs to then returns to the Flutter section of app.
However, anytime after that if you try to open the native android component again it always throws the error: [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: MissingPluginException(No implementation found for method StartSecondActivity on channel com.example)
Only restarting the app seems to solve this problem, where it will run once with no error then repeat.
dart looks something like this, runs first time and gets error any subsequent tries unless app is restarted:
MethodChannel _channel = MethodChannel('com.example');
await _channel.invokeMethod('StartSecondActivity');
MainActivity.java class:
import android.content.Intent;
import android.widget.Toast;
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "com.example";
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
GeneratedPluginRegistrant.registerWith(flutterEngine);
flutterEngine.getNavigationChannel().setInitialRoute("/waybill");
FlutterEngineCache.getInstance().put("bac_flutter_id", flutterEngine);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
.setMethodCallHandler((call, result) -> {
if(call.method.equals("StartSecondActivity")){
Intent intent = new Intent(MainActivity.this, IntentWedgeSample.class);
startActivity(intent);
//result.success("ActivityStarted");
}else{
//result.notImplemented();
}
});
}
}
Fairly new to Flutter and did not originally write this app so any hints on how to go about solving this would be much appreciated.
答案1
得分: 0
I managed to fix this by using Android Intent+ plugin instead of MethodChannel (https://pub.dev/packages/android_intent_plus)
Then instead of the MethodChannel code, use:
AndroidIntent intent = AndroidIntent(
action: 'android.intent.action.ACTION_VIEW',
// Replace this by your package name.
package: 'com.example',
// Replace this by your package name followed by the activity you want to open.
// The default activity provided by Flutter is MainActivity, but you can check
// this in AndroidManifest.xml.
componentName: 'com.example.IntentWedgeSample',
);
await intent.launch();
英文:
Managed to fix this by using Android Intent+ plugin instead of MethodChannel (https://pub.dev/packages/android_intent_plus)
Then instead of the MethodChannel code, use:
AndroidIntent intent = AndroidIntent(
action: 'android.intent.action.ACTION_VIEW',
// Replace this by your package name.
package: 'com.example',
// Replace this by your package name followed by the activity you want to open.
// The default activity provided by Flutter is MainActivity, but you can check
// this in AndroidManifest.xml.
componentName: 'com.example.IntentWedgeSample',
);
await intent.launch();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论