Search code examples
androidandroid-studioonclicklistener

Why is my code erroring?


While setting my on click listener for a particular button named convert When I write

The code gives a red underline at the space between View and v

I Am new to android studio and tried fixing this by searching on Google but no avail. Where and how am I wrong ?

Edit 1: I sorted out the errors apparently it was a misplaced curly brace at the end of onCreate effectively terminating the onCreate before my actual code was executed. However now it executes well except for one tiny little flaw it shows no output. What should I do?

Code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final EditText inputNum = (EditText) findViewById(R.id.inputNum);
    final TextView octNum = (TextView) findViewById(R.id.octNum);
    final TextView binNum = (TextView) findViewById(R.id.octNum);
    final TextView hexNum = (TextView) findViewById(R.id.octNum);
    final Button convert = (Button) findViewById(R.id.convert);
    View.OnClickListener listener = new OnClickListener() {
        @Override
        public void onClick(final View v) {
            convert.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    int a = Integer.parseInt(inputNum.getText().toString());
                    String bin = Integer.toBinaryString(a);
                    String oct = Integer.toOctalString(a);
                    String hex = Integer.toHexString(a);
                    octNum.setText(oct);
                    binNum.setText(bin);
                    hexNum.setText(hex);
                }
            });


        }
    };
}

@Override
public void onClick(View v) {

}

Solution

  •    convert.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //  handle your click event
                }
            });
    

    EDIT

    Push-buttons can be pressed, or clicked, by the user to perform an action!

    A typical use of a push-button in an activity would be the following: .

    public class MyActivity extends Activity {
         protected void onCreate(Bundle icicle) {
             super.onCreate(icicle);
             setContentView(R.layout.content_layout_id);
    
             final Button button = (Button) findViewById(R.id.button_id);
             button.setOnClickListener(new View.OnClickListener() {
                 public void onClick(View v) {
                     // Perform action on click
                 }
             });
         }
     }
    

    However, instead of applying an OnClickListener to the button in your activity, you can assign a method to your button in the XML layout, using the android:onClick. You are trying to achievesome kind of a thing and messed with setOnClickListener. Are you?

    Now, when a user clicks the button, the Android system calls the activity's myClickMethod(View)(that i created) method. In order for this to work, the method must be public and accept a View as its only parameter. For example:

         <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="myClickMethod"
            android:text="button" />
    

    and in code

    public void myClickMethod(View view) {
         // Perform action 
     }