package comxunfang.button; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends ActionBarActivity { private Button bt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt = (Button) findViewById(R.id.btn); //第一种 bt.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast.makeText(this, "这是第1种写法",0).show(); } }); }
如果这样写的话,就会报Multiple markers at this line
- Line breakpoint:MainActivity [line: 22] - onClick(View)
- The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (new View.OnClickListener(){},
String, int)
这样的错,如果把Toast.makeText(this, "这是第1种写法",0).show();改成Toast.makeText(MainActivity.this, "我弹出来了", 0).show() ;则是正常的
这说明上下文的对象导致这样的结果