类型不匹配:无法将 void 转换为 toast
我的主要活动中有一个方法,其返回类型为 void。如果我在该方法内创建一个 Toast,它会显示错误“类型不匹配:无法将 void 转换为 toast”。谁能解释一下问题是什么并帮助我解决?
public class HelloList<View> extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
ListView lv=getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0,android.view.View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
// Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(),Toast.LENGTH_SHORT).show();
System.out.println(arg2);
String s="position is "+arg2;
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
}
});
registerForContextMenu(lv);
/*int i=lv.getCheckedItemPosition();
Toast.makeText(getApplicationContext(),,Toast.LENGTH_SHORT).show();*/
}
public void onCreateContextMenu(ContextMenu menu, android.view.View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(0x7f030000, menu);
}
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case 0x7f030000:
editNote(info.id);
return true;
default:
return super.onContextItemSelected(item);
}
}
public void editNote(long id) {
Toast m=Toast.makeText(this, "asdasd", 3);
m.show();
}
i have a method in my main activity which is of void return type.if i create a Toast inside the method it shows the error "Type mismatch:cannot convert void to toast". can anyone explain what is the problem and help me with solution ?
public class HelloList<View> extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
ListView lv=getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0,android.view.View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
// Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(),Toast.LENGTH_SHORT).show();
System.out.println(arg2);
String s="position is "+arg2;
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
}
});
registerForContextMenu(lv);
/*int i=lv.getCheckedItemPosition();
Toast.makeText(getApplicationContext(),,Toast.LENGTH_SHORT).show();*/
}
public void onCreateContextMenu(ContextMenu menu, android.view.View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(0x7f030000, menu);
}
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case 0x7f030000:
editNote(info.id);
return true;
default:
return super.onContextItemSelected(item);
}
}
public void editNote(long id) {
Toast m=Toast.makeText(this, "asdasd", 3);
m.show();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您可以将方法分配给变量。如果您想直接显示它,Toast 应该看起来像这样:
或者在您的情况下:
如果您想将 Toast 存储在变量中然后显示它,您必须这样做:
或者在您的具体情况
下 :旁注:最好使用 Toast 中的常量 LENGHT_SHORT 和 LENGTH_LONG 来定义持续时间,而不是 2。特别是如果 2 在这里似乎不是有效值。请参阅此处了解更多详细信息: http://developer.android.com/reference/android /widget/Toast.html
然后它看起来像这样:
The problem is that you can assign a method to a variable. A toast should look like this if you want to directly display it:
or in your case:
If you want to store the Toast in a variable and then display it, you have to do it like this:
Or in your specific case:
On a side note: It would be better to use the constants LENGHT_SHORT and LENGTH_LONG in Toast to define the duration instead of 2. Especially if 2 does not seem to be a valid value here. See here fore more details: http://developer.android.com/reference/android/widget/Toast.html
Then it would look like this: