2016-05-20 2 views
0

は:は、Java/Androidの - スタート新しいアクティビティフラグエラーエミュレータで私のアプリをテストしながら、私は、次のエラーが発生します

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); 
    } 
} 

ありがとうございます!

答えて

-1

あなたはコンテキストが活動contextではありません、あなたが活動contextを使用するか、またはあなたのintent

+0

しかし、彼はすでにそれを知っています。質問を読む –

0

にフラグIntent.FLAG_ACTIVITY_NEW_TASKを追加する必要がありますあなたのコードは正常に見える(私はi.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);自分自身を使用すると思います)ので、私の、私はあなたが間違ったファイルを変更することを推測していますあなたの変更が適用されないように、または何らかの理由で新しいビルドがデバイス/エミュレータにインストールされていないように、あなたのアプリケーションは正しく再構築されていません。私は、プロジェクトをきれいにして再構築し、デバッガに歩み寄ります(また、インテントが実際にフラグを保持しているかどうかを検査します)。

関連する問題