2013-04-28 52 views
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拋出的異常(一):

04-28 11:04:53.100:d/AndroidRuntime(24962):關閉VM 04-28 11:04:53.100:W/dalvikvm(24962):線程ID = 1:線程退出與 未捕獲的異常(group = 0x41ebc930)04-28 11:04:53.130: E/AndroidRuntime(24962):致命異常ON: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):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):在 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):在 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):在 android.os.Looper.loop(Looper.java:137)04-28 11:04:53.130: E/AndroidRuntime(24962):在 android.app.ActivityThread.main(ActivityThread.java:5041)04 -28 11:04:53.130:E/AndroidRuntime(24962):在 java.lang.reflect.Method.invokeNative(本機方法)04-28 11:04:53.130:E/AndroidRuntime(24962):在 java.lang.refl ect.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):在 dalvik.system.NativeStart.main(本機方法)

我不確定爲什麼和不知道如何解決......

+2

「從Activity上下文外調用startActivity()需要FLAG_ACTIVITY_NEW_TASK標誌。 「這是你想要的嗎?」它在堆棧跟蹤中。 – Makoto 2013-04-28 01:08:44

+0

你是否搜索過「從Activity上下文中調用startActivity()需要FLAG_ACTIVITY_NEW_TASK標誌。」? – 2013-04-28 01:10:14

+0

我嘗試在... – tweetypi 2013-04-28 01:53:02

回答

1

試試這個,

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