我想将In App Billing实施到我的应用程序中。我写了这段代码。
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();
}
}
}
当我启动应用程序并打开此Fragment时,应用程序崩溃时出现此错误:
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
在AndroidManifest中显然我拥有这些权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.android.vending.BILLING"/>
答案 0 :(得分:2)
在try
之前调用onServiceConnected
阻止,这就是mService
为null
的原因。将try
块移到onServiceConnected
内并在分配后移动,如下所示:
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;
}
};