gpt4 book ai didi

android - 尝试在空对象引用上调用接口(interface)方法 'android.IInAppBillingService...

转载 作者:行者123 更新时间:2023-11-30 00:52:46 25 4
gpt4 key购买 nike

大家好,我有一个关于计费的问题,当我点击按钮时,我在另一个类中调用了这个方法:

public void launchPurchase() {
BillingManager.getInstance().setActivity(this);
BillingManager.getInstance().Initialize();
BillingManager.getInstance().tryToPurchase("pubgame");
}

我得到错误

java.lang.NullPointerException: Attempt to invoke interface method 'android.os.Bundle com.android.vending.billing.IInAppBillingService.getBuyIntent(int, java.lang.String, java.lang.String, java.lang.String, java.lang.String)' on a null object reference
at inappbilling.BillingManager.tryToPurchase(BillingManager.java:57)
at com.kingerino.erwan.colorun.succes.launchPurchase(succes.java:377)
at com.kingerino.erwan.colorun.succes$2.onClick(succes.java:157)
at android.view.View.performClick(View.java:5205)
at android.view.View$PerformClick.run(View.java:21164)
at android.os.Handler.handleCallback(Handler.java:745)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:171)
at android.app.ActivityThread.main(ActivityThread.java:5456)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

这是我的类 BillingManager

public class BillingManager extends Activity{
IInAppBillingService mService;
private static BillingManager ourInstance = new BillingManager();

public static BillingManager getInstance() {
return ourInstance;
}


private BillingManager() {
}
public void Initialize() {
// Payment etc
ServiceConnection mServiceConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mService = IInAppBillingService.Stub.asInterface(service);
// Do something here
}
};

Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
activity.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
}


/** A/D
*
*/

public void tryToPurchase(String idProduct) {
Bundle buyIntentBundle = null;
try {
buyIntentBundle = mService.getBuyIntent(3, activity.getPackageName(), idProduct, "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
if (pendingIntent == null) {
return;
}
activity.startIntentSenderForResult(pendingIntent.getIntentSender(), 1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
} catch (RemoteException e) {
e.printStackTrace();
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}

/** Getter/Setter
*
*/
public void setActivity(Activity activity) {
this.activity = activity;
}

/** A/D
*
*/
private Activity activity;


public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1001) {
int responseCode = data.getIntExtra("RESPONSE_CODE", 0);
String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE");

if (resultCode == Activity.RESULT_OK) {
try {
JSONObject jo = new JSONObject(purchaseData);
String sku = jo.optString("productId");
String packageName = jo.optString("packageName");
String purchaseToken = jo.optString("purchaseToken");

if (sku.equalsIgnoreCase("pubgame")) {
// Do something here...
//int response = BillingManager.getInstance().getmService().consumePurchase(3, packageName, purchaseToken);
}/*
else if (sku.equalsIgnoreCase("myItem2")) {
// Do something here...
//int response = BillingManager.getInstance().getmService().consumePurchase(3, packageName, purchaseToken);
}*/
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public void getPurchaseStatus(String idProduct) {
try {
Bundle ownedItems = null;
ownedItems = mService.getPurchases(3, activity.getPackageName(), "inapp", null);
int response = ownedItems.getInt("RESPONSE_CODE");
if (response == 0) {
ArrayList<String> ownedSkus = ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");

for (int i = 0; i < ownedSkus.size(); ++i) {
String sku = ownedSkus.get(i);
if (sku.equalsIgnoreCase(idProduct)) {
// Do something here...
}
}
}
} catch (RemoteException e) {
e.printStackTrace();
}
}


}

我使用github上的代码:https://gist.github.com/adrienfenech/a477de4d16b118402faf

最佳答案

计费服务的初始化是异步的。因此,在调用 BillingManager.getInstance().Initialize() 之后立即;变量 mService 为 null,您会得到异常。

调用mServiceConn.onServiceConnected()后初始化。

您可以通过以下方式之一正确制作:

  1. 应用启动时初始化应用内支付服务,调用tryToPurchase()后检查mService是否为null

  2. 实现 mServiceConn.onServiceConnected() 的监听器,并在计费服务初始化后运行 tryToPurchase()。

关于android - 尝试在空对象引用上调用接口(interface)方法 'android.IInAppBillingService...,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40704627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com