Android从Activity类设置新的Intent

时间:2011-10-24 17:57:20

标签: android

我有扩展Activity的类。这有一个“注销”菜单选项。当我点击它从另一个类调用logout(context)时(不是从Activity扩展)。

public class MyTask extends Activity{

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.listlayout); 
}  
   @Override
   public boolean onCreateOptionsMenu(Menu menu)
   {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mytaskmenu, menu); 
return true;
   }   
   @Override 
   public boolean onOptionsItemSelected(MenuItem item) { 
       switch (item.getItemId()) { 
           case R.id.logout: 
               Functions.logout(getBaseContext());
               return true; 
           default: 
              return true; 
        } 
    } 
}


 public class Functions {

    public static void logout(Context context)
    {
        DbAdapter_User db2= new DbAdapter_User(context);
        db2.open();
        db2.handleLogout();
        db2.close();
        context.startActivity(new Intent(context, LogIn.class));
    }

错误是

android.util.AndroidRuntimeException: 
Calling startActivity() from outside of an Activity  context requires the         FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

什么错了?

1 个答案:

答案 0 :(得分:0)

您需要通过传递活动的上下文来调用函数类中的MyTask活动的上下文,而不是传递Functions.logout(getBaseContext()); try Functions.logout(this.getContext());