イベントリスナーのエラーの詳細について

2377 ワード

リスナーはonCreateの外に置いてもいいし、setOnClickListenerの中でnewを直接出してもいいし、1つのactivityから別のactivityまでの例を挙げてもいいです.
エラーコード
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1=(Button)findViewById(R.id.btn2);// Id
        button1.setOnClickListener(listener1);// 
        this.listener1=new OnClickListener(){// 
            public void onClick(View v){
                Intent intent1=new Intent(MainActivity.this,SubActivity.class);
                intent1.putExtra("activitymain", " MaintActivity ");// 
                startActivityForResult(intent1,Request_code);// activity
            }
        });
//        button1=(Button)findViewById(R.id.button1);
//        button1.setOnClickListener(listener1);
        setTitle(" mainactivity ");
    }

setOnClickListenerでnewが出てきます
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1=(Button)findViewById(R.id.btn2);// Id
        button1.setOnClickListener(listener1);// 
        button1.setOnClickListener(new OnClickListener(){// 
            public void onClick(View v){
                Intent intent1=new Intent(MainActivity.this,SubActivity.class);
                intent1.putExtra("activitymain", " MaintActivity ");// 
                startActivityForResult(intent1,Request_code);// activity
            }
        });
        setTitle(" mainactivity ");
    }

リスナーはonCreateの外に置くことができます
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1=(Button)findViewById(R.id.btn2);// Id
        button1.setOnClickListener(listener1);// 

        setTitle(" mainactivity ");
    }
    OnClickListener listener1=new OnClickListener(){// onclick !
        public void onClick(View v ){
            Log.d(" TAG","onClick into");
            Intent intent1=new Intent(MainActivity.this,SubActivity.class);
            intent1.putExtra("activitymain", " MaintActivity ");// 
            startActivityForResult(intent1,Request_code);// activity
        }
    };