2016-06-10 65 views
1

我已經在codenameone的本地接口下實現了一些原生android paypal集成代碼。從codenameOne 電話:在爲Android構建本地接口時出現錯誤

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

在本地接口:

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

} 

我已經下build_hint的實現類下給予適當的android.xapplication寫的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.

失敗:生成失敗並出現異常。

任何人都可以請幫助....

+0

您似乎遇到了PayPal集成的一些問題,如果您有企業帳戶,則應聯繫我們的企業支持人員尋求幫助。我們可以爲你做所有事情。 –

+0

我注意到你不接受或upvote問題。回答並接受表明問題已經解決的一般是好策略。這也給了人們的回答,也給了你對StackOverflow許多事情有用的信息。 –

回答

0

中有這就是爲什麼從Android的自動生成R類不能找到它的XML包沒有activity_main

我也注意到你在impl類中繼承了Activity,這是錯誤的,你應該創建一個單獨的活動類,假設這實際上是你想要做的(你可能需要與核心Codename One活動進行交互)。

由於我不熟悉PayPal集成,我猜activity_main是指您自己的活動,它實際上是CodenameOneActivity。從這個意義上說,大部分代碼都是多餘的,因爲我們已經生成了這些代碼,您只需將調用綁定到PayPal本機代碼即可。