我正在自动售货机上实现一个应用程序,为此我使用了“PagSeguro”卡片机和PlugPag库......由于没有用于React Native的库(仅在Java中),我在Native Modules进程中使用它。
请遵循以下代码:
App.js
import React from 'react';
import { NativeModules, Button} from 'react-native';
const App= () => {
payment = () => {
NativeModules.PlugPag.doCreditPayment()
};
return (
<Button
title="test"
onPress={payment()}
/>
);
}
export default App;
我的Java代码:
package com.myapp;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import java.util.Map;
import java.util.HashMap;
@ReactMethod
public void doCreditPayment() {
try {
// WritableMap map = Arguments.createMap();
PlugPagPaymentData paymentData = new PlugPagPaymentData(PlugPag.TYPE_CREDITO, 110,
PlugPag.INSTALLMENT_TYPE_A_VISTA, 1, "APPDEMO");
PlugPagAppIdentification appIdentification = new PlugPagAppIdentification("MeuApp", "1.0.7");
PlugPag plugpag = new PlugPag(reactContext, appIdentification);
PlugPagInitializationResult result = plugpag
.initializeAndActivatePinpad(new PlugPagActivationData("403938"));
if (result.getResult() == PlugPag.RET_OK) {
plugpag.setEventListener(plugPagEventData -> {
// you can add another listener here
}
PlugPagTransactionResult transResult = plugpag.doPayment(paymentData);
if (transResult.getResult() == PlugPag.RET_OK) {
//response.invoke(transResult.getTransactionCode());
emitDeviceEvent("SuccessPayment", transResult.getTransactionCode());
});
} else {
//error.invoke(transResult.getMessage());
emitDeviceEvent("ErrorPayment", transResult.getMessage());
});
}
// Log.i("GET", "GET " + transResult);
} else {
error.invoke(RETURN_ERROR);
}
} catch (IllegalViewOperationException e) {
error.invoke(E_LAYOUT_ERROR);
}
}
private void emitDeviceEvent(String eventName, String message) {
Log.i("GET", "GET " + message);
// A method for emitting from the native side to JS
// https://facebook.github.io/react-native/docs/native-modules-android.html#sending-events-to-javascript
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName,
message);
}
我正在使用已经定义了值的库进行测试,但是在编译时显示了以下错误:
> error Failed to install the app. Make sure you have the Android
> development environment set up:
> https://reactnative.dev/docs/environment-setup. Error: Command failed:
> gradlew.bat app:installDebug -PreactNativeDevServerPort=8081
> C:\Users\leand\react\myapp\android\app\src\main\java\com\myapp\plugPag.java:11:
> error: class, interface, or enum expected public void
> doCreditPayment() {
> ^ C:\Users\leand\react\myapp\android\app\src\main\java\com\myapp\plugPag.java:18:
> error: class, interface, or enum expected
> PlugPagAppIdentification appIdentification = new PlugPagAppIdentification("MeuApp", "1.0.7");
> ^ C:\Users\leand\react\myapp\android\app\src\main\java\com\myapp\plugPag.java:20:
> error: class, interface, or enum expected
> PlugPag plugpag = new PlugPag(reactContext, appIdentification);
> ^ C:\Users\leand\react\myapp\android\app\src\main\java\com\myapp\plugPag.java:22:
> error: class, interface, or enum expected
> PlugPagInitializationResult result = plugpag
> ^ C:\Users\leand\react\myapp\android\app\src\main\java\com\myapp\plugPag.java:25:
> error: class, interface, or enum expected
> if (result.getResult() == PlugPag.RET_OK) {
> ^ C:\Users\leand\react\myapp\android\app\src\main\java\com\myapp\plugPag.java:34:
> error: class, interface, or enum expected
> if (transResult.getResult() == PlugPag.RET_OK) {
> ^ C:\Users\leand\react\myapp\android\app\src\main\java\com\myapp\plugPag.java:38:
> error: class, interface, or enum expected
> });
> ^ C:\Users\leand\react\myapp\android\app\src\main\java\com\myapp\plugPag.java:39:
> error: class, interface, or enum expected
> } else {
> ^ C:\Users\leand\react\myapp\android\app\src\main\java\com\myapp\plugPag.java:43:
> error: class, interface, or enum expected
> });
> ^ C:\Users\leand\react\myapp\android\app\src\main\java\com\myapp\plugPag.java:44:
> error: class, interface, or enum expected
> }
> ^ C:\Users\leand\react\myapp\android\app\src\main\java\com\myapp\plugPag.java:50:
> error: class, interface, or enum expected
> }
> ^ C:\Users\leand\react\myapp\android\app\src\main\java\com\myapp\plugPag.java:55:
> error: class, interface, or enum expected
> }
> ^ C:\Users\leand\react\myapp\android\app\src\main\java\com\myapp\plugPag.java:63:
> error: class, interface, or enum expected
> reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName,
> ^ C:\Users\leand\react\myapp\android\app\src\main\java\com\myapp\plugPag.java:63:
> error: <identifier> expected
> reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName,
> ^ 14 errors
你能帮帮我吗?
1条答案
按热度按时间xmq68pz91#
在Java中,所有函数都需要在Class中。
在此特定情况下,它需要是从ReactContextBaseJavaModule扩展的类,即:
虽然我相信你在创建一个本地java类时还需要一个@Override公共字符串getName()函数来在ReactNative中使用。完整的代码应该是: