Solution for: error: no suitable method found for makeText

Problem: 


You get the below error upon compiling Android App.

    [javac] Compiling 3 source files to C:\xxxx\Workspace\MyApp\bin\classes
    [javac] C:\xxxx\Workspace\MyApp\src\com\akp\MyApp\MainActivity.java:28: error: no suitable method found for makeText(<anonymous OnKeyListener>,String,int)
    [javac] Toast.makeText(this, "This is a toast!", Toast.LENGTH_SHORT).show();
    [javac]     ^
    [javac]     method Toast.makeText(Context,int,int) is not applicable
    [javac]       (actual argument <anonymous OnKeyListener> cannot be converted to Context by method invocation conversion)
    [javac]     method Toast.makeText(Context,CharSequence,int) is not applicable
    [javac]       (actual argument <anonymous OnKeyListener> cannot be converted to Context by method invocation conversion)
    [javac] 1 error

Quick Solution: 

Change

Toast.makeText(this, "This is a toast!", Toast.LENGTH_SHORT).show();
to

Toast.makeText(MainActivity.this, "This is a toast!", Toast.LENGTH_SHORT).show();


Solution Explained:

1. I had created a Toast in an EditText OnKeyListener in the onCreate method. 
The toast used 'this' only. 
I have found that inside an onKeyListener you must use the activity name before the 'this. 
So it would be 'MainActivity.this' instead of just 'this'.

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
editText = (EditText) findViewById(R.id.editText);
 
editText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
//If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
//...
// Perform your action on key press here
Toast.makeText(this, "This is a toast, using this!", Toast.LENGTH_SHORT).show(); 
// ...    
return true;
}
return false;
}
});  
    }

Now test it!