繁体   English   中英

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

[英]Why am i getting errors in my code in android studio?

我正在做一个定制应用程序,用户在其中购买个性化产品。 我正在尝试执行购物篮页面,但它无法正常工作。 我在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);
            }
        }

    }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM