Android Google计费V3空指针异常
基本上,我想先对Google Billing V3进行静态测试,然后再使用Beta版进行真正的在线测试。当我尝试运行我的程序时,我得到以下部分异常Android Google计费V3空指针异常,android,exception,billing,Android,Exception,Billing,基本上,我想先对Google Billing V3进行静态测试,然后再使用Beta版进行真正的在线测试。当我尝试运行我的程序时,我得到以下部分异常 Caused by: java.lang.IllegalArgumentException: connection is null at android.app.ContextImpl.bindServiceCommon (ContextImpl.java:1935) at and
Caused by: java.lang.IllegalArgumentException: connection is
null at android.app.ContextImpl.bindServiceCommon
(ContextImpl.java:1935)
at android.app.ContextImpl.bindService(ContextImpl.java:1921)
at android.content.ContextWrapper.bindService
(ContextWrapper.java:529)
at victory.walkto.paymenttestb.MainActivity.
onCreate(MainActivity.java:37)
at android.app.Activity.performCreate(Activity.java:5451)
我写的程序如下
public class MainActivity extends Activity {
IInAppBillingService mService;
ServiceConnection connection;
String inappid = "android.test.purchased"; //replace this with your in-app product id
@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");
bindService(serviceIntent,connection, Context.BIND_AUTO_CREATE);
connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mService = IInAppBillingService.Stub.asInterface(service);
}
};
Button purchaseBtn = (Button) findViewById(R.id.purchase);
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));
}
}
}
} 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);
}
}
}
公共类MainActivity扩展活动{
IInapplingService mService;
服务连接;
String inappid=“android.test.purchased”;//将其替换为应用内产品id
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent serviceentent=newintent(“com.android.vending.billing.InAppBillingService.BIND”);
setPackage(“com.android.vending”);
bindService(serviceIntent、connection、Context.BIND\u AUTO\u CREATE);
连接=新服务连接(){
@凌驾
ServiceDisconnected上的公共无效(组件名称){
mService=null;
}
@凌驾
服务连接上的公共无效(组件名称,IBinder服务){
mService=IInAppBillingService.Stub.asInterface(服务);
}
};
Button purchaseBtn=(按钮)findViewById(R.id.purchase);
purchaseBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
ArrayList skuList=新的ArrayList();
skuList.add(inappid);
Bundle querySkus=新Bundle();
querySkus.putStringArrayList(“项目ID列表”,skuList);
详细信息;
试一试{
skuDetails=mService.getSkuDetails(3,getPackageName(),
“inapp”,querySkus);
int response=skuDetails.getInt(“response_CODE”);
如果(响应==0){
ArrayList responseList=skuDetails
.getStringArrayList(“详细信息列表”);
for(字符串thisResponse:responseList){
JSONObject对象=新的JSONObject(此响应);
String sku=object.getString(“productId”);
String price=object.getString(“price”);
如果(sku等于(不适用)){
系统输出打印项次(“价格”+价格);
Bundle buyIntentBundle=mService
.getBuyIntent(3,getPackageName(),sku,
“inapp”,
“bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ”);
PendingEvent PendingEvent=购买意向绑定
.getParcelable(“购买意向”);
startinentsenderforresult(
PendingEvent.getIntentSender(),1001,
new Intent(),Integer.valueOf(0),
Integer.valueOf(0),Integer.valueOf(0));
}
}
}
}捕获(远程异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}catch(IntentSender.sendtintentexe){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
});
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
if(requestCode==1001){
String purchaseData=data.getStringExtra(“INAPP_purchaseData”);
if(resultCode==RESULT\u OK){
试一试{
JSONObject jo=新的JSONObject(purchaseData);
字符串sku=jo.getString(inappid);
Toast.makeText(
这个,,
“您已购买”+sku
+“太好了,冒险家!”,
Toast.LENGTH_LONG).show();
}捕获(JSONException e){
System.out.println(“未能分析采购数据”);
e、 printStackTrace();
}
}
}
}
@凌驾
公共空间{
super.ondestory();
if(连接!=null){
解除绑定服务(连接);
}
}
}
我还发现,如果你使用的是希腊账户,谷歌钱包无法下载到你的手机上!!!由于银行问题,贝宝也停止了在希腊的业务 您需要首先创建服务连接,然后绑定服务
// 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);
谢谢你,伙计。测试版本正在运行。我想问你一些别的问题。我应该使用相同的有效负载字符串进行真正的在线测试吗?或者我应该生成一个新的?最后,我应该在appid中的字符串中输入什么?我的应用程序包名后跟productid(开发者控制台中添加的产品的标识符)?i、 e..victory.walkto.paymenttestb.ProductIDIAppId和productId是开发控制台中定义的应用内产品的相同字符串-id。第一个(inappid)用于准确查询有关此产品id的信息。如果您有多个产品,则会将多个inappid添加到列表中。第二个(productId)用于从响应中提取具有正确id的产品。只有当你有多个产品时,它才有意义。如果只有一种产品,响应将仅包含该产品的详细信息。