Java / Android - 启动新的活动标志错误

时间:2016-05-20 14:38:26

标签: java android android-intent

我在模拟器中测试我的应用时收到以下错误:

05-20 09:33:20.755 16679-16679/midamcorp.com.burgerkingapp E/AndroidRuntime: FATAL EXCEPTION: main


Process: midamcorp.com.burgerkingapp, PID: 16679
                                                                         android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
                                                                             at android.app.ContextImpl.startActivity(ContextImpl.java:672)
                                                                             at android.app.ContextImpl.startActivity(ContextImpl.java:659)
                                                                             at android.content.ContextWrapper.startActivity(ContextWrapper.java:331)
                                                                             at android.content.ContextWrapper.startActivity(ContextWrapper.java:331)
                                                                             at midamcorp.com.burgerkingapp.appUpdater.onPostExecute(appUpdater.java:52)
                                                                             at midamcorp.com.burgerkingapp.appUpdater.onPostExecute(appUpdater.java:18)
                                                                             at android.os.AsyncTask.finish(AsyncTask.java:651)
                                                                             at android.os.AsyncTask.-wrap1(AsyncTask.java)
                                                                             at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

但我不明白,我已将意志添加到旗帜中。我甚至尝试在setFlags和addFlags之间交替,但没有成功。我可能会缺少什么?

public class appUpdater extends AsyncTask<URL, String, String> {
   private Context c;
public  appUpdater(Context context) {
    this.c = context;
}

    protected String doInBackground(URL... appUrl) {

        // download app file, provided a URL

        String location = c.getExternalFilesDir(null) + "/app.apk";
        try {
            URL url = appUrl[0];
            URLConnection con = url.openConnection();
            con.connect();
            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream(location);
            byte[] buffer = new byte[1024];
            int read = 0;

            while ((read = input.read(buffer)) != -1) {

                output.write(buffer, 0, read);
            }
        output.close();
        input.close();


        } catch(Exception e){
                Log.e(this.getClass().toString(), Log.getStackTraceString(e));
            }
        return location;
    }
    @Override
    protected void onPostExecute(String saveLocation) {

        // install app file

        Intent i = new Intent();
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         i.setAction(Intent.ACTION_VIEW);
        Log.i("Location of app is: ", " " + saveLocation);
        i.setDataAndType(Uri.fromFile(new File(saveLocation)), "application/vnd.android.package-archive");

        c.startActivity(i);
    }
}

谢谢!

2 个答案:

答案 0 :(得分:0)

您的代码看起来很好(我自己使用i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);)所以我猜测您编辑错误的文件或者您的应用未正确重建(或根本没有)因此您的更改不是应用或由于某些原因未在设备/仿真器上安装新版本。我清理项目并重建,然后使用调试器步进(并检查intent是否真的持有标志)。

答案 1 :(得分:-1)

您的上下文不是活动context,您应该使用活动context或将标记Intent.FLAG_ACTIVITY_NEW_TASK添加到您的intent