smamran
smamran

Reputation: 787

Toast not showing

I want to show Toast on actionItem() function inside. But it doesn't now showing Toast.

If I call

Context context = getApplicationContext();

Before actionItem() then the app crashes.

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class ShowWebViewActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_web_view);

    }


    @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_show_web_view, 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.
        // int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        //        if (id == R.id.action_settings) {
        //            return true;
        //        }

        switch (item.getItemId()) {
            case R.id.action_settings:
                actionItem();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }
// Context context = getApplicationContext() makes app to crash why??
    public void actionItem(){
// Toast function should call here. But not working
        Context context = getApplicationContext();
        Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG);
    }


}

Upvotes: 1

Views: 346

Answers (6)

Sohail Zahid
Sohail Zahid

Reputation: 8149

You missed the show() function.

Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG).show();

Upvotes: 0

Santosh Joshi
Santosh Joshi

Reputation: 3320

show() is missing use the following code

public void actionItem(){
// Toast function should call here. But not working
    Context context = getApplicationContext();
    Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG).show();
 }

Upvotes: 1

user3528635
user3528635

Reputation:

Show() is missing from your code. Kindly refer to my code:

Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG).show();

Upvotes: 4

N J
N J

Reputation: 27515

You missed show()

Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG);

it should be:

Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG).show();

Upvotes: 1

Pararth
Pararth

Reputation: 8134

Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG);  

should be:

Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG).show();  

You would have got an IDE warning also for this

Upvotes: 3

siriscac
siriscac

Reputation: 1707

It should be,

Toast.makeText(context, "Action Setting Selected", Toast.LENGTH_LONG).show();

Upvotes: 5

Related Questions