Error due to invalid combination of Toast and OnClickListener

99封情书 提交于 2019-12-31 08:34:09

问题


I'm trying to use Toast inside OnCLickListener. My code triggers the following error:

The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (new View.OnClickListener(){}, String, int)

This is my code:

    Button register = (Button) findViewById(R.id.register);
    register.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            EditText name = (EditText)findViewById(R.id.name);
            String Lname = name.getText().toString();

            Toast.makeText(this, Lname, Toast.LENGTH_SHORT).show();



        }
    });

回答1:


As The Kenny said, this is refering to the View.OnClickListener instead of your Activity. Change this, to MyActivity.this.

For example,

public class MyActivity extends Activity {
// ... other code here
Toast.makeText(MyActivity.this, Lname, Toast.LENGTH_SHORT).show();



回答2:


In this case, this refers to the instance of the anonymous subclass of View.OnClickListener. You have to refer to the this of the class where you create the anonymous class.




回答3:


Use MyActivity.this as this refers to your onclickListener.




回答4:


You can use getApplicationContext() as well. See the documentation.




回答5:


Anywhere, just use the following:

((Activity) mContext).runOnUiThread(new Runnable() {
                    public void run() {
                        Toast my_toast = Toast.makeText(mContext, "YOUR TEXT OR STRING", Toast.LENGTH_LONG);
                        my_toast.setGravity(Gravity.CENTER, 0, 0);
                        my_toast.show();
                    }
                });

You just need to define at the top of your activity (just after the onCreate):

mContext = this;

Also, see that I decomposed it a bit to be able to handle the gravity as I want (sometimes you may want the toast to appear at the center of the screen)...




回答6:


Another approach to achieve your goal is to implement the OnClickListener interface. This way you implement the onClick() method in your Activity and you could thus assign this. In addition, you can assign this to multiple Buttons. You can distinguish these Buttons from each other by comparing their IDs via an appropriate if, respectively switch statement within the onClick() method.

public class MyActivity extends Activity implements OnClickListener{

    // ...

    protected void onCreate (Bundle savedInstanceState){
        // ...
        Button register = (Button) findViewById(R.id.register);
        register.setOnClickListener(this); 
    }

    public void onClick(View arg0) {
        EditText name = (EditText) findViewById(R.id.name);
        String text = name.getText().toString();

        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }
}



回答7:


try this

 public void onClick(View arg0) {
        EditText name = (EditText)findViewById(R.id.name);
        String Lname = name.getText().toString();
        Toast.makeText(arg0.getContext(), Lname, Toast.LENGTH_SHORT).show();
    }


来源:https://stackoverflow.com/questions/4531539/error-due-to-invalid-combination-of-toast-and-onclicklistener

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