![](/img/trans.png)
[英]Android studio: error: incompatible types: double cannot be converted to String
[英]Android Studio Error:(386, 38) error: incompatible types: String cannot be converted to Context
當我嘗試在Android Studio中編譯項目時,出現以下錯誤:
Error:(386, 38) error: incompatible types: String cannot be converted to Context
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
錯誤是指行
stripe = new Stripe(Const.STRIPE_TOKEN);
存在於我的SummaryFragment.java
類中:
package com.example.Fragments;
// Stripe imports
import com.stripe.android.Stripe;
import com.stripe.android.TokenCallback;
import com.stripe.android.exception.AuthenticationException;
import com.stripe.android.model.Card;
import com.stripe.android.model.Token;
// Non-Stripe imports omitted
/**
* A simple {@link Fragment} subclass.
*/
public class SummaryFragment extends Fragment implements
View.OnClickListener {
// Other fields and methods omitted
private void getStripeToken(String cardNo, String expMonth, String expYear, String cvv) {
loadingDialog.show();
Card card = new Card(cardNo, Integer.parseInt(expMonth), Integer.parseInt(expYear), cvv);
Stripe stripe = null;
try {
stripe = new Stripe(Const.STRIPE_TOKEN);
// Other logic omitted
} catch (AuthenticationException e) {
e.printStackTrace();
}
}
}
我究竟做錯了什么?
更改:
new Stripe(Const.STRIPE_TOKEN);
至:
new Stripe(getActivity(), Const.STRIPE_TOKEN);
如錯誤消息所示, 您嘗試使用的Stripe
構造函數接受Context
,而不是String
:
/**
* A constructor with only context, to set the key later.
*
* @param context {@link Context} for resolving resources
*/
public Stripe(@NonNull Context context) {
mContext = context;
}
如果您的Const.STRIPE_TOKEN
是我認為的樣子,那么使用同時接受Context
和String
“鍵” 的兩參數構造函數 ,您可能會獲得更大的成功:
/**
* Constructor with publishable key.
*
* @param context {@link Context} for resolving resources
* @param publishableKey the client's publishable key
*/
public Stripe(@NonNull Context context, String publishableKey) {
mContext = context;
setDefaultPublishableKey(publishableKey);
}
為此,您將替換該行
stripe = new Stripe(Const.STRIPE_TOKEN);
與線
stripe = new Stripe(getActivity(), Const.STRIPE_TOKEN);
但是,在繼續之前,您應該驗證我對該令牌用途的假設。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.