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

原文 标签 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);
}

这里有什么问题?

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

而且,这里有更多详细信息:BillingService从我的应用程序的首选项屏幕开始。这里的代码来自 Preferences类(class):
@Override
protected void onDestroy() {
mBillingService.unbind();
    super.onDestroy();
}

更新。也许,我应该在主要 Activity 中启动此服务/解除绑定(bind)?

这是我的 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

最佳答案

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

您必须混淆一些您不应该喜欢的扩展应用程序/Activity/服务的类

你不应该混淆这些类:

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

关于android - 为什么使用标准 BillingService 会发生 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5684894/

相关文章:

Android:保持后台服务处于 Activity 状态(防止进程死亡)

android - 需要从 Sensor.TYPE_ORIENTATION 数据计算旋转矢量

java - 将上下文传递给异步类

r - 使用 cbind 动态扩展数据框列

android - 为什么不调用 mConnection.onServiceConnected 方法

linux - 自动重启崩溃的 Linux 守护进程

android - 图像识别后如何在vuforia sdk ImageTarget中显示布局而不是茶壶模型

android - 使用appcompat-v7在Actionmode中自定义 'back'箭头颜色

c# - 绑定(bind)不适用于自定义用户控件的依赖项属性

java - 我在Openshift中的diy应用程序的端口转发失败