setOnClickListener()与报错OnClickListener()原因

setOnClickListener()与报错OnClickListener()原因

protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.first_layout);
        Button button1 = (Button) findViewById(R.id.button_1);
        button1.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                Toast.makeText(FirstActivity.this,"You are clicked Button 1",
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

程序报错,并提示
Multiple markers at this line
- OnClickListener cannot be resolved to a type
- The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){})

后发现是导入android.view.View;时,并不会导入android.view.View.OnClickListener,需要手动导入这个包。

你可能感兴趣的:(Android学习,报错)