我已经配置并测试了所有从后端收取随机数的东西,但是我的Flutter android应用程序遇到了问题。
这是我的代码:
square_payment.dart
import 'package:flutter/material.dart';
import 'package:square_in_app_payments/models.dart';
import 'package:square_in_app_payments/in_app_payments.dart';
class SquarePayment extends StatefulWidget {
@override
_SquarePaymentState createState() => _SquarePaymentState();
}
class _SquarePaymentState extends State<SquarePayment> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.brown[900],
body: Center(
child: FlatButton(
onPressed: () {
_pay();
},
color: Colors.brown,
child: Text("Pay now"),
),
),
);
}
@override
void initState() {
super.initState();
InAppPayments.setSquareApplicationId(
'my_sandbox_application_id');
}
_pay() async {
await InAppPayments.startCardEntryFlow(
onCardNonceRequestSuccess: _success, onCardEntryCancel: _cancel);
}
_success(CardDetails result) {
print(result.nonce);
try {
InAppPayments.completeCardEntry(onCardEntryComplete: () {print("success");});
} on Exception catch (ex) {
print(ex.toString());
}
}
_cancel() {
print("canceled");
}
}
在显示Square的信用卡条目UI后,我按照here所述添加了一个沙盒测试信用卡值。它接受测试卡,并且现时已打印在控制台上,但随后我的应用程序崩溃并显示以下错误:
E/AndroidRuntime( 4007): FATAL EXCEPTION: main
E/AndroidRuntime( 4007): Process: discoverayush.squaretestapp, PID: 4007
E/AndroidRuntime( 4007): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=51789, result=-1, data=Intent { (has extras) }} to activity {discoverayush.squaretestapp/discoverayush.squaretestapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.view.ContextThemeWrapper.getResources()' on a null object reference
E/AndroidRuntime( 4007): at android.app.ActivityThread.deliverResults(ActivityThread.java:4349)
E/AndroidRuntime( 4007): at android.app.ActivityThread.handleSendResult(ActivityThread.java:4392)
E/AndroidRuntime( 4007): at android.app.ActivityThread.-wrap22(ActivityThread.java)
E/AndroidRuntime( 4007): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635)
E/AndroidRuntime( 4007): at android.os.Handler.dispatchMessage(Handler.java:105)
E/AndroidRuntime( 4007): at android.os.Looper.loop(Looper.java:156)
E/AndroidRuntime( 4007): at android.app.ActivityThread.main(ActivityThread.java:6617)
E/AndroidRuntime( 4007): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 4007): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
E/AndroidRuntime( 4007): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
E/AndroidRuntime( 4007): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.view.ContextThemeWrapper.getResources()' on a null object reference
E/AndroidRuntime( 4007): at sqip.flutter.internal.CardEntryModule.readCardEntryCloseExitAnimationDurationMs(CardEntryModule.java:215)
E/AndroidRuntime( 4007): at sqip.flutter.internal.CardEntryModule.access$500(CardEntryModule.java:52)
E/AndroidRuntime( 4007): at sqip.flutter.internal.CardEntryModule$1$1.onResult(CardEntryModule.java:86)
E/AndroidRuntime( 4007): at sqip.flutter.internal.CardEntryModule$1$1.onResult(CardEntryModule.java:73)
E/AndroidRuntime( 4007): at sqip.internal.CardEntryActivity$Companion.onActivityResult(CardEntryActivity.kt:596)
E/AndroidRuntime( 4007): at sqip.CardEntry.handleActivityResult(CardEntry.kt:58)
E/AndroidRuntime( 4007): at sqip.flutter.internal.CardEntryModule$1.onActivityResult(CardEntryModule.java:73)
E/AndroidRuntime( 4007): at io.flutter.embedding.engine.FlutterEnginePluginRegistry$FlutterEngineActivityPluginBinding.onActivityResult(FlutterEnginePluginRegistry.java:634)
E/AndroidRuntime( 4007): at io.flutter.embedding.engine.FlutterEnginePluginRegistry.onActivityResult(FlutterEnginePluginRegistry.java:367)
E/AndroidRuntime( 4007): at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onActivityResult(FlutterActivityAndFragmentDelegate.java:546)
E/AndroidRuntime( 4007): at io.flutter.embedding.android.FlutterActivity.onActivityResult(FlutterActivity.java:594)
E/AndroidRuntime( 4007): at android.app.Activity.dispatchActivityResult(Activity.java:7188)
E/AndroidRuntime( 4007): at android.app.ActivityThread.deliverResults(ActivityThread.java:4345)
E/AndroidRuntime( 4007): ... 9 more
I/Process ( 4007): Sending signal. PID: 4007 SIG: 9
Lost connection to device.
android / app / build.gradle
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}
def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new FileNotFoundException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
}
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
flutterVersionName = '1.0'
}
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
compileSdkVersion 29
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
applicationId "discoverayush.squaretestapp"
minSdkVersion 24
targetSdkVersion 29
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
signingConfig signingConfigs.debug
}
}
}
flutter {
source '../..'
}
dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'com.google.firebase:firebase-analytics:17.2.2'
}
configurations.all {
resolutionStrategy.eachDependency {details ->
def requested = details.requested
if (requested.group == "androidx.appcompat") {
if (!requested.name.startsWith("multidex")) {
details.useVersion "1.+"
}
}
}
}
谢谢!
答案 0 :(得分:0)
找到了解决方案。
问题出在Flutter SDK版本上。我使用的是 v1.12.13 + hotfix.8 ,但是square_in_app_payments
尚不适用于此版本。卡输入完成后崩溃。因此,我将SDK降级为 v1.9.1 + hotfix.6 。
现在工作正常!