[英]Context wants FLAG_ACTIVITY_NEW_TASK but I've already set that flag
I've created a common re-usable class for the company I work for to create some common interface elements.我为我工作的公司创建了一个通用的可重用类,以创建一些通用的界面元素。
The class, takes in a single parameter as in the construct: an application context.该类在构造中采用单个参数:应用程序上下文。
one of the methods, ContentClickableRowWithIcon
allows you to pass in an intent to be used as the click action.其中一种方法, ContentClickableRowWithIcon
允许您传入一个意图作为点击操作。
heres the full method declaration:这是完整的方法声明:
public LinearLayout ContentClickableRowWithIcon(Drawable icon, String title, Intent i, final Boolean chooser)
that last attribute there is used in the onClickEvent to determine whether to invoke a Chooser or just go right into the intent.在 onClickEvent 中使用的最后一个属性来确定是调用选择器还是直接进入意图。
public LinearLayout ContentClickableRowWithIcon(Drawable icon, String title, Intent i, final Boolean chooser) {
LinearLayout ll = new LinearLayout(mContext);
// .. LinerLayout construction, has nothing to do with the action
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this is apparently getting ignored... (ps: i've tried i.setFlags as well)
final Intent intent = i;
ll.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(chooser)
mContext.startActivity(Intent.createChooser(intent, "Complete With...")); // crashes here with: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
else
mContext.startActivity(intent); // this works fine
}
});
return ll;
}
As mentioned in the comments, anytime I dont provide the ability to use a chooser, everything works fine (everything in this list gets a new activity flag, im well aware of this and will cleanup when this issue is figured out)正如评论中提到的,只要我不提供使用选择器的能力,一切正常(此列表中的所有内容都会获得一个新的活动标志,我很清楚这一点,并且会在解决此问题时进行清理)
The moment I do, throws the exception: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
我这样做的那一刻,抛出异常: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
I've run out of ideas...我已经没有想法了...
/// EDIT:: Worth noting, on debug, the flags attribute in the Intent is set to 268435456
with addFlags and 268435456
with setFlags, when it reaches the time to use the intent in the onClick action /// EDIT:: 值得注意的是,在调试时,Intent 中的 flags 属性设置为268435456
with addFlags 和268435456
with setFlags,当它达到在 onClick 操作中使用意图的时间时
Problem has been fixed, I think this is simply the case of an "order of operation" scenario问题已解决,我认为这只是“操作顺序”场景的情况
heres what allowed this thing to work:继承人是什么让这件事起作用:
ll.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(chooser) {
Intent intent = Intent.createChooser(i, "Complete With");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
} else
mContext.startActivity(i);
}
});
also added a "final" modifier to the parameter in the method declaration还在方法声明中的参数中添加了“final”修饰符
public LinearLayout ContentClickableRowWithIcon(Drawable icon, String title, final Intent i, final Boolean chooser)
Actually your exception mean that you are using Not Activity Context.实际上,您的异常意味着您使用的是 Not Activity Context。 it could be called from Application context.它可以从应用程序上下文中调用。 Check that you are in Activity context since this is not a service检查您是否在 Activity 上下文中,因为这不是服务
I fixed it adding the flag to the Chooser Intent我修复了将标志添加到选择器意图的问题
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND)
.setType("text/plain")
.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here")
.putExtra(android.content.Intent.EXTRA_TEXT, url)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(Intent.createChooser(sharingIntent, "Share with").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.