CuriousNewbie
CuriousNewbie

Reputation: 329

onClickListener trouble

So, I'm learning and developing a very small app with Android AIDE on my phone, I have code like this:

package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;

public class MainActivity extends Activity {

    EditText edit1;
    Button btn1;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        edit1 = (EditText) findViewById(R.id.edit1);
        btn1 = (Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(this);
  //Problem (this) from AIDE error info: Method 
   'android.view.View.setOnClickListener(android.view.View.OnClickListener)' in 'android.widget.Button' can not be applied to    '(com.mycompany.myapp.MainActivity)'//
    }
    
    public void OnClick(View v) {
        switch (v.getId()){
            case R.id.btn1:
                Toast.makeText(getApplicationContext(),edit1.getText().toString(),Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
                
        }
    }


}
        

What is the problem?

Upvotes: 0

Views: 511

Answers (3)

Muhammad Idrees
Muhammad Idrees

Reputation: 198

This Code is working (Tested)

package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;

     public class MainActivity extends Activity implements View.OnClickListener{
            EditText edit12;
            Button btn12;  
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                
                edit12 = (EditText) findViewById(R.id.edit1);
                btn12 = (Button) findViewById(R.id.btn1);
                btn12.setOnClickListener();
            }
           
             public void onClick(View v){
                switch (v.getId()){
                    case R.id.btn12:
                        Toast.makeText(getApplicationContext(),edit1.getText().toString(),Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        break;                    
                }
            }
        }

Upvotes: 0

MarsAtomic
MarsAtomic

Reputation: 10696

You know what this refers to, don't you? That's a reference for your activity, but setOnClickListener() takes an argument of type View.OnClickListener, not your activity.

Look at the sample code from the official doc:

     final Button button = findViewById(R.id.button_id);
     button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             // Code here executes on main thread after user presses button
         }
     });

When you create the anonymous inner class with new View.OnClickListener(), that anonymous class implements the OnClick interface.

If you just implement View.OnClickListener on your MainActivity, you'd have to change the way you handle events, and I presume you want to minimize the amount of refactoring.

Upvotes: 2

Uuu Uuu
Uuu Uuu

Reputation: 1282

Your activity have to implement View.OnClickListener interface

    public class MainActivity extends Activity implements View.OnClickListener {
        @Override
        public void onClick(View view) {}
    }

Upvotes: 4

Related Questions