首页 > 代码库 > 当写listview的onItemClick的方法时写Toast的参数context写成this出现can't resolve method ’make text(OnClickListener,java.lang.String,int)'的错误,原因

当写listview的onItemClick的方法时写Toast的参数context写成this出现can't resolve method ’make text(OnClickListener,java.lang.String,int)'的错误,原因

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                Fruit fruit = fruitList.get(position);                 //Toast.makeText(this,fruit.getName(),Toast.LENGTH_SHORT).show();/*出现错误,这也是错误的写法,
提示can‘t resolve method ’make text(OnClickListener,java.lang.String,int)‘*/ Toast.makeText(MainActivity.this,fruit.getName(),Toast.LENGTH_SHORT).show(); } });

 在listview的onItemClick方法中写Toast.maketext()的时候其中参数Context,不能含糊不清直接写this,而是要正确的写出MainActivity.this。

当写listview的onItemClick的方法时写Toast的参数context写成this出现can't resolve method ’make text(OnClickListener,java.lang.String,int)'的错误,原因