我的一个软件包需要访问Context
,我将其从onAttachedToEngine
-> flutterPluginBinding.getApplicationContext()
转换为变量,但这会使NullPointerException
使应用程序崩溃。原因是我认为我正在尝试将Context
分配给变量。
访问Context
的正确方法是什么?
如果我也想怎么获得Activity
?
最佳答案
取决于Create-Flutter-Plugin中的flutter文档,请按照以下步骤操作:
1-导入ActivityAware:
import io.flutter.embedding.engine.plugins.activity.ActivityAware
2-在您的类(class)中实现ActivityAware:
public class ClassName: FlutterPlugin, MethodCallHandler, ActivityAware {
3-定义lateinit变量以使用它的类:
private lateinit var context: Context
private lateinit var activity: Activity
4-添加以下功能:
override fun onDetachedFromActivity() {
TODO("Not yet implemented")
}
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
TODO("Not yet implemented")
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
activity = binding.activity;
}
override fun onDetachedFromActivityForConfigChanges() {
TODO("Not yet implemented")
}
5-在onAttachedToEngine函数中添加以下行:
context = flutterPluginBinding.applicationContext
您可以查看此完整代码以进一步了解:
package com.example.flutter_plugin_name
import android.app.Activity
import android.content.Context
import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar
public class FlutterPluginName: FlutterPlugin, MethodCallHandler, ActivityAware {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private lateinit var channel : MethodChannel
private lateinit var context: Context
private lateinit var activity: Activity
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "flutter_plugin_name")
channel.setMethodCallHandler(this);
context = flutterPluginBinding.applicationContext
}
// This static function is optional and equivalent to onAttachedToEngine. It supports the old
// pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
// plugin registration via this function while apps migrate to use the new Android APIs
// post-flutter-1.12 via https://flutter.dev/go/android-project-migration.
//
// It is encouraged to share logic between onAttachedToEngine and registerWith to keep
// them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called
// depending on the user's project. onAttachedToEngine or registerWith must both be defined
// in the same class.
companion object {
@JvmStatic
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "flutter_plugin_name")
channel.setMethodCallHandler(FlutterMapboxTurnByTurnPlugin())
}
}
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
if (call.method == "getPlatformVersion") {
result.success("Android ${android.os.Build.VERSION.RELEASE}")
}
else {
result.notImplemented()
}
}
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
override fun onDetachedFromActivity() {
TODO("Not yet implemented")
}
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
TODO("Not yet implemented")
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
activity = binding.activity;
}
override fun onDetachedFromActivityForConfigChanges() {
TODO("Not yet implemented")
}
}
关于flutter - 如何在Flutter插件中获取事件和上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60048704/