0

我遇到了一个奇怪的问题。我以前从未见过。在代码中,执行了 asynctask,在 doInBackground 中我调用了一个函数,但函数没有执行。问题是什么 ?我没有收到任何错误消息,只是函数没有执行并item_details返回 null。

注意:在 asynctask 中,int xvalue 不为 null。

这是异步任务

public class GetItemDetailAS extends AsyncTask<String,String[],String[]>{
        String ParamID;
        private ProgressDialog dialog = new ProgressDialog(ItemActivity.this);

        public GetItemDetailAS(String ParamID){
            this.ParamID=ParamID;
        }

        @Override
         protected void onPreExecute() {
            dialog.setMessage("Loading...");
            dialog.show();
         }

        @Override
        protected String[] doInBackground(String... params) {
            int x = Integer.valueOf(ParamID);
            GetItemDetail(x);   

            return item_details;
        }

        protected void onPostExecute(String[] item_details){
             _title.setText(item_details[5]);
             _sellerName.setText(item_details[1]);
             _location.setText(item_details[6]);
             _price.setText(item_details[3]);
             _desc.setText(item_details[4]);

             if(item_details[7].toString().length()<20){
                    _picture.setImageResource(R.drawable.desire_z);
                }
                else{
                    byte[] decodedString = Base64.decode(item_details[7], Base64.DEFAULT);
                    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
                    _picture.setImageBitmap(decodedByte);
                }
             dialog.dismiss();
        }
    }

这是功能

private String[] GetItemDetail(int paramID){
        PropertyInfo id = new PropertyInfo();
        id.name= "itemid";
        id.setValue(paramID);
        id.type = PropertyInfo.INTEGER_CLASS;

        SoapObject request = new SoapObject("http://tempuri.org/", "GetItem");
        request.addProperty(id);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.bodyOut=request;
        envelope.dotNet = true;     
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE("http://service.melihmucuk.com/ShopArWS.asmx");
        androidHttpTransport.debug = true;

        try {
            androidHttpTransport.call("http://tempuri.org/GetItem", envelope);  
            SoapObject response = (SoapObject) envelope.getResponse();
            for(int i=0;i<response.getPropertyCount();i++){    
               Object property = response.getProperty(i);
               if(property instanceof SoapObject){
                   SoapObject item = (SoapObject) property;
                   String seller_id = String.valueOf(item.getProperty("seller_id").toString());
                   String seller_name = item.getProperty("seller_name").toString();
                   String item_id = String.valueOf(item.getProperty("item_id").toString());
                   String price = item.getProperty("price").toString() + " TL";
                   String desc = item.getProperty("desc").toString();
                   String title = item.getProperty("title").toString();
                   String location = item.getProperty("location").toString();
                   String picture = item.getProperty("picture").toString();
                        item_details[0] = seller_id;
                        item_details[1] = seller_name;
                        item_details[2] = item_id;
                        item_details[3] = price;
                        item_details[4] = desc;
                        item_details[5] = title;
                        item_details[6] = location;
                        item_details[7] = picture;
               }
            }
        }
             catch (Exception e) {          
                e.printStackTrace();
            }
        return item_details; 
    }

创建

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

        _title =(TextView) findViewById(R.id.titleTxt);
        _sellerName = (TextView) findViewById(R.id.sellerNameTxt);
        _location = (TextView) findViewById(R.id.LocationNameTxt);
        _price = (TextView) findViewById(R.id.PriceNameTxt);
        _desc = (TextView) findViewById(R.id.descNameText);
        _picture = (ImageView)findViewById(R.id.itemImage);
        button = (Button)findViewById(R.id.BuyButton);

        veriler = getIntent().getExtras();       
        item_id=veriler.getString("item_id");
        item_details = new String[8];

        new GetItemDetailAS(item_id).execute();

        bundle = new Bundle();

        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                final Dialog dialogConfirm = new Dialog(context);
                dialogConfirm.setContentView(R.layout.activity_cart);
                dialogConfirm.setTitle("Shopping Cart");
                dialogConfirm.setCancelable(false);

                dTitle = (TextView) dialogConfirm.findViewById(R.id.cartItemTitle);
                dPrice = (TextView) dialogConfirm.findViewById(R.id.cartItemPrice);
                dTitle.setText(item_details[5].toString()+":");
                dPrice.setText(item_details[3]);

                Button completeButton = (Button) dialogConfirm.findViewById(R.id.cartCompleteButton);
                Button cancelButton = (Button) dialogConfirm.findViewById(R.id.cartCompleteCancel);

                completeButton.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        // complete

                        dialogConfirm.dismiss();
                    }
                });

                cancelButton.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        // cancel
                        dialogConfirm.dismiss();
                    }
                });
                dialogConfirm.show();
            }
        });

    }
4

2 回答 2

0

是否item_details在某处初始化?也许你只是在尝试用数据填充它时得到一个 NPE,而你看不到e.printStackTrace();. 将其替换为真实的 Log 输出。也更换

GetItemDetail(x);   
return item_details;

return GetItemDetail(x);
于 2013-06-19T22:18:18.517 回答
0

首先认为我可以看到您的函数 private String[] GetItemDetail(int paramID) 返回值,但您没有将其分配给任何变量: GetItemDetail(x)

尝试:

@Override
         protected String[] doInBackground(String... params) {
             int x = Integer.valueOf(ParamID);
             return GetItemDetail(x);   
         }

代替

@Override
        protected String[] doInBackground(String... params) {
            int x = Integer.valueOf(ParamID);
            GetItemDetail(x);   
            return item_details;
        }

我没有看到你的整个班级,所以我看不到你是如何在 GetItemDetail 函数中引入 item_details 的?

于 2013-06-19T22:25:54.480 回答