在Flutter应用中集成GoogleTez时出错

时间:2019-12-05 10:41:52

标签: java android flutter

我正在尝试通过使用MethodChannel

在Flutter应用中实现GoogleTEZ

但是我遇到了类似的错误:

E/MethodChannel#googlePay(11353): Failed to handle method call
E/MethodChannel#googlePay(11353): java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
E/MethodChannel#googlePay(11353):   at android.app.Activity.startActivityForResult(Activity.java:5193)
E/MethodChannel#googlePay(11353):   at android.app.Activity.startActivityForResult(Activity.java:5150)
E/MethodChannel#googlePay(11353):   at com.yantrart.charge_my_car.MainActivity$1.onMethodCall(MainActivity.java:59)
E/MethodChannel#googlePay(11353):   at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:222)
E/MethodChannel#googlePay(11353):   at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:96)
E/MethodChannel#googlePay(11353):   at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:643)
E/MethodChannel#googlePay(11353):   at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#googlePay(11353):   at android.os.MessageQueue.next(MessageQueue.java:336)
E/MethodChannel#googlePay(11353):   at android.os.Looper.loop(Looper.java:174)
E/MethodChannel#googlePay(11353):   at android.app.ActivityThread.main(ActivityThread.java:7343)
E/MethodChannel#googlePay(11353):   at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#googlePay(11353):   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/MethodChannel#googlePay(11353):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:933)

这是我的代码:

import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.MethodCall;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.app.Activity;


public class MainActivity extends FlutterActivity {

    private static final int TEZ_REQUEST_CODE = 123;

  private static final String CHANNEL = "googlePay";

    private static final String GOOGLE_TEZ_PACKAGE_NAME = "com.google.android.apps.nbu.paisa.user";
    int GOOGLE_PAY_REQUEST_CODE = 123;

  MethodChannel.Result Res ;

  private  Activity activity = new Activity();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);


    new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
            new MethodCallHandler() {
              @Override
              public void onMethodCall(MethodCall call, Result result) {

                if(call.method.equals("payMerchant")){


                  Uri uri =
                          new Uri.Builder()
                                  .scheme("upi")
                                  .authority("pay")
                                  .appendQueryParameter("pa", "test@axisbank")
                                  .appendQueryParameter("pn", "Test Merchant")
                                  .appendQueryParameter("mc", "1234")
                                  .appendQueryParameter("tr", "123456789")
                                  .appendQueryParameter("tn", "test transaction note")
                                  .appendQueryParameter("am", "10.01")
                                  .appendQueryParameter("cu", "INR")
                                  .appendQueryParameter("url", "https://test.merchant.website")
                                  .build();
                  Intent intent = new Intent(Intent.ACTION_VIEW);
                  intent.setData(uri);
                  intent.setPackage(GOOGLE_TEZ_PACKAGE_NAME);
                  activity.startActivityForResult(intent,GOOGLE_PAY_REQUEST_CODE);

                }
                else
                  result.notImplemented();
              }

            });

  }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == TEZ_REQUEST_CODE) {
            // Process based on the data in response.
            Log.d("result", data.getStringExtra("Status"));
            //result.success(data.getStringExtra("Status"));
        }
    }
}```

1 个答案:

答案 0 :(得分:0)

直接调用“ startActivityForResult()”。该错误存在,因为您正在创建一个空活动并在视图中未包含的空活动上调用startActivityForResult。