'Android button onclick method isn't working

I have the following piece of code
Xml:

<TextView
        android:id="@+id/mainTextView"
        android:textSize="30sp"
        android:text="@string/hello_msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

<Button 
        android:id="@+id/submitButton"
        android:textColor="@color/colorPrimary"
        android:text="Click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:onClick="submit()"/>

Java:

public void submit(View view){
        setContentView(R.layout.main);
        TextView tv1 = (TextView)findViewById(R.id.mainTextView);
        tv1.setText("Clicked");
    }

Every time I tap the button my app crashes. How to fix it now?



Solution 1:[1]

You must be getting compile time error

onClick handler method name cannot contain the character '(' in line android:onClick="submit()".

And you might be getting you UI not visible as setContentView(R.layout.main) inside button click callback.

public void submit(View view){
        setContentView(R.layout.main);
        TextView tv1 = (TextView)findViewById(R.id.mainTextView);
        tv1.setText("Clicked");
    }

You must set this in a proper Activity lifecycle method viz in OnCreateView().Something like this :

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

Happy Coding

Solution 2:[2]

The onClick Attribute can't parse the values which contains special characters. You have used the '(' and ')'. which is valid java. It cannot be used in XML Attributes.

write only clear name in xml attritube like,

...
android:onClick="submit"
...

And then create a method inside the MainActivity.java. you can press ALT+ENTER on the method name in XML attribute"submit", which will create method for you.

if this is not working then confirm that your button is initialized. App crash happens because of unintialized components too.

Or you can use setOnClickListener method in java file .

...
Button btn; // This variable can be global or local as per your need

btn = findViewById(R.id.submitButton);

btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("Button","CLicked..");
            }
        });

...

Solution 3:[3]

First, move your setContentView inside the onCreate:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

Second, write your submit method inside the class:

public void submit(View view) {
        TextView tv1 = (TextView)findViewById(R.id.mainTextView);
        tv1.setText("Clicked");
    }

Solution 4:[4]

In your code ,

<Button 
        android:id="@+id/submitButton"
        android:textColor="@color/colorPrimary"
        android:text="Click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:onClick="submit()"/>

you can see a red line under submit(), if you keep mouse on it, you can see the description :

onClick handler method name cannot contain the character '('

after removing them, maybe again see a warning message:

Use databinding or explicit wiring of click listener in code

you have 3 options, first tools:ignore="UsingOnClickInXml" or use setOnClickListener in your activity and the last one data binding

<data>
    <variable
      name="listener"
      type="android.view.View.OnClickListener" />
</data>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman
Solution 2 Anirudhdhsinh Jadeja
Solution 3 Shailendra Madda
Solution 4