android按钮事件监听器不起作用

use*_*678 0 android

我知道这是基本的东西,但我无法弄清楚.这是我的代码:

public class test extends Activity{

     private static final String TAG = "test";
     private Button Test;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Log.v(TAG,"onCreate is called.");

        this.setContentView(R.layout.main);
        Test= (Button)this.findViewById(R.id.Test);


        Test.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Log.v(TAG, "onClick Entered.");
                // Perform action on click
            }
        });

        setContentView(R.layout.main);
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以看到第一个日志"OnCreate"但按钮单击事件监听器似乎不起作用,因为我看不到"OnClick Entered".我哪里做错了?

谢谢

Che*_*mon 5

您正在调用setContentView两次,这是您在设置单击侦听器后第二次调用.这意味着您添加侦听器的视图不再可见,它将替换为视图的其他实例.删除第二个setContentView.