首页 > 其他 > 详细

Toast--报错

时间:2017-05-13 09:35:17      阅读:392      评论:0      收藏:0      [点我收藏+]

 

Toast的makeText()方法报错
帖上一代码,用Toast来简单处理回调机制.但是Toast的makeText()方法老报错:
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.Toast;

public class MyButton  extends  Button {


public MyButton(Context context, AttributeSet attrs) {
   super(context, attrs);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
  //就是这里了

Toast toast=Toast.makeText(MyButton.this, "the callBack test", 5000);
toast.show();
return true;
}
}
报错信息:

The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (MyButton, String, int)

------解决方案--------------------
public class MyButton extends Button {

private Context mContext;

public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
   mContext = context;
}

Toast toast=Toast.makeText(mContext, "the callBack test", 5000);
toast.show(); 
------解决方案--------------------
第一个参数,使用  getContext() 或者 getApplicationContext()
第三个参数也不正确,应该是   Toast.makeText(getContext(), "the callBack test", Toast.LENGTH_LONG).show();

Toast--报错

原文:http://www.cnblogs.com/awkflf11/p/6847844.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!