【问题标题】:IAB startSetup NullPointerExceptionIAB startSetup NullPointerException
【发布时间】:2013-12-17 05:48:36
【问题描述】:

我正在尝试在我的应用程序中设置应用内计费。我没有走多远,在尝试启动我的 IabHelper 时遇到了空指针异常。我正在关注this google tutorial

import com.iabtest.util.IabHelper;
import com.iabtest.util.IabResult;

public class MainActivity extends Activity
{
    IabHelper mHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String base64EncodedPublicKey = "My_secret_key";

        mHelper = new IabHelper(this, base64EncodedPublicKey);
        mHelper.enableDebugLogging(true); //Turned on to try to help solve the issue
        Log.d("TEST", "Starting setup.");
        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener()
        {
            public void onIabSetupFinished(IabResult result)
            {
                Log.d("TEST", "Setup finished.");

                if(!result.isSuccess())
                {
                    // Oh noes, there was a problem.
                    Log.d("TEST", "Problem setting up in-app billing: " + result);
                    return;
                }

                //IAB SET UP!
                Log.d("TEST", "IAB ready");
            }
        });
    }
}

在下面的 logcat 中,似乎在第 267 行的 IabHelper.java 中触发了空指针异常。由于这是 google 代码,我不确定如何解决此问题。这是第 267 行。

if (!mContext.getPackageManager().queryIntentServices(serviceIntent, 0).isEmpty()) {

这是我的 LogCat 错误:

12-17 05:28:29.908: E/Trace(1478): error opening trace file: No such file or directory (2)

12-17 05:28:30.898: W/GooglePlayServicesUtil(1478): Google Play Store is missing.

12-17 05:28:31.838: D/TEST(1478): Starting setup.

12-17 05:28:31.838: D/IabHelper(1478): Starting in-app billing setup.

12-17 05:28:31.848: D/AndroidRuntime(1478): Shutting down VM

12-17 05:28:31.848: W/dalvikvm(1478): threadid=1: thread exiting with uncaught exception (group=0x40a71930)

12-17 05:28:31.878: E/AndroidRuntime(1478): FATAL EXCEPTION: main

12-17 05:28:31.878: E/AndroidRuntime(1478): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.iabtest/com.iabtest.MainActivity}: java.lang.NullPointerException

12-17 05:28:31.878: E/AndroidRuntime(1478):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)

12-17 05:28:31.878: E/AndroidRuntime(1478):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)

12-17 05:28:31.878: E/AndroidRuntime(1478):     at android.app.ActivityThread.access$600(ActivityThread.java:141)

12-17 05:28:31.878: E/AndroidRuntime(1478):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)

12-17 05:28:31.878: E/AndroidRuntime(1478):     at android.os.Handler.dispatchMessage(Handler.java:99)

12-17 05:28:31.878: E/AndroidRuntime(1478):     at android.os.Looper.loop(Looper.java:137)

12-17 05:28:31.878: E/AndroidRuntime(1478):     at android.app.ActivityThread.main(ActivityThread.java:5041)

12-17 05:28:31.878: E/AndroidRuntime(1478):     at java.lang.reflect.Method.invokeNative(Native Method)

12-17 05:28:31.878: E/AndroidRuntime(1478):     at java.lang.reflect.Method.invoke(Method.java:511)

12-17 05:28:31.878: E/AndroidRuntime(1478):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

12-17 05:28:31.878: E/AndroidRuntime(1478):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

12-17 05:28:31.878: E/AndroidRuntime(1478):     at dalvik.system.NativeStart.main(Native Method)

12-17 05:28:31.878: E/AndroidRuntime(1478): Caused by: java.lang.NullPointerException

12-17 05:28:31.878: E/AndroidRuntime(1478):     at com.iabtest.util.IabHelper.startSetup(IabHelper.java:267)

12-17 05:28:31.878: E/AndroidRuntime(1478):     at com.iabtest.MainActivity.onCreate(MainActivity.java:112)

12-17 05:28:31.878: E/AndroidRuntime(1478):     at android.app.Activity.performCreate(Activity.java:5104)

12-17 05:28:31.878: E/AndroidRuntime(1478):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)

12-17 05:28:31.878: E/AndroidRuntime(1478):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

12-17 05:28:31.878: E/AndroidRuntime(1478):     ... 11 more

编辑:我仍然不确定错误的原因。但是,我发现了一个比谷歌文档更好的有用教程。 http://www.techotopia.com/index.php/Integrating_Google_Play_In-app_Billing_into_an_Android_Application_%E2%80%93_A_Tutorial

【问题讨论】:

  • 你在课堂上添加onActivityResult了吗?
  • 我没有。我正在按照上面链接的教程进行操作,但它没有包含在 ActivityResult 中。
  • 什么是 com.iabtest.MainActivity.onCreate(MainActivity.java:112)?
  • 第 112 行是 mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() 为了便于阅读,我在 onCreate 方法中删除了一些其他内容
  • 您的项目中添加了google play库吗?请添加您的完整代码,以便人们理解问题。

标签: android in-app-purchase in-app-billing


【解决方案1】:

Google 的另一个很好的例子是“编写一次,让其他人解决我们的问题”。即使在这一刻(2014 年 6 月),“补丁”仍然不在已发布的 sdk 中。解决问题替换

 if (!mContext.getPackageManager().queryIntentServices(serviceIntent, 0).isEmpty()) {

  PackageManager pm=mContext.getPackageManager();
  List<ResolveInfo> intentServices = pm.queryIntentServices(serviceIntent, 0);
  if (intentServices != null && !intentServices.isEmpty())

然后在dispose代码中

替换

  if (mContext != null) mContext.unbindService(mServiceConn);

  if (mContext != null && mService!=null) mContext.unbindService(mServiceConn);

我希望这会有所帮助。

【讨论】:

  • 你也应该像这样“try .. catch”包围 unbindService: if (mContext != null && mServiceConn != null) { try {mContext.unbindService(mServiceConn);} catch (Exception e) {logDebug(e.getMessage().toString());} 因为 unbindService 可能会抛出“服务未注册”之类的异常。当设备上未安装 Google Billing Services 时,可能会发生这种情况。而这个异常 100% 在模拟器上抛出。
  • @a4vi2r,确实可以抛出“服务未注册”异常。然而,仅仅吞下这个例外并不是一个好主意。更好的是开始安装 Google Play 服务的标准流程(参见文档)。我认为这实际上在教程的源代码中得到了证明。
  • @Werner Van Belle,很遗憾我没有在教程 /extras/google/play_billing/ 中找到这个
  • 非常感谢。在我的应用程序中有很多关于此的崩溃,我希望这会起作用。
  • 虽然代码在我的测试手机上运行,​​但在检查谷歌分析时,我收到 539 次“NullPointerException (@IabHelper:startSetup:266) {main}”崩溃。难怪很少有人能够运行我的游戏应用程序。我也刚刚下载了新文件的游戏服务文件夹,令人惊讶的是他们仍然没有修复这个错误。
【解决方案2】:

请添加谷歌播放服务的jar文件。这是关于缺少谷歌播放服务的错误

【讨论】:

  • 我已经导入了 google-play-services_lib 并且我的项目正在使用该库(在属性 > Android > 库中设置)。我还需要做些什么来初始化 google play 服务吗?
  • com.iabtest.MainActivity.onCreate(MainActivity.java:112) 的行是什么
  • 第 112 行是 mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() 为了便于阅读,我在 onCreate 方法中删除了一些其他内容
【解决方案3】:

这个问题可能与here提出的问题有关

更新的代码还没有推送到 SDK Manager,但是你可以在这里查看所有与这个问题相关的变化:

https://code.google.com/p/marketbilling/source/detail?r=7ec85a9b619fc5f85023bc8125e7e6b1ab4dd69f

有 4 个文件受此问题影响。进行他们建议的更改,看看您是否仍然遇到同样的问题。

【讨论】:

  • 看来4个文件的修改已经推送了。我的文件与差异匹配。
  • 这就是整个代码。我注释掉了其余的代码,所以唯一运行的是我上面发布的代码。我清理了 cmets,使其更易于阅读。
  • 正如我在回答中告诉您的,这可能是因为某些文件已更新并且您的文件已旧。试用更新的库。
  • 您链接的库已被推送。我有更新的文件。
【解决方案4】:

其实就是在这里返回null。

Intent serviceIntent = getExplicitIapIntent();

处理这种情况:

 Intent serviceIntent = getExplicitIapIntent();

        if(serviceIntent != null) {

            if (!mContext.getPackageManager().queryIntentServices(serviceIntent, 0).isEmpty()) {
                // service available to handle that Intent
                mContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
            }
        }

【讨论】:

    猜你喜欢
    • 2013-02-10
    • 1970-01-01
    • 2013-08-03
    • 2015-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多