- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我得到了一个错误,我要疯了,因为只在某些情况下发生,但我真的不明白什么时候,也许更专业的眼睛可以帮助我:
我从 Google 教程中获取了进行应用内购买的代码,简要介绍一下这段代码:
Initialize the In-App Service
Retrieve the price
对象Start the in-purchase
如果用户点击“购买”按钮。现在,在我所做的所有测试中(更多设备和 API)工作正常(更多设备和 API)但是我收到大量报告说:抛出 NullPointerException
在 mHelper
.
我认为这发生在处理服务时的 OnDestroy() 上,但我不确定并且我无法修复它(最后完成错误日志)。
这是我尽可能多地清理和注释的代码:
IabHelper mHelper;
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener;
Activity c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_room);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
c=this;
//Initialize the In-App Service
mHelper = new IabHelper(this, "my_key");
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
if (!result.isSuccess()) {
//Problem setting up In-app Billing
return;
}
if (mHelper == null) return;
//Ask for the price
List additionalSkuList = new ArrayList();
additionalSkuList.add("SKU_ID");
try {
mHelper.queryInventoryAsync(true, additionalSkuList, mQueryFinishedListener);
}catch (Exception e){
//Fail while asking the price
}
}
});
//Buy Button Listener
b_buy.setOnClickListener(new View.OnClickListener()
{
public void onClick(View V)
{
try {
String payload= "my_payload"
mHelper.launchPurchaseFlow(c, "SKU_ID",1111,mPurchaseFinishedListener, payload);
} catch (Exception e) {
//Error launching purchase flow. Another async operation in progress
}
}
});
// Callback for when a purchase is finished
mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
// if we were disposed of in the meantime, quit.
if (mHelper == null) return;
if (result.isFailure()) {
//Error while buying
return;
}
if (purchase.getSku().equals("SKU_ID")) {
// bought the premium upgrade!
}
}
};
}
//For retrieve the price:
IabHelper.QueryInventoryFinishedListener mQueryFinishedListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
if (result.isFailure()) {
return;
}
String z = inventory.getSkuDetails("SKU_ID").getPrice();
//The price of the object is + z !!!
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (mHelper == null) return;
// Pass on the activity result to the helper for handling
if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mHelper != null) mHelper.dispose();
mHelper = null;
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
}
这是错误:
Exception java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
com.myproj.util.IabHelper.startSetup (IabHelper.java)
__null__.dispose (IabHelper.java)
__null__.launchPurchaseFlow (IabHelper.java)
__null__.handleActivityResult (IabHelper.java)
__null__.queryInventory (IabHelper.java)
__null__.queryInventoryAsync (IabHelper.java)
__null__.getResponseDesc (IabHelper.java)
__null__.checkSetupDone (IabHelper.java)
__null__.getResponseCodeFromBundle (IabHelper.java)
__null__.getResponseCodeFromIntent (IabHelper.java)
__null__.queryPurchases (IabHelper.java)
__null__.querySkuDetails (IabHelper.java)
com.myproj.util.IabHelper.startSetup (IabHelper.java)
__null__.dispose (IabHelper.java)
__null__.launchPurchaseFlow (IabHelper.java)
__null__.handleActivityResult (IabHelper.java)
__null__.queryInventory (IabHelper.java)
__null__.queryInventoryAsync (IabHelper.java)
__null__.getResponseDesc (IabHelper.java)
__null__.checkSetupDone (IabHelper.java)
__null__.getResponseCodeFromBundle (IabHelper.java)
__null__.getResponseCodeFromIntent (IabHelper.java)
__null__.queryPurchases (IabHelper.java)
__null__.querySkuDetails (IabHelper.java)
com.myproj.util.IabHelper.startSetup (IabHelper.java)
__null__.dispose (IabHelper.java)
__null__.launchPurchaseFlow (IabHelper.java)
__null__.handleActivityResult (IabHelper.java)
__null__.queryInventory (IabHelper.java)
__null__.queryInventoryAsync (IabHelper.java)
__null__.getResponseDesc (IabHelper.java)
__null__.checkSetupDone (IabHelper.java)
__null__.getResponseCodeFromBundle (IabHelper.java)
__null__.getResponseCodeFromIntent (IabHelper.java)
__null__.queryPurchases (IabHelper.java)
__null__.querySkuDetails (IabHelper.java)
com.myproj.util.IabHelper$2.run (IabHelper.java)
java.lang.Thread.run (Thread.java:818)
这是 Google IabHelper 类的一些相关方法(可能您不需要阅读此内容)它们只是在错误日志中提到并由 Google 编写:
处置:
public void dispose() {
logDebug("Disposing.");
mSetupDone = false;
if (mServiceConn != null) {
logDebug("Unbinding from service.");
if (mContext != null) mContext.unbindService(mServiceConn);
}
mDisposed = true;
mContext = null;
mServiceConn = null;
mService = null;
mPurchaseListener = null;
}
开始设置:
public void startSetup(final OnIabSetupFinishedListener listener) {
// If already set up, can't do it again.
checkNotDisposed();
if (mSetupDone) throw new IllegalStateException("IAB helper is already set up.");
// Connection to IAB service
logDebug("Starting in-app billing setup.");
mServiceConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
logDebug("Billing service disconnected.");
mService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
if (mDisposed) return;
logDebug("Billing service connected.");
mService = IInAppBillingService.Stub.asInterface(service);
String packageName = mContext.getPackageName();
try {
logDebug("Checking for in-app billing 3 support.");
// check for in-app billing v3 support
int response = mService.isBillingSupported(3, packageName, ITEM_TYPE_INAPP);
if (response != BILLING_RESPONSE_RESULT_OK) {
if (listener != null) listener.onIabSetupFinished(new IabResult(response,
"Error checking for billing v3 support."));
// if in-app purchases aren't supported, neither are subscriptions.
mSubscriptionsSupported = false;
return;
}
logDebug("In-app billing version 3 supported for " + packageName);
// check for v3 subscriptions support
response = mService.isBillingSupported(3, packageName, ITEM_TYPE_SUBS);
if (response == BILLING_RESPONSE_RESULT_OK) {
logDebug("Subscriptions AVAILABLE.");
mSubscriptionsSupported = true;
}
else {
logDebug("Subscriptions NOT AVAILABLE. Response: " + response);
}
mSetupDone = true;
}
catch (RemoteException e) {
if (listener != null) {
listener.onIabSetupFinished(new IabResult(IABHELPER_REMOTE_EXCEPTION,
"RemoteException while setting up in-app billing."));
}
e.printStackTrace();
return;
}
if (listener != null) {
listener.onIabSetupFinished(new IabResult(BILLING_RESPONSE_RESULT_OK, "Setup successful."));
}
}
};
Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
if (!mContext.getPackageManager().queryIntentServices(serviceIntent, 0).isEmpty()) {
// service available to handle that Intent
mContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
}
else {
// no service available to handle that Intent
if (listener != null) {
listener.onIabSetupFinished(
new IabResult(BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE,
"Billing service unavailable on device."));
}
}
}
和 IabHelper 的构造函数:
public IabHelper(Context ctx, String base64PublicKey) {
mContext = ctx.getApplicationContext();
mSignatureBase64 = base64PublicKey;
logDebug("IAB helper created.");
}
Here是完整的 IabHelper 类。
回顾:
告诉我您是否可以在哪里/何时看到 NullPointerException
变量 mHelper
被抛出。 由于工作正常,我无法从代码中使用虚拟和物理设备进行测试。
我很确定这个错误是在 Activity 关闭后抛出的,但我不明白为什么要修复它。
如果您需要更多信息,请写信。感谢大家!
最佳答案
您应该尽量避免使用 getApplicationContext(),因为这会大大增加强制关闭的几率。
使用 Activity 上下文。如果您在 Activity 中创建 IabHelper 对象,则传递 ActivityName.this(表示 Activity 上下文)。
如果您在 Fragment 中,请使用 getActivity()。
并且在 IabHelper 类中使用这样的::
public IabHelper(Context ctx, String base64PublicKey) {
mContext = ctx;
mSignatureBase64 = base64PublicKey;
logDebug("IAB helper created.");
}
我希望它能正常工作。
关于java - Android 应用内购买 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41828237/
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 6年前关闭。 Improve t
我们有一个专有的销售系统,我们已经使用了一段时间了。最近我们添加了“购买”方面,以便我们可以比较匹配产品的平均购买/销售价格以及查看库存状况。 在 MySQL 中,我有 2 个表:tblPurchas
在查看 Paypal 文档以寻找针对这种情况的解决方案后,我一头雾水。我想要的是一种让购物车订阅(定期付款)和购买商品的方法。有没有一种方法可以解决这个问题,或者我是否必须做一些自定义的事情(如果我使
我想知道是否可以使用youtube api获取可购买或可租借的电影列表。当我转到youtube网站并登录到Google帐户时,我可以看到要购买的电影及其价格。 我想在我的应用程序(http://www
我使用 JavaScript 购买 SDK 和 Node.js。 const fetch = require('node-fetch'); const shopify = require('shopi
我购买了三个不同期限的不同订阅。我已经配置了测试账户,我可以进行测试购买。对于这些购买,谷歌不向我收费,但它们看起来非常像真实的。购买成功后,应用内结算会向我发送一些有关我的购买的数据,例如 pack
我目前正在实现应用内购买,并且刚刚阅读了一些帖子,说需要恢复购买按钮,否则苹果将拒绝应用。 我不想在我的 UI 设计中添加第二个按钮。 所以我的问题是... 有没有办法检查用户之前是否进行过应用内购买
我的应用中有多个项目。我有两个设备。如果我在这些设备中的第一个上购买商品,然后尝试在另一个设备上购买相同的商品,我不能。(Google play intent 显示消息 - 商品已拥有!然后它崩溃了.
有没有办法检测何时通过应用商店为您的应用进行了购买? 检测应用内购买似乎很容易(即我们的服务器可以收到通知),但是对于直接购买有没有办法做到这一点? 如果没有,是否有一些用户的唯一标识符(例如购买时通
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我计划在用户使用该应用程序成功扫描二维码时为应用内购买提供折扣。我知道无法为现有商品提供折扣。我打算以折扣价添加另一件商品。有没有人以前有过使用这种方法的经验? 提前致谢 最佳答案 没有办法直接这样做
我很好奇其他商店在基础应用程序框架方面做了什么?我将应用程序框架视为能够提供额外或扩展的功能以提高基于它构建的应用程序的质量。 有各种开箱即用的框架,例如 Spring(或 Spring.NET)等。
我们正在开发一款使用非续订订阅 IAP 模型的应用。在沙盒中测试订阅购买流程时,我们看到弹出两 strip 有“购买”按钮的消息。 显示第一条消息和产品信息:“您想以 xx.xx 美元购买一个订阅吗?
我的老板购买了 Microsoft 365,它包含三种产品。他现在要求我设计一个管理系统,比如员工自助服务门户。我特此寻求有关从哪里开始或使用哪种产品的建议,因为我对此很陌生。 我尝试了一些研究,发现
我的老板购买了 Microsoft 365,它包含三种产品。他现在要求我设计一个管理系统,比如员工自助服务门户。我特此寻求有关从哪里开始或使用哪种产品的建议,因为我对此很陌生。 我尝试了一些研究,发现
我刚刚了解了IAP Cracker的存在,并试图找出在我的应用中验证IAP购买的最佳方法。 我无法确定的是IAP Cracker是否可以处理“消耗性”商品。如果没有,我没有什么可担心的。 这是维护/验
我正在编写一个允许应用内购买的简单应用。我已经使用 SKU 代码 android.test.purchased 进行了测试,一切正常。 我进入我的 google play 控制台,创建了一个应用程序,
我即将启动一个应用程序,该应用程序将包含多个“应用程序内购买”。 我想做的是有一种方法可以提供少量免费的“应用内购买”来选择评论家等人。 在 apple 框架内有没有办法做到这一点,如果没有,我可以采
所以我在这个网站上工作,用户可以在该网站上发布他们的商品,其他用户可以将一些商品添加到他们的购物车并在线购买。 我考虑的流程是这样的: 商家发布商品及其信用卡/ Paypal 信息。 买家将(来自不同
我对这个主题进行了广泛的研究,但我的知识仍然很模糊。我正在寻找一个简单站点的基本 DV,但我看到每个在线 SSL 都具有三个级别, Root->Intermidiate (充当 Root 的代理)和我
我是一名优秀的程序员,十分优秀!