为什么会出现在我的Andr​​oid code空指针错误? [英] Why am I getting a null pointer error in my Android Code?

查看:108
本文介绍了为什么会出现在我的Andr​​oid code空指针错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个空点错误在我的code和日志在这里指点

  199公共无效getPurchasedSubs()将抛出RemoteException {
200捆绑ownedItems = mService.getPurchases(3 getPackageName(),潜艇,NULL);

下面是logcat的文件转储

  03-08 13:24:45.619:E / AndroidRuntime(7149):致命异常:主要
03-08 13:24:45.619:E / AndroidRuntime(7149):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.imobilize.ETV_V3_INAPP_SUB / com.imobilize.ETV_V3_INAPP_SUB.IntroBLevActivity}:显示java.lang.NullPointerException
03-08 13:24:45.619:E / AndroidRuntime(7149):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
03-08 13:24:45.619:E / AndroidRuntime(7149):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
03-08 13:24:45.619:E / AndroidRuntime(7149):在android.app.ActivityThread.access $ 600(ActivityThread.java:128)
03-08 13:24:45.619:E / AndroidRuntime(7149):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1161)
03-08 13:24:45.619:E / AndroidRuntime(7149):在android.os.Handler.dispatchMessage(Handler.java:99)
03-08 13:24:45.619:E / AndroidRuntime(7149):在android.os.Looper.loop(Looper.java:137)
03-08 13:24:45.619:E / AndroidRuntime(7149):在android.app.ActivityThread.main(ActivityThread.java:4517)
03-08 13:24:45.619:E / AndroidRuntime(7149):在java.lang.reflect.Method.invokeNative(本机方法)
03-08 13:24:45.619:E / AndroidRuntime(7149):在java.lang.reflect.Method.invoke(Method.java:511)
03-08 13:24:45.619:E / AndroidRuntime(7149):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:980)
03-08 13:24:45.619:E / AndroidRuntime(7149):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
03-08 13:24:45.619:E / AndroidRuntime(7149):在dalvik.system.NativeStart.main(本机方法)
03-08 13:24:45.619:E / AndroidRuntime(7149):因:显示java.lang.NullPointerException
03-08 13:24:45.619:E / AndroidRuntime(7149):在com.imobilize.ETV_V3_INAPP_SUB.IntroBLevActivity.getPurchasedSubs(IntroBLevActivity.java:200)
03-08 13:24:45.619:E / AndroidRuntime(7149):在com.imobilize.ETV_V3_INAPP_SUB.IntroBLevActivity.checkValidation(IntroBLevActivity.java:238)
03-08 13:24:45.619:E / AndroidRuntime(7149):在com.imobilize.ETV_V3_INAPP_SUB.IntroBLevActivity.onCreate(IntroBLevActivity.java:96)
03-08 13:24:45.619:E / AndroidRuntime(7149):在android.app.Activity.performCreate(Activity.java:4470)
03-08 13:24:45.619:E / AndroidRuntime(7149):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)

SPECS此方法

  / **
 *返回的类型和包名以及指定的用户所拥有的单品电流
 *购买信息和数据的签名进行验证。
 *这将返回已购买的V3和管理项目使用购买的所有SKU
 * V1和V2尚未消耗掉。
 该应用程序使用*参数apiVersion计费API版本
 * @参数的packageName包调用应用程序的名称
 * @参数类型为请求的应用程序内的项目类型
 *(应用程式内一次性购买,潜艇的订阅)。
 * @参数continuationToken被设置为空的第一个电话,如果拥有的数
 *单品太多,一个continuationToken在响应包返回。
 *这个方法可以再次延续标记被调用,以获得下一组
 *拥有的单品。
 包含下列键值对* @返回捆绑
 *RESPONSE_ code用int值,RESULT_OK(0),如果成功,其他响应codeS上
 *如上面列出的失败。
 *INAPP_PURCHASE_ITEM_LIST - 包含的SKU列表StringArrayList
 *INAPP_PURCHASE_DATA_LIST - 包含购买信息StringArrayList
 *INAPP_DATA_SIGNATURE_LIST - 包含签名StringArrayList
 的采购信息*
 *INAPP_CONTINUATION_TOKEN - 包含一个延续标记字符串
 *下一组应用内购买的。只有设置为在
 *用户拥有更多的SKU比当前列表。
 * /
捆绑getPurchases(INT apiVersion,字符串的packageName,字符串类型,字符串continuationToken);

当我检查的包名它回来有效

下面是创建MSERVICE的code
        IInAppBillingService MSERVICE;

  ServiceConnection mServiceConn =新ServiceConnection(){
   // @超越控制
       公共无效onServiceDisconnected(组件名名){
       MSERVICE = NULL;
   }
       // @超越控制
   公共无效onServiceConnected(组件名的名字,
      的IBinder服务){
       MSERVICE = IInAppBillingService.Stub.asInterface(服务);
   }};

我并不擅长间preting的logcat的文件,所以答案可能在我面前是正确的。

这件code的我以为是应该打开它,所以我coud使用MSERVICE随时随地。

  bindService(新
            意图(com.android.vending.billing.InAppBillingService.BIND),
                    mServiceConn,Context.BIND_AUTO_CREATE);


解决方案

我觉得你用 MSERVICE 之前连接的服务。

我的建议是使用布尔变量来检查活动连接服务与否,
或者使用 getPurchasedSubs() onServiceConnected 是这样的:

  ServiceConnection mServiceConn =新ServiceConnection(){
   公共无效onServiceDisconnected(组件名名){
       MSERVICE = NULL;
   }   公共无效onServiceConnected(组件名的名字,
      的IBinder服务){
       MSERVICE = IInAppBillingService.Stub.asInterface(服务);
getPurchasedSubs();
   }};

希望这有助于你。

I am getting a null point error in my code and the log is pointing here

199     public void getPurchasedSubs () throws RemoteException {
200         Bundle ownedItems = mService.getPurchases(3, getPackageName(), "subs", null);

Here is the logcat file dump

03-08 13:24:45.619: E/AndroidRuntime(7149): FATAL EXCEPTION: main
03-08 13:24:45.619: E/AndroidRuntime(7149): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.imobilize.ETV_V3_INAPP_SUB/com.imobilize.ETV_V3_INAPP_SUB.IntroBLevActivity}: java.lang.NullPointerException
03-08 13:24:45.619: E/AndroidRuntime(7149):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at android.app.ActivityThread.access$600(ActivityThread.java:128)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at android.os.Looper.loop(Looper.java:137)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at android.app.ActivityThread.main(ActivityThread.java:4517)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at java.lang.reflect.Method.invokeNative(Native Method)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at java.lang.reflect.Method.invoke(Method.java:511)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at dalvik.system.NativeStart.main(Native Method)
03-08 13:24:45.619: E/AndroidRuntime(7149): Caused by: java.lang.NullPointerException
03-08 13:24:45.619: E/AndroidRuntime(7149):     at com.imobilize.ETV_V3_INAPP_SUB.IntroBLevActivity.getPurchasedSubs(IntroBLevActivity.java:200)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at com.imobilize.ETV_V3_INAPP_SUB.IntroBLevActivity.checkValidation(IntroBLevActivity.java:238)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at com.imobilize.ETV_V3_INAPP_SUB.IntroBLevActivity.onCreate(IntroBLevActivity.java:96)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at android.app.Activity.performCreate(Activity.java:4470)
03-08 13:24:45.619: E/AndroidRuntime(7149):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)

SPECS for this method

    /**
 * Returns the current SKUs owned by the user of the type and package name specified along with
 * purchase information and a signature of the data to be validated.
 * This will return all SKUs that have been purchased in V3 and managed items purchased using
 * V1 and V2 that have not been consumed.
 * @param apiVersion billing API version that the app is using
 * @param packageName package name of the calling app
 * @param type the type of the in-app items being requested
 *        ("inapp" for one-time purchases and "subs" for subscription).
 * @param continuationToken to be set as null for the first call, if the number of owned
 *        skus are too many, a continuationToken is returned in the response bundle.
 *        This method can be called again with the continuation token to get the next set of
 *        owned skus.
 * @return Bundle containing the following key-value pairs
 *         "RESPONSE_CODE" with int value, RESULT_OK(0) if success, other response codes on
 *              failure as listed above.
 *         "INAPP_PURCHASE_ITEM_LIST" - StringArrayList containing the list of SKUs
 *         "INAPP_PURCHASE_DATA_LIST" - StringArrayList containing the purchase information
 *         "INAPP_DATA_SIGNATURE_LIST"- StringArrayList containing the signatures
 *                                      of the purchase information
 *         "INAPP_CONTINUATION_TOKEN" - String containing a continuation token for the
 *                                      next set of in-app purchases. Only set if the
 *                                      user has more owned skus than the current list.
 */
Bundle getPurchases(int apiVersion, String packageName, String type, String continuationToken);

When I check for the package name it comes back valid

Here is the code where mService is created IInAppBillingService mService;

ServiceConnection mServiceConn = new ServiceConnection() {
   //@Overide
       public void onServiceDisconnected(ComponentName name) {
       mService = null;
   }
       //@Overide
   public void onServiceConnected(ComponentName name, 
      IBinder service) {
       mService = IInAppBillingService.Stub.asInterface(service);
   }

};  

I am not that good at interpreting the logcat file, so the answer may be right in front of me.

This piece of code I thought was supposed to open it up so I coud use mService anytime.

        bindService(new 
            Intent("com.android.vending.billing.InAppBillingService.BIND"),
                    mServiceConn, Context.BIND_AUTO_CREATE);

解决方案

I think you use mService before connect to service.

My suggestion is to use boolean variable to check if activity connect to service or not, or use getPurchasedSubs () in onServiceConnected like this:

ServiceConnection mServiceConn = new ServiceConnection() {
   public void onServiceDisconnected(ComponentName name) {
       mService = null;
   }

   public void onServiceConnected(ComponentName name, 
      IBinder service) {
       mService = IInAppBillingService.Stub.asInterface(service);
getPurchasedSubs ();
   }

};  

hope this helped you.

这篇关于为什么会出现在我的Andr​​oid code空指针错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆