android 活动结果的寄存器在Flutter中不起作用

qyuhtwio  于 2022-11-20  发布在  Android
关注(0)|答案(1)|浏览(130)

我正在尝试从android MainActivity类启动上下文到flutter。代码:

val authResult = ComponentActivity().registerForActivityResult(PianoIdAuthResultContract()) { r ->
                when (r) {
                    null -> { /* user cancelled Authorization process */ }
                    is PianoIdAuthSuccessResult -> {
                        val token = r.token
                        val isNewUserRegistered = r.isNewUser
                        if (token != null) {
                            if (token.emailConfirmationRequired) {
                                // process enabled Double opt-in
                            }
                        }
                        // process successful authorization
                    }
                    is PianoIdAuthFailureResult -> {
                        val e = r.exception
                        // Authorization failed, check e.cause for details
                    }
                }
            }

然后调用方法启动代码:

try{
                authResult.launch(PianoId.signIn());
            }catch (e : Exception){
                val text = e.message
                val duration = Toast.LENGTH_SHORT

                val toast = Toast.makeText(applicationContext, text, duration)
                toast.show()
            }

然后通过在Flutter和android之间创建一个通道并调用它来从Flutter中调用此方法:

signInChannel.invokeMethod('testSignIn');

当我按下登录按钮时,它显示此异常:
尝试在空对象引用上调用虚拟方法“android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()”

dsekswqp

dsekswqp1#

我也在寻找解决方案。我所做的是用FlutterFragmentActivity扩展MainActivity,并将 this 传递给方法通道处理程序。

public class MainActivity extends FlutterFragmentActivity{
           ...
           @Override
           public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) 
           {
              handlePickerMethodChannel(flutterEngine);
           }
        

        private void handlePickerMethodChannel(FlutterEngine flutterEngine) {
            PickerMethodChannelHandler PickerMethodChannelHandler = new PickerMethodChannelHandler(new WeakReference<>(this));
            new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), PHOTO_PICKER_METHOD_CHANNEL)
                    .setMethodCallHandler(pickerMethodChannelHandler);
        }
    }

class PickerMethodChannelHandler(
    private val activity: WeakReference<Activity>,
) : MethodChannel.MethodCallHandler {

    private val pickMedia = (activity.get() as ComponentActivity).registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->
        if (uri != null) {
            Log.d("PhotoPicker", "Selected URI: $uri")
        } else {
            Log.d("PhotoPicker", "No media selected")
        }
    }

    override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
        when (call.method) {
            "pickMedia" -> pickMedia(call,result)
            else -> result.notImplemented()
        }
    }

    private fun pickMedia(call: MethodCall, result: MethodChannel.Result) {
        val context = activity.get() as ComponentActivity

        Log.i("PICK_MEDIA","PICK ${context != null}")
        pickMedia.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageAndVideo))
    }

}

成功了

相关问题