【问题标题】:why my code work on debug mode but don't work in real time为什么我的代码在调试模式下工作但不能实时工作
【发布时间】:2014-05-20 07:06:58
【问题描述】:

我拥有的是一个与 Web 服务连接的 java 代码,我正在尝试向页面添加一些数据.. 当我进行调试时,代码工作得很好,但是当我尝试在实时我面临一些问题..这是我的java代码:

public class PersonalInfo extends Activity{
    private GoogleMap googleMap;
    LocationManager mLocationManager;
    Geocoder geocoder;
    EditText addressnew;
     GPSTracker gps;
     boolean loginStatus;
     String Item_Name;
     String Item_Price;
     String Item_Quantity;
     String Total_Price;
     String Customer_Name;
     String Customer_Number;
     String Customer_Address;
        ArrayList<String> mixlist=new ArrayList<String>();


     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.personal_info);
           final EditText name=(EditText)findViewById(R.id.edittext1);
           final EditText mobile=(EditText)findViewById(R.id.edittext2);
           addressnew=(EditText)findViewById(R.id.editText3);
           final Button locationnew=(Button)findViewById(R.id.button1);
           Button send=(Button)findViewById(R.id.button2);

      send.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            try{
                Intent newintent = getIntent();
                mixlist=newintent.getStringArrayListExtra("listmix");
                Log.e("listmix",mixlist+"");
                for(int i=0;i<=mixlist.size();i++){
                    if(i==mixlist.size()){
                         Item_Name="0";

                            Item_Price="0";

                            Item_Quantity="0";

                            Total_Price="0";

                            Customer_Name=name.getText().toString();
                            Log.e("customer_name",Customer_Name);
                            Customer_Number=mobile.getText().toString();
                            Customer_Address=addressnew.getText().toString();

                            //Call execute 
                            AsyncCallWS task = new AsyncCallWS();
                            task.execute(); 
                    }
                    else{
                Item_Name=mixlist.get(i);
                i++;
                Item_Price=mixlist.get(i);
                i++;
                Item_Quantity=mixlist.get(i);
                i++;
                Total_Price=mixlist.get(i);

                Customer_Name="0";
                Customer_Number="0";
                Customer_Address="0";

//              AsyncCallWSnew tasknew = new AsyncCallWSnew();
                //Call execute 
                AsyncCallWS task = new AsyncCallWS();
                task.execute();
                    }
                }
            }
            catch(Exception e){
                e.printStackTrace();
            }

        }
    });


//         mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);



//       locationnew.setOnClickListener(new OnClickListener() {
//          
//          @Override
//          public void onClick(View v) {

//          } 
//           });
//      
//          

     } 
     @Override
     public void onBackPressed() {
        Log.d("CDA", "onBackPressed Called");
        Intent setIntent = new Intent(PersonalInfo.this,MainActivity.class);

        setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(setIntent);
     }  
     private class AsyncCallWS extends AsyncTask<Void, Void, Void> {



            protected void onPostExecute(Void result) {
                //Make Progress Bar invisible

                Toast.makeText(getApplicationContext(), "order has been sent", Toast.LENGTH_LONG).show();
                Intent intObj = new Intent(PersonalInfo.this,MainActivity.class);
                startActivity(intObj);
                //Error status is false
            }


            //Make Progress Bar visible
            protected void onPreExecute() {

            }


            @Override
            protected Void doInBackground(Void... params) {
                // TODO Auto-generated method stub
                 loginStatus = WebService.invokeLoginWS(Item_Name,Item_Price,Item_Quantity, Total_Price, Customer_Name,  

               Customer_Number, Customer_Address,"InsertData");
                return null;
            }          
        }         
}

现在例如混合列表是:

[orange, 2, 3, 6.0, Tomato, 0.3, 6, 1.7999999999999998]

它将为商品名称、商品价格、商品数量、总价格添加第一个索引,然后添加其他值(customer-name=0、customer_number=0、customer_address=0),并在数组完成后执行异步任务它会做相反的事情,我将填写 (item name, item price, item_quantity, total_price) = 0 并且其他值将从编辑文本中获取.. 并执行 asynctask .. 在调试模式下它工作得很好..实时反转这个数组中的值,它只会添加橙色、2、3、6.0,其他三个值将重复两次!

请问为什么会这样??

【问题讨论】:

  • 提示:即使主线程在调试器中挂起,用于异步任务的后台线程也会运行。
  • 我没有精力理解您的问题或代码,但仍然给出了一个提示,可以解释为什么涉及异步任务的代码在调试和正常运行时表现不同。比赛条件。
  • 那么这个问题的解决方法是什么?

标签: android web-services android-asynctask


【解决方案1】:

您在 for 循环中创建多个异步任务对象,其中每个对象在单独的线程上运行 它并行运行,并且您的第二个线程可能首先完成,第一个线程最后完成或以任何顺序完成,这就是为什么他们没有以正确的顺序插入数据,而是以随机顺序。

【讨论】:

  • 你有什么控制它的建议吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多