我试图进入应用程序账单3工作,但不知道我做错了什么。我正在学习一个教程,但是我被卡住了。如果有人能帮助我让它工作,我将不胜感激。
我只是想让最简单的静态测试正常工作。我可以在我的手机上得到它,我正确地导入了android.vending.billing
和inappbillingservice.aidl
。
我在SkuDetails = mservice.getSkuDetails(3, getPackageName(), "inapp", querySkus)
拿到了NullPointerException
学位。
我可能还有其他错误,但这就是我目前所做的。
public class MainActivity extends ActionBarActivity {
IInAppBillingService mservice;
ServiceConnection connection;
String inappid= "android.test.purchased";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("oncreabte!!!!!!!!");
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
mservice = IInAppBillingService.Stub.asInterface(service);
System.out.println("it conncekcted!!!!!!!!");
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
System.out.println("it disconncected!!!!!!!!!!");
}
};
bindService (new Intent(
"com.android.vending.billing.InAppBillService.BIND"),
connection, Context.BIND_AUTO_CREATE);
Button purchaseBtn = (Button)findViewById(R.id.purchase);
System.out.println("bind!!!!!!!!!!");
purchaseBtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
System.out.println("onclccccick");
ArrayList skuList = new ArrayList();
skuList.add(inappid);
Bundle querySkus = new Bundle();
querySkus.putStringArrayList("ITEM_ID_LIST",skuList);
// Bundle SkuDetails;
System.out.println("right befor try!!!");
Bundle SkuDetails;
try{
System.out.println("right "+ skuList.get(0));
System.out.println( "mine! "+querySkus);
SkuDetails = mservice.getSkuDetails(3,
getPackageName(), "inapp", querySkus);
int responce = SkuDetails.getInt("RESPONCE_CODE");
System.out.println("Endddd!!!" + responce);
if(responce ==0){
System.out.println("Endddd!!!");
ArrayList<String> responceList = SkuDetails.getStringArrayList("DETAILS_LIST");
for (String thisResponse : responceList){
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) {
System.out.println("RemoteE");
e.printStackTrace();
}
catch (JSONException e) {
System.out.println("Remotejso");
e.printStackTrace();
}
catch (SendIntentException e) {
System.out.println("Remotein");
e.printStackTrace();
}
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == 1001) {
String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
if (resultCode == RESULT_OK) {
System.out.println("resuuut goood");
try{
JSONObject jo = new JSONObject(purchaseData);
String sku = jo.getString(inappid);
Toast.makeText(MainActivity.this,
"u have bought"+ sku,
Toast.LENGTH_LONG).show();
}
catch(JSONException e){
System.out.println("didnt work");
e.printStackTrace();
}
}
}
}
//end
public void onDestroy(){
super.onDestroy();
if(connection != null){
unbindService(connection);
}
}
}
例外:
10-11 16:48:15.944: E/AndroidRuntime(20612): FATAL EXCEPTION: main
10-11 16:48:15.944: E/AndroidRuntime(20612): java.lang.NullPointerException
10-11 16:48:15.944: E/AndroidRuntime(20612): at com.example.bill.MainActivity$2.onClick(MainActivity.java:91)
10-11 16:48:15.944: E/AndroidRuntime(20612): at android.view.View.performClick(View.java:4101)
10-11 16:48:15.944: E/AndroidRuntime(20612): at android.view.View$PerformClick.run(View.java:17085)
10-11 16:48:15.944: E/AndroidRuntime(20612): at android.os.Handler.handleCallback(Handler.java:615)
10-11 16:48:15.944: E/AndroidRuntime(20612): at android.os.Handler.dispatchMessage(Handler.java:92)
10-11 16:48:15.944: E/AndroidRuntime(20612): at android.os.Looper.loop(Looper.java:137)
10-11 16:48:15.944: E/AndroidRuntime(20612): at android.app.ActivityThread.main(ActivityThread.java:4941)
10-11 16:48:15.944: E/AndroidRuntime(20612): at java.lang.reflect.Method.invokeNative(Native Method)
10-11 16:48:15.944: E/AndroidRuntime(20612): at java.lang.reflect.Method.invoke(Method.java:511)
10-11 16:48:15.944: E/AndroidRuntime(20612): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
10-11 16:48:15.944: E/AndroidRuntime(20612): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
10-11 16:48:15.944: E/AndroidRuntime(20612): at dalvik.system.NativeStart.main(Native Method)
发布于 2014-10-12 13:11:42
我让它起作用了,我知道它远不是最好的例子,但对于这个问题,我找不到一个有效的例子,所以希望这能对某些人有所帮助。我打算放一张源文件的图片,这让我有点困惑,但没有足够的代表来添加一个img。我的例子来自https://www.youtube.com/watch?v=-h2ESH71hAI,感谢他。
公共类MainActivity扩展了ActionBarActivity {
IInAppBillingService mservice;
ServiceConnection connection;
String inappid = "android.test.purchased";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("oncreccabte!!!!!!!!");
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
System.out.println("it disconncected!!!!!!!!!!");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
mservice = IInAppBillingService.Stub.asInterface(service);
System.out.println("it cmonnxcekcted!!!!!!!!");
}
};
bindService (new Intent(
"com.android.vending.billing.InAppBillingService.BIND"), // had error here
connection, Context.BIND_AUTO_CREATE);
Button purchaseBtn = (Button)findViewById(R.id.purchase);
System.out.println("bind!!!!!!!!!!");
purchaseBtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
System.out.println("onclccccick");
ArrayList skuList = new ArrayList();
skuList.add(inappid);
Bundle querySkus = new Bundle();
querySkus.putStringArrayList("ITEM_ID_LIST",skuList);
// Bundle SkuDetails;
System.out.println("right befor try!!!");
Bundle SkuDetails;
try{
System.out.println("right "+ skuList.get(0));
System.out.println( "mine! "+querySkus);
SkuDetails = mservice.getSkuDetails(3,
getPackageName(), "inapp", querySkus);
int responce = SkuDetails.getInt("RESPONCE_CODE");
System.out.println("Endddd!!!" + responce);
if(responce ==0){
System.out.println("equls 0!!!");
ArrayList<String> responceList = SkuDetails.getStringArrayList("DETAILS_LIST");
for (String thisResponse : responceList){
JSONObject object = new JSONObject(thisResponse);
String sku = object.getString("productId");
String price = object.getString("price");
System.out.println("thr price" + price);
System.out.println("thr price" + sku);
//error had inappid in ""
if (sku.equals(inappid)){
System.out.println("it does prob");
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) {
System.out.println("RemoteE");
e.printStackTrace();
}
catch (JSONException e) {
System.out.println("Remotejso");
e.printStackTrace();
}
catch (SendIntentException e) {
System.out.println("Remotein");
e.printStackTrace();
}
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == 1001) {
String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
if (resultCode == RESULT_OK) {
System.out.println("re ssuut ood");
try{
JSONObject jo = new JSONObject(purchaseData);
String sku = jo.getString(inappid);
Toast.makeText(MainActivity.this,
"u have bo ught"+ sku,
Toast.LENGTH_LONG).show();
}
catch(JSONException e){
System.out.println("didnt work");
e.printStackTrace();
}
}
}
}
//end
public void onDestroy(){
super.onDestroy();
if(connection != null){
unbindService(connection);
}
}
}enter image description here
https://stackoverflow.com/questions/26319316
复制相似问题