【发布时间】:2013-01-25 20:02:45
【问题描述】:
我为我工作的公司创建了一个通用的可重用类,以创建一些通用的界面元素。
该类在构造中采用单个参数:应用程序上下文。
其中一种方法,ContentClickableRowWithIcon 允许您传入意图用作点击操作。
这里是完整的方法声明:
public LinearLayout ContentClickableRowWithIcon(Drawable icon, String title, Intent i, final Boolean chooser)
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;
}
正如 cmets 中所述,只要我不提供使用选择器的功能,一切正常(此列表中的所有内容都会获得一个新的活动标志,我很清楚这一点,并会在解决此问题时进行清理)
我这样做的那一刻,抛出异常:
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
我的想法已经用完了......
/// EDIT:: 值得注意的是,在调试时,Intent 中的 flags 属性使用 addFlags 设置为 268435456,使用 setFlags 设置为 268435456,当它到达在 onClick 操作中使用 Intent 的时间时
【问题讨论】:
-
你试过
mContext.startActivity(Intent.createChooser(i, "Complete With..."));吗? -
我的猜测是 createChooser() 返回的新 Intent 没有设置 FLAG_ACTIVITY_NEW_TASK。尝试获取意图,设置标志,然后调用 startActivity
-
除了我需要最终确定之外?当我这样做时(在参数
final Intent i中设置,标志永远不会设置,调试中的标志 = 0 -
刚刚这样做:
if(chooser) { Intent intent = i; intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mContext.startActivity(Intent.createChooser(intent, "Complete With...")); }in leu,仍然崩溃:\ -
试试这个:
Intent newIntent = Intent.createChooser(intent, "Complete With..."); newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mContext.startActivity(newIntent);
标签: java android android-intent android-sdk-2.3