类型不匹配:无法将 void 转换为 toast

发布于 2024-11-02 13:21:42 字数 1948 浏览 3 评论 0原文

我的主要活动中有一个方法,其返回类型为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云归处 2024-11-09 13:21:42

问题是您可以将方法分配给变量。如果您想直接显示它,Toast 应该看起来像这样:

Toast.makeText(context, text, duration).show();

或者在您的情况下:

Toast.makeText(this, "sadasd", 2).show();

如果您想将 Toast 存储在变量中然后显示它,您必须这样做:

Toast toast = Toast.makeText(context, text, duration);

toast.show();

或者在您的具体情况

Toast toast = Toast.makeText(this, "sadasd", 2);
toast.show();

下 :旁注:最好使用 Toast 中的常量 LENGHT_SHORT 和 LENGTH_LONG 来定义持续时间,而不是 2。特别是如果 2 在这里似乎不是有效值。请参阅此处了解更多详细信息: http://developer.android.com/reference/android /widget/Toast.html

然后它看起来像这样:

Toast.makeText(this, "sadasd", Toast.LENGTH_LONG).show();

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:

Toast.makeText(context, text, duration).show();

or in your case:

Toast.makeText(this, "sadasd", 2).show();

If you want to store the Toast in a variable and then display it, you have to do it like this:

Toast toast = Toast.makeText(context, text, duration);

toast.show();

Or in your specific case:

Toast toast = Toast.makeText(this, "sadasd", 2);
toast.show();

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:

Toast.makeText(this, "sadasd", Toast.LENGTH_LONG).show();
裂开嘴轻声笑有多痛 2024-11-09 13:21:42
Toast m=Toast.makeText(this, "sadasd", 2);
m.show();
Toast m=Toast.makeText(this, "sadasd", 2);
m.show();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文