java - 关于上下文 FLAG_ACTIVITY_NEW_TASK 的错误

标签 java android pdf

在我的应用程序中,我有一个可扩展 ListView ,我想在单击特定子项时打开从 Internet 下载的 PDF。当我点击它时,应用程序崩溃并且在 Android Studio 的 Android Monitor 上出现这个错误:

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

当我尝试 addflag() 或 setflag() 时,它告诉我一些关于静态上下文的信息。

ContextGetter 类:

public class ContextGetter extends Application {
    private static Context context;

    public void onCreate(){
        super.onCreate();
        context = getApplicationContext();
    }

    public static Context getAppContext() {
        return context;
    }
}

下载器类:

public class Downloader {
    public static void DownloadFile(String fileURL, File directory) {
        try {
            FileOutputStream f = new FileOutputStream(directory);
            URL u = new URL(fileURL);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            InputStream in = c.getInputStream();

            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = in.read(buffer)) > 0) {
                f.write(buffer, 0, len1);
            }
            f.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

OpenPDF(我的语言是 AbrirPDF)类:

public class AbrirPDF {
    public static void showPdf() {
        File file = new File(Environment.getExternalStorageDirectory()+File.separator+"lightflow/Read.pdf");
        PackageManager packageManager = ContextGetter.getAppContext().getPackageManager();
        Intent testIntent = new Intent(Intent.ACTION_VIEW);
        testIntent.setType("application/pdf");
        List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/pdf");
        ContextGetter.getAppContext().startActivity(intent);
    }
}

部分Activity Java代码:

private void registerClick() {
    expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {
            if ((groupPosition == 0) && (childPosition == 0)) {
                File file = new File(Environment.getExternalStorageDirectory()+File.separator+"IAVE", "Read.pdf");
                try {
                    file.createNewFile();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                Downloader.DownloadFile("MY_URL", file);


                AbrirPDF.showPdf();
            } else {

            }

            return false;
        }
    });
}

最佳答案

由于您正在使用应用程序上下文,因此您必须根据您的 Intent 设置此标志

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

如果您不想使用该标志并继续使用当前任务,您需要将一个 Activity 参数添加到您的 showPdf() 方法并使用它来改为开始下一个 Activity 。

public static void showPdf(Activity activity) {
    ...
    activity.startActivity(intent);
}

然后您可以使用以下方法从您的 onChildClick 处理程序中调用它:

AbrirPDF.showPdf(MyActivityClassName.this);

关于java - 关于上下文 FLAG_ACTIVITY_NEW_TASK 的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35090654/

相关文章:

java - 有没有办法将 OGNL 与 Struts2 UI 标签绑定(bind)

java - 使用java通过UDP发送/接收不同的对象

java - 为什么 Sonar :sonar needs mvn install before?

android - 是否可以从 Android 中的远程 mp3 文件检索专辑封面?

java - 如何在Android中使用Java从服务器获取的字符串中使用Pipeline Break?

php - 使用 Ghostscript 时禁止旋转 PDF 中的页面

ios - 有没有办法将pdf文件嵌入到html5页面中?

java - 在启动另一个函数的同时在后台运行一个函数

java - Android TicTacToe - 随机游戏结局

r - ggplot pdf中的嵌入式字体