gpt4 book ai didi

android - Android 构建 native 界面时出错

转载 作者:太空宇宙 更新时间:2023-11-03 16:07:00 25 4
gpt4 key购买 nike

我在codenameone的原生接口(interface)下实现了一些原生的android paypal集成代码。来自 codenameOne 的调用:

MyNative my = (MyNative)NativeLookup.create(MyNative.class);

在 native 界面中:

package com.mycompany.myapp;
import com.codename1.system.NativeInterface;
public interface MyNative extends NativeInterface{

}

我在 build_hint 下给出了正确的 android.xapplication 并在 impl 类下编写了 android 代码:

    package com.mycompany.myapp;
import java.math.BigDecimal;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Toast;

import com.paypal.android.sdk.payments.PayPalAuthorization;
import com.paypal.android.sdk.payments.PayPalConfiguration;
import com.paypal.android.sdk.payments.PayPalFuturePaymentActivity;
import com.paypal.android.sdk.payments.PayPalPayment;
import com.paypal.android.sdk.payments.PayPalService;
import com.paypal.android.sdk.payments.PaymentActivity;
import com.paypal.android.sdk.payments.PaymentConfirmation;

public class MyNativeImpl extends Activity{
// private static final String TAG = "paymentdemoblog";
/**
* - Set to PaymentActivity.ENVIRONMENT_PRODUCTION to move real money.
*
* - Set to PaymentActivity.ENVIRONMENT_SANDBOX to use your test credentials
* from https://developer.paypal.com
*
* - Set to PayPalConfiguration.ENVIRONMENT_NO_NETWORK to kick the tires
* without communicating to PayPal's servers.
*/
// private static final String CONFIG_ENVIRONMENT =
// PayPalConfiguration.ENVIRONMENT_NO_NETWORK;
private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_SANDBOX;

// note that these credentials will differ between live & sandbox
// environments.
private static final String CONFIG_CLIENT_ID = "Aeqc2X1rBIEUtDNqsaRNr0h1neFo9QnNmfgmpA3D32uSLaHpGJu9NV1KfMnFmy7O-_hV47I7ST0SXDW2";

private static final int REQUEST_CODE_PAYMENT = 1;
private static final int REQUEST_CODE_FUTURE_PAYMENT = 2;

private static PayPalConfiguration config = new PayPalConfiguration()
.environment(CONFIG_ENVIRONMENT)
.clientId(CONFIG_CLIENT_ID)
// The following are only used in PayPalFuturePaymentActivity.
.merchantName("Hipster Store")
.merchantPrivacyPolicyUri(
Uri.parse("https://www.example.com/privacy"))
.merchantUserAgreementUri(
Uri.parse("https://www.example.com/legal"));

PayPalPayment thingToBuy;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Intent intent = new Intent(this, PayPalService.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
startService(intent);
findViewById(R.id.order).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
thingToBuy = new PayPalPayment(new BigDecimal("10"), "USD",
"HeadSet", PayPalPayment.PAYMENT_INTENT_SALE);

Intent intent = new Intent(MyNativeImpl.this,
PaymentActivity.class);

intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);

startActivityForResult(intent, REQUEST_CODE_PAYMENT);
}
});

}

public void onFuturePaymentPressed(View pressed) {
Intent intent = new Intent(MyNativeImpl.this,
PayPalFuturePaymentActivity.class);

startActivityForResult(intent, REQUEST_CODE_FUTURE_PAYMENT);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PAYMENT) {
if (resultCode == Activity.RESULT_OK) {
PaymentConfirmation confirm = data
.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
if (confirm != null) {
try {
System.out.println(confirm.toJSONObject().toString(4));
System.out.println(confirm.getPayment().toJSONObject()
.toString(4));

Toast.makeText(getApplicationContext(), "Order placed",
Toast.LENGTH_LONG).show();

} catch (Exception e) {
e.printStackTrace();
}
}
} else if (resultCode == Activity.RESULT_CANCELED) {
System.out.println("The user canceled.");
} else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
System.out
.println("An invalid Payment or PayPalConfiguration was submitted. Please see the docs.");
}
} else if (requestCode == REQUEST_CODE_FUTURE_PAYMENT) {
if (resultCode == Activity.RESULT_OK) {
PayPalAuthorization auth = data
.getParcelableExtra(PayPalFuturePaymentActivity.EXTRA_RESULT_AUTHORIZATION);
if (auth != null) {
try {
Log.i("FuturePaymentExample", auth.toJSONObject()
.toString(4));

String authorization_code = auth.getAuthorizationCode();
Log.i("FuturePaymentExample", authorization_code);

sendAuthorizationToServer(auth);
Toast.makeText(getApplicationContext(),
"Future Payment code received from PayPal",
Toast.LENGTH_LONG).show();

} catch (Exception e) {
Log.e("FuturePaymentExample",
"an extremely unlikely failure occurred: ", e);
e.printStackTrace();
}
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.i("FuturePaymentExample", "The user canceled.");
} else if (resultCode == PayPalFuturePaymentActivity.RESULT_EXTRAS_INVALID) {
Log.i("FuturePaymentExample",
"Probably the attempt to previously start the PayPalService had an invalid PayPalConfiguration. Please see the docs.");
}
}
}

private void sendAuthorizationToServer(PayPalAuthorization authorization) {

}

public void onFuturePaymentPurchasePressed(View pressed) {
// Get the Application Correlation ID from the SDK
String correlationId = PayPalConfiguration
.getApplicationCorrelationId(this);

Log.i("FuturePaymentExample", "Application Correlation ID: "
+ correlationId);

// TODO: Send correlationId and transaction details to your server for
// processing with
// PayPal...
Toast.makeText(getApplicationContext(),
"App Correlation ID received from SDK", Toast.LENGTH_LONG)
.show();
}

@Override
public void onDestroy() {
// Stop service when done
stopService(new Intent(this, PayPalService.class));
super.onDestroy();
}
public boolean isSupported() {
return true;
}

}

现在我在 android 构建过程中遇到异常:

All input files are considered out-of-date for incremental task ':compileDebugJavaWithJavac'. Compiling with source level 1.7 and target level 1.7. :compileDebugJavaWithJavac - is not incremental (e.g. outputs have changed, no previous execution, etc.). file or directory '/tmp/build220258639476712910xxx/MyApplication/src/debug/java', not found Compiling with JDK Java compiler API. /tmp/build220258639476712910xxx/MyApplication/src/main/java/com/mycompany/myapp/MyNativeImpl.java:62: error: cannot find symbol setContentView(R.layout.activity_main); ^ symbol: variable activity_main location: class layout /tmp/build220258639476712910xxx/MyApplication/src/main/java/com/mycompany/myapp/MyNativeImpl.java:74: error: cannot find symbol Intent intent = new Intent(MainActivity.this, ^ symbol: class MainActivity /tmp/build220258639476712910xxx/MyApplication/src/main/java/com/mycompany/myapp/MyNativeImpl.java:67: error: cannot find symbol findViewById(R.id.order).setOnClickListener(new OnClickListener() { ^ symbol: variable order location: class id /tmp/build220258639476712910xxx/MyApplication/src/main/java/com/mycompany/myapp/MyNativeImpl.java:86: error: cannot find symbol Intent intent = new Intent(MainActivity.this, ^ symbol: class MainActivity location: class MyNativeImpl Note: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details. Note: Some input files use unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 4 errors :compileDebugJavaWithJavac FAILED :compileDebugJavaWithJavac (Thread[Daemon worker,5,main]) completed. Took 10.51 secs.

失败:构建失败,出现异常。

谁能帮忙....

最佳答案

XML 包中没有 activity_main,这就是 Android 自动生成的 R 类无法找到它的原因。

我还注意到您在 impl 类中继承了 Activity,这是错误的,您应该创建一个单独的 Activity 类,假设这实际上是您想要做的(您可能需要与核心 Codename One Activity 交互)。

由于我不熟悉 PayPal 集成,我猜测 activity_main 指的是您自己的 Activity ,它实际上是 CodenameOneActivity。从这个意义上说,大部分代码都是多余的,因为我们已经生成了这些代码,您只需将调用绑定(bind)到 paypal native 代码中即可。

关于android - Android 构建 native 界面时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37745289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com