【问题标题】:Can you show me why these methods are out of scope?你能告诉我为什么这些方法超出了范围吗?
【发布时间】:2014-11-21 00:26:46
【问题描述】:

我正在使用教程来创建下载类,它使用进度对话框。 show 和 dissmiss 方法位于 asynchTask 类内的受保护类中。 IDE 告诉我它无法解决它们

public class DownloadHandler {

    private Context mContext;
    public String filename;
    private String remotePath;

    public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
    private ProgressDialog mProgressDialog;

    public DownloadHandler(String rp, String f, Context c) throws Exception {
    mContext = c;
    remotePath = rp;
    filename = f;
}

private void startDownload() {
    String url = "http://example.com/"+remotePath+"/"+filename+".pdf";
    new DownloadFileAsync().execute(url);
}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DIALOG_DOWNLOAD_PROGRESS:
            mProgressDialog = new ProgressDialog(mContext);
            mProgressDialog.setMessage("Downloading file..");
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setCancelable(false);
            mProgressDialog.show();
            return mProgressDialog;
        default:
            return null;
    }
}

class DownloadFileAsync extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

//===================showDialog can not be resolved============================

        showDialog(DIALOG_DOWNLOAD_PROGRESS);
//========================================================================

    }

    @Override
    protected String doInBackground(String... aurl) {
        int count;

        try {

            URL url = new URL(aurl[0]);
            URLConnection conexion = url.openConnection();
            conexion.connect();

            int lengthOfFile = conexion.getContentLength();

            InputStream input = new BufferedInputStream(url.openStream());
            //write it to the internal storage

            OutputStream output = new FileOutputStream(filename+".pdf");


            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;
                publishProgress(""+(int)((total*100)/lengthOfFile));

                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();
        } catch (Exception e) {}
        return null;

    }
    protected void onProgressUpdate(String... progress) {
        Log.d("ANDRO_ASYNC",progress[0]);
        mProgressDialog.setProgress(Integer.parseInt(progress[0]));
    }

    @Override
    protected void onPostExecute(String unused) {
//=========dismissDialog can not be resolved ==================

        dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
//=============================================================
    }
}

}

和受保护的类有关系吗?

【问题讨论】:

  • 我在API documentation中没有看到这些方法
  • 嗯,我不知道。教程中就是这样写的。现在我做了一个没有任何修改的直接复制和粘贴,我的 IDE 说这些方法已被弃用。
  • 链接我的教程
  • 如果我扩展 Activity 它已解决但显示已弃用。稍等,我会得到链接。

标签: java android class scope


【解决方案1】:

showDialogdismissDialogActivity 类中。在教程中,它显示了 Download 类扩展了活动,然后内部类可以使用它。

Download 扩展Activity。至于不推荐使用的方法,这是因为您应该使用DialogFragment。显然,您所遵循的教程已过时,您应该查看how to use a DialogFragment

【讨论】:

    猜你喜欢
    • 2020-04-10
    • 1970-01-01
    • 2015-09-12
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多