为什么我在android studio中的代码中出错?

时间:2016-01-27 19:28:53

标签: java android button package void

我正在做一个用户购买个性化产品的自定义应用。我正在尝试做篮子页面,但它只是不起作用。我在Bundle BuyIntent = mservice.getBuyIntent(3, getPackageName(), sku,"Purchase text item","bGoa+V7g/yqD");skuDetails= mservice.getSkuDetails(3, getPackageName(),"Purchase text item", querySku);中都有错误。这是说无法解决方法。请有人帮忙,谢谢。

import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Button;
import android.content.ServiceConnection;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.ResponseCache;
import java.util.ArrayList;
import android.support.v4.os.IResultReceiver.Stub;
import android.support.v7.app.ActionBarActivity;


public class Basket extends AppCompatActivity {
    Basket mservice;
    ServiceConnection connection;
    String purchase = "android.test.purchased";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basket);

        connection = new ServiceConnection() {

            @Override
            public void onServiceDisconnected(ComponentName name) {

                mservice = null;
            }
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
               Stub.asInterface(service);
            }};

            bindService(new Intent(
                    "com.example.emily.activity.414timber"),
                    connection, Context.BIND_AUTO_CREATE);
        Button purchaseBtn = (Button) findViewById(R.id.purchase);
        purchaseBtn.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                ArrayList skuList = new ArrayList();
                skuList.add(purchase);
                Bundle querySku = new Bundle();
                querySku.putStringArrayList("ITEM_ID_LIST", skuList);
                Bundle skuDetails;

                try {
                    skuDetails= mservice.getSkuDetails(3, getPackageName(),
                            "Purchase text item", querySku);

                    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(purchase)){
                                System.out.println("price " + price);
                                Bundle BuyIntent = mservice.getBuyIntent(3, getPackageName(), sku,
                                        "Purchase text item",
                                        "bGoa+V7g/yqD");
                                PendingIntent pendingIntent= BuyIntent
                                        .getParcelable("BUY_INTENT");
                                startIntentSenderForResult(
                                        pendingIntent.getIntentSender(),1001,
                                        new Intent(),Integer.valueOf(0),
                                        Integer.valueOf(0), Integer.valueOf(0));
                            }

                        }
                    }
                } catch (RemoteException e){
                    e.printStackTrace();
                }catch (JSONException e){
                    e.printStackTrace();
                }catch (IntentSender.SendIntentException e){
                    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(purchase);
                    Toast.makeText(
                            Basket.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);
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

你能为getSkuDetails()添加声明吗?从您的错误消息,听起来您可能期望不同数量的参数或不同的数据类型。