【问题标题】:None of the methods of AsyncTask called没有调用 AsyncTask 的方法
【发布时间】:2017-04-08 15:06:50
【问题描述】:

我的应用程序中有一个按钮,单击该按钮将打开 PDF(将显示“打开方式”提示)。 PDF 文件位于应用程序的我的资产文件夹中。我已经编写了代码,以便 AsyncTask 执行从资产文件夹复制到外部存储然后打开它的操作。然而,在运行时,我从 Logcat 中可以看到,AsyncTask 类的所有方法都没有执行。我看过其他类似的问题并尝试了他们的不同答案:

  1. .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR) 而不是 .execute()
  2. .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null);
  3. 添加 onPreExecute()

我得到的错误是 onPostExecute(File file) 方法中的 NullPointerException,即找不到文件。三种方法中记录的语句都没有在logcat中。

我在下面调用这个执行方法:

公共无效执行(){

    Context context = contextWeakReference.get();
    if (context != null) {
        new CopyFileAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null);
    }

}


private class CopyFileAsyncTask extends AsyncTask<Void, Void, File> {


    final String appDirectoryName = BuildConfig.APPLICATION_ID;
    final File fileRoot = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DOCUMENTS), appDirectoryName);

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Log.e("AsyncTask", "onPreExecute");
    }

    @Override
    protected File doInBackground(Void... params) {

        Context context = contextWeakReference.get();

        AssetManager assetManager = context.getAssets();

        File file = new File(fileRoot, fileName);

        InputStream in = null;
        OutputStream out = null;
        try {

            file.mkdirs();

            if (file.exists()) {
                file.delete();
            }

            file.createNewFile();


            in = assetManager.open(fileName);
            Log.d(TAG, "In");

            out = new FileOutputStream(file);
            Log.d(TAG, "Out");

            Log.d(TAG, "Copy file");
            copyFile(in, out);

            Log.d(TAG, "Close");
            in.close();

            out.flush();
            out.close();

            return file;
        } catch (Exception e)
        {
            Log.e(TAG, e.getMessage());
        }

        return null;
    }

    private void copyFile(InputStream in, OutputStream out) throws IOException
    {
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1)
        {
            out.write(buffer, 0, read);
        }
    }

    @Override
    protected void onPostExecute(File file) {
        super.onPostExecute(file);

        Context context = contextWeakReference.get();


        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(
                Uri.fromFile(file),
                "application/pdf");

        context.startActivity(intent);

    }
}

【问题讨论】:

    标签: java android android-asynctask


    【解决方案1】:

    你总是在 doInBackground

    中返回 null

    所以

    protected void onPostExecute(File file) {
        super.onPostExecute(file);
    
        Context context = contextWeakReference.get();
    
    
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(
                Uri.fromFile(file),
                "application/pdf");
    
        context.startActivity(intent);
    
    }
    

    文件总是Null

    尝试编辑这个

      @Override
        protected File doInBackground(Void... params) {
    
            Context context = contextWeakReference.get();
    
            AssetManager assetManager = context.getAssets();
    
            File file = new File(fileRoot, fileName);
    
            InputStream in = null;
            OutputStream out = null;
            try {
    
                file.mkdirs();
    
                if (file.exists()) {
                    file.delete();
                }
    
                file.createNewFile();
    
    
                in = assetManager.open(fileName);
                Log.d(TAG, "In");
    
                out = new FileOutputStream(file);
                Log.d(TAG, "Out");
    
                Log.d(TAG, "Copy file");
                copyFile(in, out);
    
                Log.d(TAG, "Close");
                in.close();
    
                out.flush();
                out.close();
    
            } catch (Exception e)
            {
                Log.e(TAG, e.getMessage());
            }
                return file;
    
         }
    

    【讨论】:

    • 非常感谢!!应用程序运行并且“打开方式”对话框也会出现。 adobe reader flash 屏幕出现,但随后我收到一条 toast 消息:“无法访问此文件。请检查位置或网络,然后重试”。你能帮我解决这个问题吗?
    • 还有一点:我用了其他应用,他们也打不开。似乎该文件不在该目录中。
    • 还有!我再次查看日志:遇到了onPreExecute,但没有遇到其他两种方法!也许这就是为什么文件在它应该存在的地方不存在的原因
    • 我的答案是否让你不满意
    • 我的问题是为什么 onPreExecute 方法正在执行,而其他两个没有执行
    【解决方案2】:

    问题解决了!!显然 Marshmallow 在幕后做了一些秘密工作,比如不请求权限(使用 android studio 安装时)。我发现这里的答案android mkdirs not working 对我有用。您必须转到设置 > 应用程序 > 您的应用程序 > 打开存储权限。我花了三天时间才解决这个问题!

    【讨论】:

      猜你喜欢
      • 2011-09-13
      • 1970-01-01
      • 2016-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-31
      • 2012-06-18
      • 1970-01-01
      相关资源
      最近更新 更多