在应用空指针异常 [英] In App Null Pointer Exception

查看:316
本文介绍了在应用空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时特林实现在应用内结算到我的应用程序。我写这篇code。

 公共类设置扩展$ P $ {pferenceFragment    ServiceConnection mServiceConn;
    IInAppBillingService MSERVICE;
    悬而未决的PendingIntent;
    意图意图;
    捆束;    @覆盖
    公共无效的onDestroy(){
        super.onDestroy();
        如果(MSERVICE!= NULL){
            。getActivity()unbindService(mServiceConn);
        }
    }    @覆盖
    公共无效的onCreate(捆绑savedIstanceState){
        super.onCreate(savedIstanceState);
        加preferencesFromResource(R.xml.settings);        。getActivity()getActionBar()的setTitle(的getString(R.string.settings));         mServiceConn =新ServiceConnection(){
                @覆盖
                公共无效onServiceConnected(组件名称为arg0,ARG1的IBinder){
                    // TODO自动生成方法存根
                       MSERVICE = IInAppBillingService.Stub.asInterface(ARG1);
                }                @覆盖
                公共无效onServiceDisconnected(组件名称为arg0){
                    // TODO自动生成方法存根
                    MSERVICE = NULL;
                }
            };            意图=新意图(com.android.vending.billing.InAppBillingService.BIND)setPackage(com.android.vending);
            getActivity()bindService(意向,mServiceConn,Context.BIND_AUTO_CREATE)。            尝试{
                捆绑= mService.getBuyIntent(3,getActivity()getPackageName(),pro_version,应用程式内,bGoa + V7g / yqDXvKRqq + JTFn4uQZbPiQJo4pf9RzJ);
                未决= bundle.getParcelable(BUY_INTENT);
                。getActivity()startIntentSenderForResult(pending.getIntentSender(),1001,新意图(),Integer.valueOf(0),Integer.valueOf(0),Integer.valueOf(0));
            }赶上(RemoteException的E){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(SendIntentException E){
                // TODO自动生成catch块
                e.printStackTrace();
            }    }}

当我启动应用程序,并打开这个片段的应用程序崩溃与此错误:

  01-31 19:59:30.969:E / AndroidRuntime(11546):显示java.lang.NullPointerException:尝试调用接口方法android.os.Bundle com.android.vending .billing.IInAppBillingService.getBuyIntent(INT,java.lang.String中,java.lang.String中,java.lang.String中,java.lang.String中)一空对象引用

在AndroidManifest显然我这些权限:

 <使用许可权的android:NAME =android.permission.ACCESS_NETWORK_STATE/>
 <使用许可权的android:NAME =android.permission.INTERNET对/>
 <使用许可权的android:NAME =com.android.vending.BILLING/>


解决方案

尝试块之前 onServiceConnected 称为是,这就是为什么 MSERVICE 。移动尝试里面块 onServiceConnected 键,像这样的分配之后:

  mServiceConn =新ServiceConnection(){
            @覆盖
            公共无效onServiceConnected(组件名称为arg0,ARG1的IBinder){
                MSERVICE = IInAppBillingService.Stub.asInterface(ARG1);
                尝试{
                    捆绑= mService.getBuyIntent(3,getActivity()getPackageName(),pro_version,应用程式内,bGoa + V7g / yqDXvKRqq + JTFn4uQZbPiQJo4pf9RzJ);
                    未决= bundle.getParcelable(BUY_INTENT);
                    。getActivity()startIntentSenderForResult(pending.getIntentSender(),1001,新意图(),Integer.valueOf(0),Integer.valueOf(0),Integer.valueOf(0));
                }赶上(RemoteException的E){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }赶上(SendIntentException E){
                     // TODO自动生成catch块
                    e.printStackTrace();
                }
            }            @覆盖
            公共无效onServiceDisconnected(组件名称为arg0){
                // TODO自动生成方法存根
                MSERVICE = NULL;
            }
        };

i'm tring to implement In App Billing into my application. I wrote this code.

public class Settings extends PreferenceFragment {

    ServiceConnection mServiceConn;
    IInAppBillingService mService;
    PendingIntent pending;
    Intent intent;
    Bundle bundle;

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mService != null) {
            getActivity().unbindService(mServiceConn);
        }   
    }

    @Override
    public void onCreate(Bundle savedIstanceState) {
        super.onCreate(savedIstanceState);
        addPreferencesFromResource(R.xml.settings);

        getActivity().getActionBar().setTitle(getString(R.string.settings));

         mServiceConn = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName arg0, IBinder arg1) {
                    // TODO Auto-generated method stub
                       mService = IInAppBillingService.Stub.asInterface(arg1);
                }

                @Override
                public void onServiceDisconnected(ComponentName arg0) {
                    // TODO Auto-generated method stub
                    mService = null;
                }
            };

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

            try {
                bundle = mService.getBuyIntent(3, getActivity().getPackageName(), "pro_version", "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
                pending = bundle.getParcelable("BUY_INTENT");
                getActivity().startIntentSenderForResult(pending.getIntentSender(), 1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SendIntentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    }

}

When i launch the application and open this Fragment the app crash with this error:

01-31 19:59:30.969: E/AndroidRuntime(11546): java.lang.NullPointerException: Attempt to invoke interface method 'android.os.Bundle com.android.vending.billing.IInAppBillingService.getBuyIntent(int, java.lang.String, java.lang.String, java.lang.String, java.lang.String)' on a null object reference

In the AndroidManifest obviously i've these permissions:

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>     
 <uses-permission android:name="android.permission.INTERNET"/> 
 <uses-permission android:name="com.android.vending.BILLING"/>

解决方案

Your try block is called before onServiceConnected is, that's why mService is null. Move the try block inside the onServiceConnected and after the assignment like so:

    mServiceConn = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName arg0, IBinder arg1) {
                mService = IInAppBillingService.Stub.asInterface(arg1);
                try {
                    bundle = mService.getBuyIntent(3, getActivity().getPackageName(), "pro_version", "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
                    pending = bundle.getParcelable("BUY_INTENT");
                    getActivity().startIntentSenderForResult(pending.getIntentSender(), 1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0));
                } catch (RemoteException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SendIntentException e) {
                     // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName arg0) {
                // TODO Auto-generated method stub
                mService = null;
            }
        };

这篇关于在应用空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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