为什么Toast.makeText不接受格式化文本?

时间:2017-02-18 15:20:55

标签: android android-studio toast

作为android中的begginer,我在使用Toast.makeText时遇到困难,这里是代码。

public class CustomOnSelectedListen implements OnItemSelectedListener{

public void onItemSelected(AdapterView<?> parent, View view,int pos,long id){
    Toast.makeText(parent.getContext(),
            "OnItemSelectedListener :" + parent.getItemAtPosition(pos).toString(),
            Toast.LENGTH_SHORT).show();
}

 public void onNothingSelected(AdapterView<?> arg0){
     Toast.makeText(CustomOnSelectedListen.this,"Please select place and class".toString(),Toast.LENGTH_SHORT).show();
 }

}

,错误是

Error:(19, 15) error: no suitable method found for  
       makeText(CustomOnSelectedListen,String,int)
method Toast.makeText(Context,CharSequence,int) is not applicable
(argument mismatch; CustomOnSelectedListen cannot be converted to Context)
method Toast.makeText(Context,int,int) is not applicable
(argument mismatch; CustomOnSelectedListen cannot be converted to Context)

这里CustomOnSelectedListen无法转换,可能出现什么问题?

编辑1 :是的CustomOnSelectedListen不是一个上下文实例,我需要使用toast在函数onNothingSelected()中显示一条消息,有哪些不同的方法呢?

1 个答案:

答案 0 :(得分:1)

onNothingSelected功能上:

Toast.makeText(CustomOnSelectedListen.this,"Please select place and class".toString(),Toast.LENGTH_SHORT).show();

第一个参数(CustomOnSelectedListen.this)不是Context个实例。