Android In-App Billing v3: “Can't perform operation: queryInventory”

后端 未结 11 2146
野趣味
野趣味 2021-01-31 09:15

I have setup In-App Billing for the first time using the new v3 API. It is working correctly on my devices but I have received a lot of error reports from other users.

O

11条回答
  •  长情又很酷
    2021-01-31 09:41

    In addition to @DavidM and @Ereza.

    Another major issue with the IabHelpr class is the poor choice of throwing RuntimeExcptions (IllegalStateException) in multiple methods. Throwing RuntimeExeptions from your own code in most cases is not desirable due to the fact that they are unchecked exceptions. That is like sabotaging your own application- if not caught, these exceptions will bubble up and crash your app.

    The solution to this is to implement your own checked exception and change the IabHelper class to throw it, instead of the IllegalStateException. That will force you to handle this exception everywhere it could be thrown in your code at compile time.

    Here is my custom exception:

    public class MyIllegalStateException extends Exception {
    
        private static final long serialVersionUID = 1L;
    
        //Parameterless Constructor
        public MyIllegalStateException() {}
    
        //Constructor that accepts a message
        public MyIllegalStateException(String message)
        {
           super(message);
        }
    }
    

    Once we make the changes in the IabHelper class, we can handle our checked exception in our code where we call the class methods. For example:

    try {
       setUpBilling(targetActivityInstance.allData.getAll());
    } catch (MyIllegalStateException ex) {
        ex.printStackTrace();
    }
    

提交回复
热议问题