我创建了一个包含Google billing v3的简单应用。它只有一个简单的按钮,您点击它,付款完成。到目前为止,该应用程序已上传为Beta版并且可以正常运行。但是当你启动它并点击支付按钮时它会崩溃。以下是我从Google Play开发者控制台崩溃报告中获得的例外情况。
java.lang.NullPointerException
at victory.walkto.testingpaymentsb.MainActivity$2.
onClick(MainActivity.java:92)
at android.view.View.performClick(View.java:4502)
at android.view.View$PerformClick.run(View.java:18857)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5420)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:857)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:673)
at dalvik.system.NativeStart.main(Native Method)
我真的不明白。为什么我会得到空指针异常?代码如下。
public class MainActivity extends Activity {
IInAppBillingService mService;
ServiceConnection connection;
String inappid = "victory.walkto.testingpaymentsb.productid"; //replace this with your in-app product id
Button purchaseBtn;
Button clickBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
// first this
connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mService = IInAppBillingService.Stub.asInterface(service);
}
};
// then this
bindService(serviceIntent,connection, Context.BIND_AUTO_CREATE);
purchaseBtn = (Button) findViewById(R.id.purchase);
clickBtn = (Button) findViewById(R.id.clickButton);
clickBtn.setVisibility(View.GONE);
purchaseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList skuList = new ArrayList();
skuList.add(inappid);
Bundle querySkus = new Bundle();
querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
Bundle skuDetails;
try {
skuDetails = mService.getSkuDetails(3, getPackageName(),
"inapp", querySkus);
int response = skuDetails.getInt("RESPONSE_CODE");
if (response == 0) {
ArrayList<String> responseList = skuDetails
.getStringArrayList("DETAILS_LIST");
for (String thisResponse : responseList) {
JSONObject object = new JSONObject(thisResponse);
String sku = object.getString("productId");
String price = object.getString("price");
if (sku.equals(inappid)) {
System.out.println("price " + price);
Bundle buyIntentBundle = mService
.getBuyIntent(3, getPackageName(), sku,
"inapp",
"bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
PendingIntent pendingIntent = buyIntentBundle
.getParcelable("BUY_INTENT");
startIntentSenderForResult(
pendingIntent.getIntentSender(), 1001,
new Intent(), Integer.valueOf(0),
Integer.valueOf(0), Integer.valueOf(0));
purchaseBtn.setVisibility(View.GONE);
clickBtn.setVisibility(View.VISIBLE);
clickBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"I am clicked",Toast.LENGTH_SHORT).show();
}
});
}
}
}
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IntentSender.SendIntentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1001) {
String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
if (resultCode == RESULT_OK) {
try {
JSONObject jo = new JSONObject(purchaseData);
String sku = jo.getString(inappid);
Toast.makeText(
MainActivity.this,
"You have bought the " + sku
+ ". Excellent choice,adventurer!",
Toast.LENGTH_LONG).show();
} catch (JSONException e) {
System.out.println("Failed to parse purchase data.");
e.printStackTrace();
}
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (connection != null) {
unbindService(connection);
}
}
}