【问题标题】:Android file upload progress dialog errorAndroid文件上传进度对话框错误
【发布时间】:2012-09-26 20:00:03
【问题描述】:

我收到了这个 AsyncTask 文件上传:

// ASync Task Begin to perform Billing information
class performBackgroundTask extends AsyncTask<Void, Void, Void> {
    Context context;
    private ProgressDialog Dialog;

    protected void onPreExecute() {
        // here you have place code which you want to show in UI thread like
        // progressbar or dialog or any layout . here i am displaying a
        // progressDialog with test please wait while loading......

        Dialog.setMessage(" please wait while loading............");
        Dialog.show();

    }



    private Context getApplicationContext() {
        // TODO Auto-generated method stub
        return null;
    }

    protected Void doInBackground(Void... params) {
        // write here the code to download or any background task.
        goforIt(); // example call method which will download vedio .
        return null;
    }

    protected void onPostExecute(Void unused) {
        Dialog.dismiss();
        // the background task is completed .You can do the code here for next
        // things

    }

    public void goforIt() {

        FTPClient con = null;

        try {
            con = new FTPClient();
            con.connect(globalconstant.host);

            if (con.login(globalconstant.nev, globalconstant.jelszo)) {

                con.enterLocalPassiveMode(); // important!
                con.setFileType(FTP.BINARY_FILE_TYPE);
                String substr = globalconstant.path.substring(4,
                        globalconstant.path.length());
                String filename = substr + "/Festivale.db";
                Log.e("TravellerLog :: ", substr + "/Festivale.db");
                String data = filename;

                FileInputStream in = new FileInputStream(new File(data));
                boolean result = con.storeFile("/Festivale.db", in);
                in.close();
                if (result)
//                  Toast.makeText(getApplicationContext(),
//                          "A fájl feltöltve!", Toast.LENGTH_SHORT).show();

                Log.v("upload result", "succeeded");
                con.logout();
                con.disconnect();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

但总是得到错误信息,这个:

09-26 11:30:30.538: E/AndroidRuntime(456): java.lang.NullPointerException
09-26 11:30:30.538: E/AndroidRuntime(456):  at com.eyecom.festivale.performBackgroundTask.onPreExecute(performBackgroundTask.java:25)
...

第 25 行:Dialog.setMessage(" please wait while loading............");

请帮助我如何制作一个适用于此的进度对话框。 原文是这样的:

private ProgressDialog Dialog = new ProgressDialog(this);

但也有错误,

构造函数 ProgressDialog(performBackgroundTask) 未定义

【问题讨论】:

    标签: android ftp


    【解决方案1】:

    您在做错事,因为 Dialog 从未启动它只是声明未定义。 另一件事 Dialog 关键字已经是一个类名,只要您定义任何对象/变量名称开始字符,如果字母必须是小写。开始字符的所有大写字母都用于类名。

    已编辑

    private ProgressDialog dialog;
    
    protected void onPreExecute() {
        dialog = ProgressDialog.show(YourActivity.this, "Processing", "please wait while loading............");
    }
    

    onPostExecution(Void unused)

    onPostExecution(Void unused){
         if(dialog!=null)
             dialog.dismiss();
    }
    

    【讨论】:

    • 09-26 11:50:46.389: E/AndroidRuntime(528): at com.eyecom.festivale.performBackgroundTask.onPreExecute(performBackgroundTask.java:25)
    • 我认为 getApplicationContext 有问题
    • dialog = ProgressDialog.show(performBackgroundTask.this, "Processing", "加载中请稍候............");错误:ProgressDialog 类型中的方法 show(Context, CharSequence, CharSequence) 不适用于参数 (performBackgroundTask, String, String)
    【解决方案2】:
    public class async extends AsyncTask<void, void, void>{
    
      private Context context;
      ProgressDialog prog;
    
      public async(Context context) {
        this.context = context;
      }
    
    
      @Override
      protected void onPreExecute() {
        super.onPreExecute();
        prog=new ProgressDialog(context); 
        prog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        prog.setMax(100);
        prog.show();
      }
    
      // ...
    }
    

    然后实例化并运行这个AsyncTask:

    async mTask = new async(context);
    mTask.execute();
    

    【讨论】:

    • 构造函数 ProgressDialog(performBackgroundTask) 未定义
    • 我应该在参数中写什么?还是留下参数?
    • 保留它的参数。你应该使用 void。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    相关资源
    最近更新 更多