Cannot display Toast from an Activity other than my main activity

…衆ロ難τιáo~ 提交于 2019-12-12 13:22:43

问题


I have a Activity called main. If I call

Toast.makeText(this, "Hello World from main", Toast.LENGTH_SHORT);

this works fine. However, for every other activity in my application, I cannot display a Toast. No exception, nothing in the Log but I don't see the Toast.

My main activity starts another one with an options menu:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case R.id.main_menu_entry:

        Intent infolist = new Intent(this, infolist.class);
        startActivityForResult(infolist, R.layout.infolist);

        return true;
    default:
        return super.onOptionsItemSelected(item); 
    }
}

In my infolist activity I have another options menu which should display a Toast.

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case R.id.infolist_menu_entry:

                    // this Toast is never shown.
        Toast.makeText(this, "Hello World from infolist", Toast.LENGTH_Short);          
        return true;

    default:
        return super.onOptionsItemSelected(item); 
    }
}

Any ideas what could cause this issue? I am using the latest SDK with Min SDK Version = 3 and an 1.5 Emulator.


回答1:


I would say, classical error :
You forgot the Toast.show() method ;)




回答2:


You miss the show() method at the end.

Toast.makeText(this, "Hello World from infolist", Toast.LENGTH_Short).show();


来源:https://stackoverflow.com/questions/3466087/cannot-display-toast-from-an-activity-other-than-my-main-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!