0

这是代码:

public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        convertView = new ExpenseIcon(context);

        ((ExpenseIcon) convertView)
                .setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        System.out.println("Expense Icon Clicked");

                        Intent a = new Intent(context, ExpenseInput.class);
                        a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP & Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(a);
                    }
                });
    }

    if (image == null)
        image = BitmapFactory.decodeResource(context.getResources(), R.drawable.dollar);

    ((ExpenseIcon)convertView).setImageBitmap(image);

    return convertView;
}

当我单击 ExpenseIcon 按钮时,我在 context.startActivity(a) 行抛出异常:

04-28 11:04:53.100: D/AndroidRuntime(24962): 关闭 VM 04-28 11:04:53.100: W/dalvikvm(24962): threadid=1: 线程退出未捕获异常 (group=0x41ebc930) 04 -28 11:04:53.130: E/AndroidRuntime(24962): 致命异常: main 04-28 11:04:53.130: E/AndroidRuntime(24962): android.util.AndroidRuntimeException: 从 Activity 外部调用 startActivity()上下文需要 FLAG_ACTIVITY_NEW_TASK 标志。这真的是你想要的吗?04-28 11:04:53.130: E/AndroidRuntime(24962): 在 android.app.ContextImpl.startActivity(ContextImpl.java:944) 04-28 11:04:53.130: E/AndroidRuntime(24962): 在 android. app.ContextImpl.startActivity(ContextImpl.java:931) 04-28 11:04:53.130: E/AndroidRuntime(24962): 在 android.content.ContextWrapper.startActivity(ContextWrapper.java:284) 04-28 11:04: 53.130:

我不确定为什么,也不知道如何解决...

4

1 回答 1

1

试试这个,

Intent a = new Intent(context, ExpenseInput.class);
a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(a);
于 2013-04-28T02:40:21.733 回答