Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在android中,如何将用户定义的数组参数传递给asynctask_Java_Android_Android Asynctask - Fatal编程技术网

Java 在android中,如何将用户定义的数组参数传递给asynctask

Java 在android中,如何将用户定义的数组参数传递给asynctask,java,android,android-asynctask,Java,Android,Android Asynctask,但是,当我只检查用户定义的数组参数时,customerList在外部类中有值,但是uploadAsyncTask innerclass在模拟器上不断失败,不幸的是,应用程序失败了 private ArrayList<CustomerData> customerList = new ArrayList<CustomerData>(); @Override public void onCreate(Bundle savedInstanceState) {

但是,当我只检查用户定义的数组参数时,customerList在外部类中有值,但是uploadAsyncTask innerclass在模拟器上不断失败,不幸的是,应用程序失败了

private  ArrayList<CustomerData> customerList = new ArrayList<CustomerData>();

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        db = new DBAdapter(this);

        presidents = getResources().getStringArray(R.array.presidents_array);

        client = new AsyncHttpClient();

        Button btn_Save, btn_Backup;

        btn_Save = (Button) findViewById(R.id.btnSave);
        btn_Backup = (Button) findViewById(R.id.btnBackup);

        txt_AcctNum = (TextView) findViewById(R.id.txtAcctNum);
        txt_AcctName = (TextView) findViewById(R.id.txtAcctName);
        s1 = (Spinner) findViewById(R.id.spinner);
        txt_Amt = (TextView) findViewById(R.id.txtAmt);
                /*
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, presidents);
        */
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_single_choice, presidents);

        s1.setAdapter(adapter);
        s1.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0,
                                       View arg1, int arg2, long arg3) {
                int index = arg0.getSelectedItemPosition();
                //Toast.makeText(getBaseContext(),
                // "You have selected item : " + presidents[index],
                // Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });
         -------other codes here----

        btn_Backup.setOnClickListener(new Button.OnClickListener() {
            @SuppressWarnings(value = "unchecked")
            public void onClick(View view) {

                ArrayList<CustomerData> custList = new ArrayList<CustomerData>();
                // db = new DBAdapter(context);
                db.open();
                //db.backupToSD();
                //---get all contacts---
                Cursor c = db.getAllCustomers();
                if (c.moveToFirst()) {
                    do {
                        //DisplayContact(c);
                        //sendData();
                        custList = addAndDisplayCustomer(c);
                    } while (c.moveToNext());
                }
               /* for (int i = 0; i < custList.size(); i++) {
                    Toast.makeText(getApplicationContext(), "---Customer Data--- " + "id: " +
                            custList.get(i).getId() + " Acct Name: " +
                            custList.get(i).getAcctName() + " Acct Num: " +
                            custList.get(i).getAcctNum() + " Tnx type: " +
                            custList.get(i).getTxnType() + "  Amt: " +
                            custList.get(i).getAmt(), Toast.LENGTH_LONG).show();  }*/


                Toast.makeText(getApplicationContext(), " Uploading data ... " + custList.get(0).getId(), Toast.LENGTH_LONG).show();
                UploadASyncTask upload = new UploadASyncTask();
                upload.execute(custList);

            }
            public ArrayList<CustomerData> addAndDisplayCustomer(Cursor c)
            {
                CustomerData customer = new CustomerData(c.getString(0), c.getString(1),
                        c.getString(2), c.getString(3), c.getString(4));
                customerList.add(customer);

                return customerList;
            }
            //int delRows = db.deleteAll();
            //db.backupToSD();
            //db.dropTable();
            //Toast.makeText(getApplicationContext(), " Table successfully dropped ! ", Toast.LENGTH_LONG).show();
            //db.close();
        });
    }
    private class UploadASyncTask extends AsyncTask<ArrayList<CustomerData>,  Void, Void> {
        private Cursor c;
        private String id;
        private String acct_Name ;
        private String acct_Num;
        private String txnType;
        private String amt;
        //private ArrayList<CustomerData> custList;
        private Context mContext1;
        private ProgressDialog dialog = null;
        private Context mContext = null;

        @Override
        protected void onPreExecute() {
            dialog = new ProgressDialog(MainActivity.this);
            dialog.setTitle(" Sending to the server... ");
            dialog.setMessage("Please wait...");
            dialog.setProgressDrawable(mContext.getWallpaper());
            dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            dialog.setCancelable(false);
            dialog.show();
        }
        @Override
        @SafeVarargs
         final protected Void doInBackground(ArrayList<CustomerData>... custList) {

            try {
                  ArrayList<CustomerData>  custom = custList[0];
                  for (int i = 0; i<custom.size(); i++) {
                    String id = custom.get(i).getId();
                    String acct_Name = custom.get(i).getAcctName();
                    String acct_Num = custom.get(i).getAcctNum();
                    String txnType = custom.get(i).getTxnType();
                    String amt = custom.get(i).getAmt();
                   /*runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "Welcome guy: " + id,
                                    Toast.LENGTH_LONG).show();
                        }
                    });*/
                    HttpParams params = new BasicHttpParams();
                    HttpClient httpclient = new DefaultHttpClient(params);
                    HttpPost httpPost = new HttpPost
                            ("http://10.0.2.2:8080/RestWebService/rest/customer");

                    List<NameValuePair> postParams = new ArrayList<NameValuePair>();

                    postParams.add(new BasicNameValuePair("id", id));
                    postParams.add(new BasicNameValuePair("acct_name", acct_Name));
                    postParams.add(new BasicNameValuePair("acct_num", acct_Num));
                    postParams.add(new BasicNameValuePair("txn_type", txnType));
                    postParams.add(new BasicNameValuePair("amt", amt));

                    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams);
                    entity.setContentEncoding(HTTP.UTF_8);
                    httpPost.setEntity(entity);

                    HttpResponse httpResponse = httpclient.execute(httpPost);

                    InputStream inputStream = httpResponse.getEntity().getContent();
                    String result = "";
                    id = "";
                    acct_Name = "";
                    acct_Num = "";
                    txnType = "";
                    amt = "";
                    }
            }
             catch (Exception e)
              {
                Log.e("Server Error: ",e.getMessage());
              }
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            //custList.clear();
            dialog.dismiss();
        }
    }

 }

当我看到你的代码时:有一个小问题。我不知道这是个好答案,但是:

private Context mContext = null;

这行中有一个nullPointerException

对我来说,您可以删除AsyncTask上的上下文和光标。在AsyncTask上创建一个构造函数,并将上下文放入参数中,然后将其用于progressDialog


希望能有所帮助

请不要在没有从logcat发布堆栈跟踪的情况下发布崩溃错误。您的错误具体在哪里?我没有看到传递的ArrayList有错误。
dialog.setProgressDrawable(mContext.getWallpaper());