2013-01-06 49 views
0

我已經寫了一個應用程序購買測試應用程序,以瞭解如何在我製作的應用程序中實現它。 我修改了谷歌提供的TrivialDrive示例中的代碼。 但是,在我的朋友使應用程序崩潰付款後,它不起作用。 的代碼看起來像這樣付款後應用程序內購買崩潰

public class MainActivity extends Activity { 



    String TAG = "AppPurchaseTest"; 
    IabHelper mHelper; 
    boolean mIsPremium = false; 
    static final String SKU_PREMIUM = "premium"; 
    static final int RC_REQUEST = 10001; 


// User clicked the "Upgrade to Premium" button. 
    public void onUpgradeAppButtonClicked(View arg0) { 
     Log.d(TAG, "Upgrade button clicked; launching purchase flow for upgrade."); 
    //  setWaitScreen(true); 
     mHelper.launchPurchaseFlow(this, SKU_PREMIUM, RC_REQUEST, mPurchaseFinishedListener); 
    } 


    //this is not working 

// Callback for when a purchase is finished 
    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() { 
     public void onIabPurchaseFinished(IabResult result, Purchase purchase) { 
      Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase); 

      int duration = Toast.LENGTH_SHORT; 
      if (result.isFailure()) { 
       // Oh noes! 
      // complain("Error purchasing: " + result); 
      // setWaitScreen(false); 
       Toast.makeText(getBaseContext(), "Fail :(", duration).show(); 
       return; 
      } 

      Log.d(TAG, "Purchase successful."); 

      if (purchase.getSku().equals(SKU_PREMIUM)) { 
       // bought the premium upgrade! 
       Log.d(TAG, "Purchase is premium upgrade. Congratulating user."); 
       // alert("Thank you for upgrading to premium!"); 
       mIsPremium = true; 

       Toast.makeText(getBaseContext(), "Successo: adesso sei premium", duration).show(); 
       Button test = (Button) findViewById(R.id.test); 
       test.setVisibility(View.INVISIBLE); 
       // updateUi(); 
      // setWaitScreen(false); 
      } 
     } 
    }; 


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

     String base64EncodedPublicKey = null; 

     // compute your public key and store it in base64EncodedPublicKey 
     mHelper = new IabHelper(this, base64EncodedPublicKey); 

     mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { 
      public void onIabSetupFinished(IabResult result) { 
       if (!result.isSuccess()) { 
        // Oh noes, there was a problem. 
        Log.d(TAG, "Problem setting up In-app Billing: " + result); 
       }    
        // Hooray, IAB is fully set up! 
      } 
     }); 
    } 


    @Override 
    protected void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 

     if (mHelper != null) { 
      Log.d(TAG, "mHelper doesn't = null "); 
     mHelper.dispose(); 
     mHelper = null; 
     } 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data); 

     // Pass on the activity result to the helper for handling 
     if (!mHelper.handleActivityResult(requestCode, resultCode, data)) { 
      // not handled, so handle it ourselves (here's where you'd 
      // perform any handling of activity results not related to in-app 
      // billing... 
      super.onActivityResult(requestCode, resultCode, data); 
     } 
     else { 
      Log.d(TAG, "onActivityResult handled by IABUtil."); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 


} 

你能點什麼了嗎?我忘了什麼?

另外本教程https://developer.android.com/google/play/billing/billing_integrate.html 看起來更簡單,但我不明白如何實現它,有沒有樣本或從中我可以看到它是如何實現的?我只需要一個簡單的升級,以溢價購買

這是很難讓它工作,因爲我不能親自測試一下,每次我測試了我賠錢:(

+1

如果「每次我測試它,我賠錢」是真的,那麼你做錯了什麼。在測試「https://developer.android.com/google/play/billing/billing_testing.html」時查看Google頁面。您應該開始使用測試sku值,然後移動到使用測試帳戶。即使您使用生產帳戶,也可以在控制檯中取消銷售。 – DrC

+0

感謝您的鏈接,非常有幫助:)我已閱讀過,但沒有明白它,只是看到截圖後,我意識到我必須做的 – DoubleP90

回答

1

花了很長時間來搞清楚上我的項目,但你必須明白,你的mPurchaseFinishedListener是在非渲染線程上調用的,並且在你的應用程序onResume()被調用之前(只需檢查IabHelper代碼)

所以,如果你嘗試做任何渲染,渲染上下文還沒有恢復。

在你的情況下它可能是:

Toast.makeText(getBaseContext(), "Successo: adesso sei premium", duration).show(); 

會崩潰,這同樣適用

Button test = (Button) findViewById(R.id.test); 

,如果你檢查TEXT按鈕可見性設置爲true瑣事例子,但按鈕本身是在OnCreate指定一個類的屬性( ) 方法。

讓我知道這是否有幫助..

相關問題