【问题标题】:In app billing, getskudetails null point exception?在应用计费中,getskudetails 空点异常?
【发布时间】:2014-10-11 21:11:40
【问题描述】:

我正在尝试让应用计费 3 正常工作,但不知道我做错了什么。我正在学习教程,但我被卡住了。如果有人可以帮助我使其正常工作,我将不胜感激。

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

我在SkuDetails = mservice.getSkuDetails(3, getPackageName(), "inapp", querySkus) 收到NullPointerException

我也可能有其他错误,但就我目前而言。

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)

【问题讨论】:

    标签: java android in-app-billing


    【解决方案1】:

    我让它工作,我知道它远非最好的例子,但对于这个问题,我找不到一个工作的例子,所以希望这会对某人有所帮助。我打算放一张源文件外观的图像,这让我有点困惑,但没有足够的代表来添加 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
    

    【讨论】:

    • 这段代码不是真的正确,它会起作用,然后我得到 "RESPONSE_CODE" ==7 这是 BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED 我试图弄清楚如何使用它,好像我遇到了问题startIntentSenderForResult,当它确实起作用时,不要认为它成功到 onActivityResult,不要回忆 toast。确定其他人可以更好地回答这个问题,我很乐意为答案表示感谢,只是把我的,因为我认为它有效并且可以帮助某人。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多