IAB startSetup NullPointerException

时间:2013-12-17 05:48:36

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

我正在尝试在我的应用程序中设置应用程序内结算。我没有走得太远,并且在尝试启动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中触发了空指针异常。由于这是谷歌代码,我不知道如何解决这个问题。这是第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

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

4 个答案:

答案 0 :(得分:67)

Googles的另一个很好的例子是写一次,让其他人解决我们的问题'。即使在这个时刻(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())

然后在配置代码中

替换

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

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

我希望这会有所帮助。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

有关

更新的代码尚未推送到SDK Manager,但您可以在此处查看与此问题相关的所有更改:

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

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

答案 3 :(得分:0)

实际上,它在这里返回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);
            }
        }