尝试启动活动会导致抛出异常,不确定原因

时间:2013-04-28 01:07:11

标签: java android button android-activity

以下是代码:

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:线程退出   未捕获的异常(组= 0x41ebc930)04-28 11:04:53.130:   E / AndroidRuntime(24962):致命异常:主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):at   android.app.ContextImpl.startActivity(ContextImpl.java:944)04-28   11:04:53.130:E / AndroidRuntime(24962):at   android.app.ContextImpl.startActivity(ContextImpl.java:931)04-28   11:04:53.130:E / AndroidRuntime(24962):at   android.content.ContextWrapper.startActivity(ContextWrapper.java:284)   04-28 11:04:53.130:E / AndroidRuntime(24962):at   com.example.mytestapp.controls.ExpenseIconGridGenerate $ 1.onClick(ExpenseIconGridGenerate.java:63)   04-28 11:04:53.130:E / AndroidRuntime(24962):at   android.view.View.performClick(View.java:4204)04-28 11:04:53.130:   E / AndroidRuntime(24962):at   android.view.View $ PerformClick.run(View.java:17355)04-28   11:04:53.130:E / AndroidRuntime(24962):at   android.os.Handler.handleCallback(Handler.java:725)04-28   11:04:53.130:E / AndroidRuntime(24962):at   android.os.Handler.dispatchMessage(Handler.java:92)04-28   11:04:53.130:E / AndroidRuntime(24962):at   android.os.Looper.loop(Looper.java:137)04-28 11:04:53.130:   E / AndroidRuntime(24962):at   android.app.ActivityThread.main(ActivityThread.java:5041)04-28   11:04:53.130:E / AndroidRuntime(24962):at   java.lang.reflect.Method.invokeNative(Native Method)04-28   11:04:53.130:E / AndroidRuntime(24962):at   java.lang.reflect.Method.invoke(Method.java:511)04-28 11:04:53.130:   E / AndroidRuntime(24962):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)   04-28 11:04:53.130:E / AndroidRuntime(24962):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)04-28   11:04:53.130:E / AndroidRuntime(24962):at   dalvik.system.NativeStart.main(原生方法)

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

1 个答案:

答案 0 :(得分:1)

试试这个,

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