2017-07-19 43 views
1

當我嘗試編譯我的Android Studio中的項目,我得到以下錯誤:Android Studio中的錯誤:(386,38)錯誤:不兼容的類型:字符串不能轉換爲背景

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. 

錯誤指對存在於我的SummaryFragment.java班線

stripe = new Stripe(Const.STRIPE_TOKEN); 

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(); 
     } 
    } 
} 

我是什麼做錯了?

回答

0

變化:

new Stripe(Const.STRIPE_TOKEN); 

要:

new Stripe(getActivity(), Const.STRIPE_TOKEN); 
0

隨着錯誤消息表示,該Stripe constructor you are attempting to use接受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是什麼我認爲這是,你可能會有更多的成功使用the two-argument constructor接受既是ContextString '鑰匙':

/** 
* 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); 

但是,您應該驗證我在繼續之前就該令牌的目的作出了假設。

+0

非常感謝您爲格式化我的原始問題並使其更加智能化。當我嘗試你的建議時,我得到另一個錯誤:錯誤:(405,11)錯誤:異常AuthenticationException永遠不會在相應的try語句的主體中拋出。這個錯誤指向405行,這裏是405行:} catch(AuthenticationException e){ – geemint

+0

不客氣!新的錯誤告訴你,相關的'try'塊中包含的代碼實際上都不會拋出類型爲'AuthenticationException'的Exception。您可能完全可以刪除catch塊。但是,如果這不起作用,請發佈一個包含相關代碼的小片段的新問題。由於此答案似乎已成功解決了原始問題,請將其標記爲已接受(綠色複選標記)。通過確保每個問題都專注於一個問題,我們會讓其他人更容易找到答案! – stkent

相關問題