在app billing中,getskudetails null point exception?

时间:2014-10-11 21:11:40

标签: java android in-app-billing

我正试图让应用账单3工作,不知道我做错了什么。我正在关注一个教程而且我被困住了。如果有人能帮助我开展工作,我将非常感激。

我只是想让最简单的静态测试工作。我可以通过手机获取,我正确导入了android.vending.billinginappbillingservice.aidl

我在NullPointerException获得SkuDetails = 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)

1 个答案:

答案 0 :(得分: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