android.util.AndroidRuntimeException:从Activity上下文外部调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志

时间:2017-02-11 08:38:48

标签: android menuitem

从我的MainActivity我想使用另一个类来操作我的菜单项。

我的主要活动

public class MainActivity extends AppCompatActivity {

Button familyButton, friendButton,loverButton,otherButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new MyCustomButtonClickListener(ReceipientType.FAB));

    familyButton = (Button) findViewById(R.id.button_family);
    friendButton = (Button) findViewById(R.id.button_friend);
    loverButton = (Button) findViewById(R.id.button_lover);
    otherButton = (Button) findViewById(R.id.button_other);

    familyButton.setOnClickListener(new MyCustomButtonClickListener(ReceipientType.FAMILY));
    friendButton.setOnClickListener(new MySecondLayerClickListner(ReceipientType.FRIEND));
    loverButton.setOnClickListener(new MyCustomButtonClickListener(ReceipientType.LOVER));
    otherButton.setOnClickListener(new MyCustomButtonClickListener(ReceipientType.OTHER));


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    MenuController menuController = new MenuController();
    menuController.setmenus(this,item);

    return super.onOptionsItemSelected(item);
}}

我的MenuController类是

public class MenuController {


public void setmenus(AppCompatActivity compatActivity, MenuItem item) {
    AppCompatActivity appCompatActivity = compatActivity;
    Intent myIntent;
    int id = item.getItemId();
    AdController adController = new AdController();
    switch (id){
        case R.id.action_home:
            myIntent = new Intent(appCompatActivity.getApplicationContext(), MainActivity.class);
            appCompatActivity.startActivityForResult(myIntent, 0);
        case R.id.action_support:
            adController.loadInterstitialAd(appCompatActivity);
        case  R.id.action_shareApps:
            adController.openShareApps(appCompatActivity.getApplicationContext(),"Hey, I have found this application helpful for you!! https://play.google.com/store/apps/details?id=com.ctl.valentinemessages");
        case R.id.action_rate:
            try {
                appCompatActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.ctl.valentinemessages")));
            } catch (android.content.ActivityNotFoundException anfe) {
                appCompatActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pub:Md+Ziaur+Rahman")));
            }
        case  R.id.action_more_apps:
            try {
                appCompatActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pub:Md+Ziaur+Rahman")));
            } catch (android.content.ActivityNotFoundException anfe) {
                appCompatActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/developer?id=Md+Ziaur+Rahman")));
            }
    }
}}

现在每当我按主菜单项时,我得到 android.util.AndroidRuntimeException:从Activity上下文外部调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志。这真的是你想要的吗?

0 个答案:

没有答案