为什么使用标准BillingService会发生NullPointerException?

时间:2011-04-16 06:21:42

标签: android binding service

我在我的应用程序中使用标准BillingService(即从sample application复制而不做更改)。 但有时我的应用程序崩溃了logcat中的以下数据:

04-16 10:05:43.556: INFO/ActivityManager(96): Start proc tv.kinobaza.app for service tv.kinobaza.app/tv.kinobaza.billing.BillingService: pid=28748 uid=10081 gids={3003}
04-16 10:05:43.646: DEBUG/AndroidRuntime(28748): Shutting down VM
04-16 10:05:43.656: WARN/dalvikvm(28748): threadid=1: thread exiting with uncaught exception (group=0x400259f8)
04-16 10:05:43.656: ERROR/AndroidRuntime(28748): FATAL EXCEPTION: main
04-16 10:05:43.656: ERROR/AndroidRuntime(28748): java.lang.RuntimeException: Unable to start service tv.kinobaza.billing.BillingService@4632f578 with null: java.lang.NullPointerException
04-16 10:05:43.656: ERROR/AndroidRuntime(28748):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3282)
04-16 10:05:43.656: ERROR/AndroidRuntime(28748):     at android.app.ActivityThread.access$3600(ActivityThread.java:135)
04-16 10:05:43.656: ERROR/AndroidRuntime(28748):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2211)
04-16 10:05:43.656: ERROR/AndroidRuntime(28748):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 10:05:43.656: ERROR/AndroidRuntime(28748):     at android.os.Looper.loop(Looper.java:144)
04-16 10:05:43.656: ERROR/AndroidRuntime(28748):     at android.app.ActivityThread.main(ActivityThread.java:4937)
04-16 10:05:43.656: ERROR/AndroidRuntime(28748):     at java.lang.reflect.Method.invokeNative(Native Method)
04-16 10:05:43.656: ERROR/AndroidRuntime(28748):     at java.lang.reflect.Method.invoke(Method.java:521)
04-16 10:05:43.656: ERROR/AndroidRuntime(28748):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-16 10:05:43.656: ERROR/AndroidRuntime(28748):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-16 10:05:43.656: ERROR/AndroidRuntime(28748):     at dalvik.system.NativeStart.main(Native Method)
04-16 10:05:43.656: ERROR/AndroidRuntime(28748): Caused by: java.lang.NullPointerException
04-16 10:05:43.656: ERROR/AndroidRuntime(28748):     at tv.kinobaza.billing.BillingService.onStart(Unknown Source)
04-16 10:05:43.656: ERROR/AndroidRuntime(28748):     at android.app.Service.onStartCommand(Service.java:420)
04-16 10:05:43.656: ERROR/AndroidRuntime(28748):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3267)
04-16 10:05:43.656: ERROR/AndroidRuntime(28748):     ... 10 more
04-16 10:05:43.736: DEBUG/SurfaceFlinger(96): Layer::setBuffers(this=0xb947a8), pid=96, w=1, h=1
04-16 10:05:43.736: DEBUG/SurfaceFlinger(96): Layer::setBuffers(this=0xb947a8), pid=96, w=1, h=1
04-16 10:05:43.766: DEBUG/SurfaceFlinger(96): Layer::requestBuffer(this=0xb947a8), index=0, pid=96, w=480, h=418 success
04-16 10:05:43.986: DEBUG/dalvikvm(28721): GC_FOR_MALLOC freed 9604 objects / 1369120 bytes in 102ms

以下是相关代码:

/**
 * We don't support binding to this service, only starting the service.
 */
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onStart(Intent intent, int startId) {
    handleCommand(intent, startId);
}

这里有什么问题?

我注意到了 - 当设备在夜间睡觉时,一旦我开始使用它,它会在约5分钟内崩溃。

以下是更多详细信息:BillingService从我的应用程序的“首选项”屏幕开始。这里是Preferences类的代码:

@Override
protected void onDestroy() {
mBillingService.unbind();
    super.onDestroy();
}

UPD。也许,我应该在Main活动中启动这项服务/解除绑定?

这是我的proguard.cfg:

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
-keep class com.android.vending.billing.**

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

-libraryjars /lib/libGoogleAnalytics.jar
-libraryjars /lib/signpost-commonshttp4-1.2.1.1.jar
-libraryjars /lib/signpost-core-1.2.1.1.jar
-dontwarn org.apache.commons.codec.binary.Base64

2 个答案:

答案 0 :(得分:4)

所以你在proguard之后得到这个NPE而不是事先。

你必须混淆一些你不应该喜欢扩展应用程序/活动/服务

的类

你不应该混淆这些类:

http://proguard.sourceforge.net/index.html#/manual/examples.html(转到示例7)

答案 1 :(得分:0)

只是指出显而易见的问题,但如果是问题,那么我们就不会在堆栈跟踪中看到他的计费服务名称。 (我们会看到一个混淆的名字,而不是tv.kinobaza.billing.BillingService.onStart)。

试试这个Why Android service crashes with NullPointerException?