2014-10-11 46 views
0

我試圖讓應用程序結算3工作,不知道我做錯了什麼。我正在跟着一個教程,我被卡住了。如果有人能夠幫助我實現這個目標,我會非常感激。在應用程序結算中,getskudetails零點異常?

我只是想獲得最簡單的靜態測試工作。我可以在手機上獲得,我正確導入了android.vending.billinginappbillingservice.aidl

我收到NullPointerExceptionSkuDetails = mservice.getSkuDetails(3, getPackageName(), "inapp", querySkus)

我可能還有其他錯誤,但這就像我現在做的那樣。

public class MainActivity extends ActionBarActivity { 

    IInAppBillingService mservice; 
    ServiceConnection connection; 
    String inappid= "android.test.purchased"; 


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

    System.out.println("oncreabte!!!!!!!!"); 


     ServiceConnection connection = new ServiceConnection() { 

      @Override 
      public void onServiceConnected(ComponentName name, IBinder service) { 
       // TODO Auto-generated method stub 
       mservice = IInAppBillingService.Stub.asInterface(service); 
       System.out.println("it conncekcted!!!!!!!!"); 



      } 

      @Override 
      public void onServiceDisconnected(ComponentName name) { 
       // TODO Auto-generated method stub 
       System.out.println("it disconncected!!!!!!!!!!"); 
      } 
    }; 

    bindService (new Intent(

     "com.android.vending.billing.InAppBillService.BIND"), 
      connection, Context.BIND_AUTO_CREATE); 

    Button purchaseBtn = (Button)findViewById(R.id.purchase); 

    System.out.println("bind!!!!!!!!!!"); 

     purchaseBtn.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v) { 

       System.out.println("onclccccick"); 

       ArrayList skuList = new ArrayList(); 
       skuList.add(inappid); 
       Bundle querySkus = new Bundle(); 
       querySkus.putStringArrayList("ITEM_ID_LIST",skuList); 
      // Bundle SkuDetails; 
       System.out.println("right befor try!!!"); 
       Bundle SkuDetails; 


       try{ 
        System.out.println("right "+ skuList.get(0)); 
        System.out.println("mine! "+querySkus); 

        SkuDetails = mservice.getSkuDetails(3, 
           getPackageName(), "inapp", querySkus); 




        int responce = SkuDetails.getInt("RESPONCE_CODE"); 
        System.out.println("Endddd!!!" + responce); 

        if(responce ==0){ 
         System.out.println("Endddd!!!"); 
         ArrayList<String> responceList = SkuDetails.getStringArrayList("DETAILS_LIST"); 
         for (String thisResponse : responceList){ 
          JSONObject object = new JSONObject(thisResponse); 
           String sku = object.getString("productId"); 
           String price = object.getString("price"); 


           if (sku.equals("inappid")){ 
           System.out.println("price" + price); 
           Bundle buyIntentBundle = mservice.getBuyIntent(3, getPackageName(), 
              sku, "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ"); 
           PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT"); 

           startIntentSenderForResult(pendingIntent.getIntentSender(), 
              1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0), 
              Integer.valueOf(0)); 


           } 


         } 




        } 
       } 

        catch (RemoteException e) { 
         System.out.println("RemoteE"); 
         e.printStackTrace(); 
         } 


        catch (JSONException e) { 
         System.out.println("Remotejso"); 
         e.printStackTrace(); 
         } 
        catch (SendIntentException e) { 
          System.out.println("Remotein"); 
         e.printStackTrace(); 
         } 


        } 

       }); 

    }  

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     if (resultCode == 1001) { 
     String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA"); 

      if (resultCode == RESULT_OK) { 
       System.out.println("resuuut goood"); 
      try{ 
       JSONObject jo = new JSONObject(purchaseData); 
       String sku = jo.getString(inappid); 
       Toast.makeText(MainActivity.this, 
         "u have bought"+ sku, 
         Toast.LENGTH_LONG).show(); 
      } 
      catch(JSONException e){ 
       System.out.println("didnt work"); 
       e.printStackTrace(); 
      } 
      } 
     } 
    }  
    //end 

    public void onDestroy(){ 
     super.onDestroy(); 
     if(connection != null){ 
      unbindService(connection); 
     } 
    } 

} 

例外:

10-11 16:48:15.944: E/AndroidRuntime(20612): FATAL EXCEPTION: main 
10-11 16:48:15.944: E/AndroidRuntime(20612): java.lang.NullPointerException 
10-11 16:48:15.944: E/AndroidRuntime(20612): at com.example.bill.MainActivity$2.onClick(MainActivity.java:91) 
10-11 16:48:15.944: E/AndroidRuntime(20612): at android.view.View.performClick(View.java:4101) 
10-11 16:48:15.944: E/AndroidRuntime(20612): at android.view.View$PerformClick.run(View.java:17085) 
10-11 16:48:15.944: E/AndroidRuntime(20612): at android.os.Handler.handleCallback(Handler.java:615) 
10-11 16:48:15.944: E/AndroidRuntime(20612): at android.os.Handler.dispatchMessage(Handler.java:92) 
10-11 16:48:15.944: E/AndroidRuntime(20612): at android.os.Looper.loop(Looper.java:137) 
10-11 16:48:15.944: E/AndroidRuntime(20612): at android.app.ActivityThread.main(ActivityThread.java:4941) 
10-11 16:48:15.944: E/AndroidRuntime(20612): at java.lang.reflect.Method.invokeNative(Native Method) 
10-11 16:48:15.944: E/AndroidRuntime(20612): at java.lang.reflect.Method.invoke(Method.java:511) 
10-11 16:48:15.944: E/AndroidRuntime(20612): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794) 
10-11 16:48:15.944: E/AndroidRuntime(20612): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561) 
10-11 16:48:15.944: E/AndroidRuntime(20612): at dalvik.system.NativeStart.main(Native Method) 

回答

0

我得到它的工作,我知道這是迄今爲止最好的例子,但對於這個問題,我無法找到工作的例子,所以希望這會幫助別人。我打算把源文件的外觀,這讓我有點困惑,但沒有足夠的代表添加img。我的例子來自於https://www.youtube.com/watch?v=-h2ESH71hAI,感謝他。

公共類MainActivity擴展ActionBarActivity {

IInAppBillingService mservice; 
    ServiceConnection connection; 
    String inappid = "android.test.purchased"; 


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

    System.out.println("oncreccabte!!!!!!!!"); 


     ServiceConnection connection = new ServiceConnection() { 


      @Override 
      public void onServiceDisconnected(ComponentName name) { 
       // TODO Auto-generated method stub 
       System.out.println("it disconncected!!!!!!!!!!"); 
      } 



      @Override 
      public void onServiceConnected(ComponentName name, IBinder service) { 
       // TODO Auto-generated method stub 
       mservice = IInAppBillingService.Stub.asInterface(service); 
       System.out.println("it cmonnxcekcted!!!!!!!!"); 



      } 





    }; 

    bindService (new Intent(

     "com.android.vending.billing.InAppBillingService.BIND"), // had error here 
      connection, Context.BIND_AUTO_CREATE); 

    Button purchaseBtn = (Button)findViewById(R.id.purchase); 

    System.out.println("bind!!!!!!!!!!"); 

     purchaseBtn.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v) { 

       System.out.println("onclccccick"); 

       ArrayList skuList = new ArrayList(); 
       skuList.add(inappid); 
       Bundle querySkus = new Bundle(); 
       querySkus.putStringArrayList("ITEM_ID_LIST",skuList); 
      // Bundle SkuDetails; 
       System.out.println("right befor try!!!"); 
       Bundle SkuDetails; 


       try{ 
        System.out.println("right "+ skuList.get(0)); 
        System.out.println("mine! "+querySkus); 

        SkuDetails = mservice.getSkuDetails(3, 
           getPackageName(), "inapp", querySkus); 




        int responce = SkuDetails.getInt("RESPONCE_CODE"); 
        System.out.println("Endddd!!!" + responce); 

        if(responce ==0){ 
         System.out.println("equls 0!!!"); 
         ArrayList<String> responceList = SkuDetails.getStringArrayList("DETAILS_LIST"); 

         for (String thisResponse : responceList){ 
          JSONObject object = new JSONObject(thisResponse); 
           String sku = object.getString("productId"); 
           String price = object.getString("price"); 
           System.out.println("thr price" + price); 
           System.out.println("thr price" + sku); 

          //error had inappid in "" 

           if (sku.equals(inappid)){ 
            System.out.println("it does prob"); 
            System.out.println("price" + price); 
           Bundle buyIntentBundle = mservice.getBuyIntent(3, getPackageName(), 
              sku, "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ"); 
           PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT"); 

           startIntentSenderForResult(pendingIntent.getIntentSender(), 
              1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0), 
              Integer.valueOf(0)); 


           } 


         } 




        } 
       } 

        catch (RemoteException e) { 
         System.out.println("RemoteE"); 
         e.printStackTrace(); 
         } 


        catch (JSONException e) { 
         System.out.println("Remotejso"); 
         e.printStackTrace(); 
         } 
        catch (SendIntentException e) { 
          System.out.println("Remotein"); 
         e.printStackTrace(); 
         } 


        } 

       }); 

    }  

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     if (resultCode == 1001) { 
     String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA"); 

      if (resultCode == RESULT_OK) { 
       System.out.println("re ssuut ood"); 
      try{ 
       JSONObject jo = new JSONObject(purchaseData); 
       String sku = jo.getString(inappid); 
       Toast.makeText(MainActivity.this, 
         "u have bo ught"+ sku, 
         Toast.LENGTH_LONG).show(); 
      } 
      catch(JSONException e){ 
       System.out.println("didnt work"); 
       e.printStackTrace(); 
      } 
      } 
     } 
    }  
    //end 

    public void onDestroy(){ 
     super.onDestroy(); 
     if(connection != null){ 
      unbindService(connection); 
     } 
    } 

}enter image description here 
+0

此代碼是不是真的正確,它會工作,然後我得到「RESPONSE_CODE」 == 7這是BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED我想FIGUR如何使用它,好像我在startIntentSenderForResult遇到了問題,並且當它工作時,不要認爲它是在onActivityResult上做的,不要回想起敬酒。當然別人可以更好地回答這個問題,我會很樂意回答這個問題,因爲我認爲它有效,可以幫助某人。 – user3705179 2014-10-12 19:59:50