在android中没有调用onPostExecute

时间:2012-12-26 08:18:09

标签: android android-asynctask

在我的onPostExecute()应用程序中从Web服务获取值时,我无法调用Android。我在下面添加了代码。返回ArrayList后线程退出。你能检查我的代码中是否有任何问题吗?

public class searchuser extends AsyncTask<Void, Void,ArrayList<DeleteuserDetails>>{

        protected ArrayList<DeleteuserDetails> doInBackground(Void... arg0) {
            // TODO Auto-generated method stub

            path.setNamespace();
            path.setMethod_name("searchuser");
            path.setSoap_action();
            path.setUrl();
            selecteduserType=String.valueOf(utype.getSelectedItem());
            userid=edtTxtUserId.getText().toString();
            if(!String.valueOf(utype.getSelectedItem()).equals("select")){

            Webservice service=new Webservice(path.getNamespace(), path.getMethod_name(), path.getUrl(), path.getSoap_action());
            try{
            SoapObject searchUser=service.getRequest();
             PropertyInfo search_userid=new PropertyInfo();
             search_userid.setName("userID");
             search_userid.setValue(userid);
             searchUser.addProperty(search_userid);
            /* PropertyInfo search_utype=new PropertyInfo(); 
             search_utype.setName("utype");
             search_utype.setValue(selecteduserType);
             searchUser.addProperty(search_utype);*/
             SoapSerializationEnvelope envelope = service.getEnvelope(searchUser);
                SoapObject response = service.getResponse(envelope);
                int count = response.getPropertyCount();
                for (int i = 0; i < count; i++) {

                    SoapObject result = (SoapObject) response.getProperty(i);
                    DeleteuserDetails deleteuserDetails=new DeleteuserDetails();
                    if(result.getPropertyAsString(4)!=null){
                    deleteuserDetails.setUserId(result.getPropertyAsString(4));
                    }
                    else{

                    }
                    if(result.getPropertyAsString(2)!=null){
                    deleteuserDetails.setUserName(result.getPropertyAsString(2));
                    }
                    else{

                    }
                    if(result.getPropertyAsString(3)!=null){
                        deleteuserDetails.setUserRole(result.getPropertyAsString(3));   
                    }
                    if(result.getPropertyAsString(1)!=null){
                        deleteuserDetails.setCreatedDate(result.getPropertyAsString(1));    
                    }
                    else{

                    }
                    try {
                        deleteuserDetails.setCreatedBy(result.getPropertyAsString(0).toString());
                    }
                    catch(NullPointerException npe) {
                        deleteuserDetails.setCreatedBy("null");
                    }
                    userdetail.add(deleteuserDetails);
                }

        }
            catch(Exception e){
                e.printStackTrace();
            }
            }
            else{
                alertbox.generateAlert(Deleteuser.this, "Please select usertype", "Default value Selected", R.drawable.noicon);

            }
            return userdetail;
        }
        protected void onPostExecute( ArrayList<DeleteuserDetails>deleteuserDetails) {
            deleteuserList = new TextView[deleteuserDetails.size()];

            for(int i=0;i<deleteuserDetails.size();i++){

                    TableRow deleteUserRow=new TableRow(Deleteuser.this);
                    deleteUserRow.setId(i);
                    deleteUserRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
                    selectcheck = new CheckBox(Deleteuser.this);
                    selectcheck.setId(i);
                    selectcheck.setTag(i);

                    deleteUserRow.addView(selectcheck);

                    deleteuserList[i]=new TextView(Deleteuser.this);
                //  deleteuserList[i].setId(i);
                    deleteuserList[i].setTag(i);
                    deleteuserList[i].setText(deleteuserDetails.get(i).getUserId());
                    deleteUserRow.addView(deleteuserList[i]);

                    uName=new TextView(Deleteuser.this);
                    uName.setId(i);
                    uName.setText(deleteuserDetails.get(i).getUserName());
                    deleteUserRow.addView(uName);

                    Userrole=new TextView(Deleteuser.this);
                    Userrole.setId(i);
                    Userrole.setPadding(20, 0, 0, 0);
                    Userrole.setText(deleteuserDetails.get(i).getUserRole());
                    deleteUserRow.addView(Userrole);

                    CreatedDate=new TextView(Deleteuser.this);
                    CreatedDate.setId(i);
                    CreatedDate.setPadding(20, 0, 0, 0);
                    CreatedDate.setText(deleteuserDetails.get(i).getCreatedDate());
                    deleteUserRow.addView(CreatedDate);

                    CreatedBy=new TextView(Deleteuser.this);
                    CreatedBy.setId(i);
                    CreatedBy.setPadding(20, 0, 0, 0);
                    CreatedBy.setText(deleteuserDetails.get(i).getCreatedBy());
                    deleteUserRow.addView(CreatedBy);
                    userdetailTable.addView(deleteUserRow,
                            new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
                                    LayoutParams.WRAP_CONTENT));



                    selectcheck.setOnClickListener(listener);
                    Log.i("Exception",""+i);
                }
            TableRow addcomponentrow=new TableRow(Deleteuser.this);
            addcomponentrow.setId(200);
            addcomponentrow.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

            Button Deletecomponentbtn=new Button(Deleteuser.this);
            Deletecomponentbtn.setText("Delete");
            Deletecomponentbtn.setId(200);
            Deletecomponentbtn.setPadding(10, 0, 20, 2);
            Deletecomponentbtn.setBackgroundColor(color.red);
            addcomponentrow.addView(Deletecomponentbtn);

            userdetailTable.addView(addcomponentrow,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            Deletecomponentbtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    // TODO Auto-generated method stub
                    new DeleteUserid().execute(checkedValue);
                }
            });


        }
        final OnClickListener listener=new OnClickListener() {

            @Override
            public void onClick(View v) {

                // TODO Auto-generated method stub
                int index = (Integer) v.getTag();
                checkedValue=(String) deleteuserList[index].getText();

            }
        };

    }

2 个答案:

答案 0 :(得分:2)

更改

protected void onPostExecute( ArrayList<DeleteuserDetails>deleteuserDetails) {
                                                     // ^^^

@Override
protected void onPostExecute( ArrayList<DeleteuserDetails> deleteuserDetails) {
 // your code here...

答案 1 :(得分:0)

按如下方式初始化此DeleteuserDetails

  DeleteuserDetails deleteuserDetails = new DeleteuserDetails();

声明String变量并赋值

  String userId = "";
  String userName = "";
  String userRole = "";
  String createdDate = "";
  String createdBy = "";

在Model中,生成构造函数如下:

  DeleteuserDetails(String userId, String userName, String userRole, String createdDate, String createdBy) {
   this.userId = userId;
   this.userName = userName;
   this.userRole = userRole;
   this.createdDate = createdDate;
   this.createdBy = createdBy;
 }

现在,您在doInBackground(..)

中执行以下操作
   if (result.getPropertyAsString(4) != null) 
             userId = result.getPropertyAsString(4);
   if (result.getPropertyAsString(2) != null) 
             userName = result.getPropertyAsString(2);
   if (result.getPropertyAsString(3) != null) 
             userRole = result.getPropertyAsString(3);
   if (result.getPropertyAsString(1) != null) 
             createdDate = result.getPropertyAsString(1);
    if (result.getPropertyAsString(0) != null) 
        createdBy = result.getPropertyAsString(0);

   deleteuserDetails = new  DeleteuserDetails(userId, userName, userRole, createdDate,  createdBy);

     userdetail.add(deleteuserDetails);

现在在onPostExecute()方法中添加runOnUiThread()方法,然后按照以下步骤操作:

        runOnUiThread(new Runnable() {

            @Override
            public void run() {


            deleteuserList = new TextView[deleteuserDetails.size()];

            for(int i=0;i<deleteuserDetails.size();i++){

                    TableRow deleteUserRow=new TableRow(Deleteuser.this);
                    deleteUserRow.setId(i);
                    deleteUserRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
                    selectcheck = new CheckBox(Deleteuser.this);
                    selectcheck.setId(i);
                    selectcheck.setTag(i);

                    deleteUserRow.addView(selectcheck);

                    deleteuserList[i]=new TextView(Deleteuser.this);
                //  deleteuserList[i].setId(i);
                    deleteuserList[i].setTag(i);
                    deleteuserList[i].setText(deleteuserDetails.get(i).getUserId());
                    deleteUserRow.addView(deleteuserList[i]);

                    uName=new TextView(Deleteuser.this);
                    uName.setId(i);
                    uName.setText(deleteuserDetails.get(i).getUserName());
                    deleteUserRow.addView(uName);

                    Userrole=new TextView(Deleteuser.this);
                    Userrole.setId(i);
                    Userrole.setPadding(20, 0, 0, 0);
                    Userrole.setText(deleteuserDetails.get(i).getUserRole());
                    deleteUserRow.addView(Userrole);

                    CreatedDate=new TextView(Deleteuser.this);
                    CreatedDate.setId(i);
                    CreatedDate.setPadding(20, 0, 0, 0);
                    CreatedDate.setText(deleteuserDetails.get(i).getCreatedDate());
                    deleteUserRow.addView(CreatedDate);

                    CreatedBy=new TextView(Deleteuser.this);
                    CreatedBy.setId(i);
                    CreatedBy.setPadding(20, 0, 0, 0);
                    CreatedBy.setText(deleteuserDetails.get(i).getCreatedBy());
                    deleteUserRow.addView(CreatedBy);
                    userdetailTable.addView(deleteUserRow,
                            new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
                                    LayoutParams.WRAP_CONTENT));



                    selectcheck.setOnClickListener(listener);
                    Log.i("Exception",""+i);
                }
            TableRow addcomponentrow=new TableRow(Deleteuser.this);
            addcomponentrow.setId(200);
            addcomponentrow.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

            Button Deletecomponentbtn=new Button(Deleteuser.this);
            Deletecomponentbtn.setText("Delete");
            Deletecomponentbtn.setId(200);
            Deletecomponentbtn.setPadding(10, 0, 20, 2);
            Deletecomponentbtn.setBackgroundColor(color.red);
            addcomponentrow.addView(Deletecomponentbtn);

            userdetailTable.addView(addcomponentrow,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            Deletecomponentbtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    // TODO Auto-generated method stub
                    new DeleteUserid().execute(checkedValue);
                }
            });


        }
        final OnClickListener listener=new OnClickListener() {

            @Override
            public void onClick(View v) {

                // TODO Auto-generated method stub
                int index = (Integer) v.getTag();
                checkedValue=(String) deleteuserList[index].getText();

            }
        };

            }
        });
    }