1

我只想问是否可以在我的 Asyntask 的 onPostExecute 中使用我的自定义吐司在 android 中。如果是,那怎么办?我试图把它放在 onPostExecute 上,但我得到了很多红线。这是我的自定义吐司代码:

Typeface tfR= Typeface.createFromAsset(getAssets(), "Gothic_Regular.TTF");
    LayoutInflater inflater = getLayoutInflater();
    View layouttoast = inflater.inflate(R.layout.toast_bg, (ViewGroup)findViewById(R.id.toastAttribute));
    TextView msg = ((TextView) layouttoast.findViewById(R.id.txt_toast));
    msg.setTypeface(tfR);
    msg.setText(toast_msg);
    msg.setTextSize(TypedValue.COMPLEX_UNIT_PX,16);
    Toast mytoast = new Toast(getBaseContext());
    mytoast.setView(layouttoast);
    mytoast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    mytoast.setDuration(Toast.LENGTH_SHORT);
    mytoast.show();

然后我想把它放在这里:

public class DoPost extends AsyncTask<String, Void, Boolean> 
{
    Exception exception = null;
    Context mContext = null;

        . . . . . 


public DoPost(Context context, String username, String password,
        String reportcode, String remarks, String date, String province,
        String infotype, String competitor, ArrayList<String> brands,
        ArrayList<String> segments) 
{
    mContext         = context;

           . . . .

    databaseHandler = new DatabaseHandler(context);
    if (databaseHandler != null)
    {
        databaseHandler.close();
        databaseHandler.createDB();
    }
}

protected void onPreExecute() 
{
    progressDialog = new ProgressDialog(mContext);
    progressDialog.setMessage("Uploading attachment details....");
    progressDialog.show();
    progressDialog.setCancelable(false);
}

@Override
protected Boolean doInBackground(String... arg0) 
{

    try {
            JSONObject jObject              = new JSONObject();

            Log.d("DoPost Constants.FILE_URI", Constants.FILE_URI.toString());
            Log.d("DoPost create SELECTEDFILE URI", SelectedFiles.listFileUri.toString());
            Log.d("DoPost create SELECTEDFILE FILENAME", SelectedFiles.listFileName.toString());

        //  String filename                 = "Chapt-19.pdf";
            String filename                 = "";
            try {

                JSONArray jArraySubrands            = new JSONArray();
                JSONArray jArrayConsumerSegments    = new JSONArray();
                JSONArray jArrayReportUpload    = new JSONArray();

                for (int i = 0; i < Constants.SHARED_PREFERENCES_SUBBRANDS.size(); i++) 
                {
                    JSONObject brand = new JSONObject();
                    brand.put("Id", _brands.get(i));
                    jArraySubrands.put(brand);
                }

                for (int j = 0; j < Constants.SHARED_PREFERENCES_SEGMENTS.size(); j++) 
                {
                    JSONObject consumerSegments = new JSONObject();
                    consumerSegments.put("Id", _segments.get(j));
                    jArrayConsumerSegments.put(consumerSegments);
                }

                for (int i = 0; i < Constants.ARRAYLIST_FILENAME.size(); i++) 
                {
                    JSONObject jObjectReportUpload  = new JSONObject();
                    filename = Constants.ARRAYLIST_FILENAME.get(i);
                    jObjectReportUpload.put("ReportUploadId", 0);
                    jObjectReportUpload.put("Filename", filename);
                    jObjectReportUpload.put("TempFilename", filename);
                    jObjectReportUpload.put("Description", "Image Testing");
                    jObjectReportUpload.put("ReportUploadTypeId", 1);
                    jObjectReportUpload.put("ReportId", 0);
                    jArrayReportUpload.put(jObjectReportUpload);

                    Log.d("filename: ", filename);

                }

                jObject.put("ReportId", 0);
                jObject.put("ReportCode", _code);
                jObject.put("Title", "Mobile Developer");
                jObject.put("Remarks", _remarks);
                jObject.put("DateObserved", _date);
                jObject.put("ProvinceId", _province);
                jObject.put("InformationTypeId", _infotype);
                jObject.put("ReportTypeId", 1);
                jObject.put("IsCompetitor", _competitor);
                jObject.put("SubBrands", jArraySubrands);
                jObject.put("ConsumerSegments", jArrayConsumerSegments);
                jObject.put("ReportUploads", jArrayReportUpload);

            } catch (Exception e) 
            {
                e.printStackTrace();
            }

            ResponseHandler<String> resonseHandler  = new BasicResponseHandler();
            HttpParams httpParameters               = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
            HttpConnectionParams.setSoTimeout(httpParameters, 15000);
            HttpPost httpPost                       = new HttpPost("http://phsjghhghghulchghg4.hgh.com:1214/api/reports");
            HttpClient httpclient                   = new DefaultHttpClient(httpParameters);
            httpPost.addHeader("Authorization","Basic "+ Base64.encodeToString((_username + ":" + _password).getBytes(),Base64.NO_WRAP));
            httpPost.setHeader("Content-Type", "application/json");
            httpPost.setHeader("Accept", "application/json");
            httpPost.setEntity(new ByteArrayEntity(jObject.toString().getBytes(HTTP.UTF_8)));

            String response = httpclient.execute(httpPost).toString();
            Log.i("response: ", response);
            Log.i("JSON", jObject.toString());

        } catch (ClientProtocolException e) 
            {
                e.printStackTrace();
                Log.e("Header","ClientProtocolException in callWebService(). "+ e.getMessage());
                error = String.valueOf(e);
                return false;
            } 
    catch (IOException e) 
        {
            e.printStackTrace();
            Log.e("Header","IOException in callWebService(). " + e.getMessage());
            error = String.valueOf(e);
            return false;
        }
    return true;
}

protected void onPostExecute(Boolean valid) 
{
    progressDialog.dismiss();

    Log.d("RESULT", String.valueOf(valid));

    if(valid){
        //Customzize toast here.
        new DoPost(mContext,_username, _password, _code, _remarks, _date, _province, _infotype,_competitor,_brands, _segments).execute();

    }else{
        //Customzize toast here.

    }
}
4

1 回答 1

0

我想我已经知道发生了什么事。你的范围是错误的。当前您的代码在 AsyncTask 中运行,而不是在 Activity 中运行!这就是为什么你不能使用 getAssets、getLayoutInflater、findViewById、getBaseContext。在你的 xxxActivity 中创建你的 AsyncTask。并使用你的 xxxActivity.this.findViewById 等等。

于 2013-08-12T03:19:10.560 回答