Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

android - IAB startSetup NullPointerException

I am attempting to setup In-App Billing in my application. I haven't got very far and am running into a null pointer exception when trying to start my IabHelper. I am following 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");
            }
        });
    }
}

In the below logcat, it appears that the null pointer exception is being triggered in IabHelper.java on line 267. Since this is google code, I'm not sure how to fix this. Here is line 267.

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

Here is my LogCat with the error:

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

Edit: I'm still not sure the reason for the error. However, I have found a useful tutorial that has been better than the google documentation. http://www.techotopia.com/index.php/Integrating_Google_Play_In-app_Billing_into_an_Android_Application_%E2%80%93_A_Tutorial

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Another fine example of Googles 'write once, let others fix our problems'. Even at this moment (June 2014) the 'patch' is still not in the released sdk. To solve the problem replace

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

with

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

Then in the dispose code

replace

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

with

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

I hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...