码迷,mamicode.com
首页 > 其他好文 > 详细

context.startActivity时报错startActivity() from outside of an Activity context require the FLAG_ACTIVITY_NEW_TASK flag

时间:2014-07-07 20:48:23      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   java   color   使用   

源代码如下:

 

 1 public class ReceiveHandler extends BroadcastReceiver{
 2 
 3     private final String ACTION_RECE_LinkReply="com.hutao.linkRequestReceive";
 4     private Context context;
 5     private int ReceiveNumber;
 6     private byte businessType;
 7     @Override
 8     public void onReceive(Context context, Intent intent) {
 9         // TODO Auto-generated method stub
10         this.context=context;
11         if (intent.getAction().equals(ACTION_RECE_LinkReply)) {
12             ReceiveNumber=intent.getIntExtra("number", 0);
13             businessType=intent.getByteExtra("businessType", (byte) 0);
14             switch (businessType) {
15                 case DataReceive.AUDIO_CONNECT:
16                     audioHandle();                    
17                     break;
18     
19                 default:
20                     break;
21             }
22             
23         }
24         
25     }
26     private void audioHandle() {
27         // TODO Auto-generated method stub
28         AlertDialog builder=new AlertDialog.Builder(context).setTitle("注意:")
29                 .setIcon(R.drawable.dailog_icon)
30                 .setMessage("MS0"+ReceiveNumber+"请求建立语音连接,是否同意?")
31                 .setPositiveButton("同意", new DialogInterface.OnClickListener() {
32                     
33                     @Override
34                     public void onClick(DialogInterface arg0, int arg1) {
35                         // TODO Auto-generated method stub
36                         /*发送语音连接请求,同意接收*/
37                         System.out.println("进入语音连接请求中……………………");
38                         new Thread(new DataSend(CreateSignal.LinkReply(businessType))).start();/*发送连接请求同意信令,businessType业务类型*/
39                         new Thread(new DataSend(CreateSignal.AudioReceive())).start();/*发送音频接收操作命令*/
40                         Intent audioIntent=new Intent(context,PhoneInterface.class);
41                         audioIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
42                         context.startActivity(audioIntent);
43                     }
44                 })
45                 .setNegativeButton("取消", new DialogInterface.OnClickListener() {
46                     
47                     @Override
48                     public void onClick(DialogInterface arg0, int arg1) {
49                         // TODO Auto-generated method stub
50                         
51                     }
52                 }).create();
53         builder.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
54         builder.show();
55     }
56 
57 }

 

在没有audioIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);这句时候程序会报错,报错内容如下:

 1 06-28 11:24:40.359: E/AndroidRuntime(7397): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
 2 06-28 11:24:40.359: E/AndroidRuntime(7397):     at android.app.ContextImpl.startActivity(ContextImpl.java:617)
 3 06-28 11:24:40.359: E/AndroidRuntime(7397):     at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
 4 06-28 11:24:40.359: E/AndroidRuntime(7397):     at com.hotalk.ui.homepage.favorites.FavoritesListAdapter$7.onClick(FavoritesListAdapter.java:1509)
 5 06-28 11:24:40.359: E/AndroidRuntime(7397):     at android.view.View.performClick(View.java:2408)
 6 06-28 11:24:40.359: E/AndroidRuntime(7397):     at android.view.View$PerformClick.run(View.java:8816)
 7 06-28 11:24:40.359: E/AndroidRuntime(7397):     at android.os.Handler.handleCallback(Handler.java:587)
 8 06-28 11:24:40.359: E/AndroidRuntime(7397):     at android.os.Handler.dispatchMessage(Handler.java:92)
 9 06-28 11:24:40.359: E/AndroidRuntime(7397):     at android.os.Looper.loop(Looper.java:123)
10 06-28 11:24:40.359: E/AndroidRuntime(7397):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11 06-28 11:24:40.359: E/AndroidRuntime(7397):     at java.lang.reflect.Method.invokeNative(Native Method)
12 06-28 11:24:40.359: E/AndroidRuntime(7397):     at java.lang.reflect.Method.invoke(Method.java:521)
13 06-28 11:24:40.359: E/AndroidRuntime(7397):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
14 06-28 11:24:40.359: E/AndroidRuntime(7397):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
15 06-28 11:24:40.359: E/AndroidRuntime(7397):     at dalvik.system.NativeStart.main(Native Method)

报错的原因:

Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

Context中有一个startActivity方法,Activity继承Context,重载了startActivity方法。如果使用的startActivity方法,不会有任何限制,而如果使用Context的startActivity方法的话,就需要开启一个新的task,遇到上面那个异常的,都是因为使用了Context的startActivity方法。解决办法是:加一个flag,audioIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

 

 

 

context.startActivity时报错startActivity() from outside of an Activity context require the FLAG_ACTIVITY_NEW_TASK flag,布布扣,bubuko.com

context.startActivity时报错startActivity() from outside of an Activity context require the FLAG_ACTIVITY_NEW_TASK flag

标签:android   style   blog   java   color   使用   

原文地址:http://www.cnblogs.com/chuji1988/p/3813162.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!