开发者

Type mismatch:cannot convert void to toast

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=getListV开发者_运维问答iew();
      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();

    }


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();


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜