0

When I click on the Sign up txt on my application it crashes I've tried almost everything i could find including the XML onClick

XML clickable onClick

public class LoginActivity extends AppCompatActivity
{
    TextView sign_up_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        sign_up_text = (TextView) findViewById(R.id.sign_up);
        sign_up_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));

            }
        });

    }

}

xml:

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/sign_up_text"
        android:textSize="18dp"
        android:gravity="center"
        android:layout_alignParentBottom="true"
        android:id="@+id/sign_up"
        android:clickable="true"
        android:onClick="onClick"
        />

Android app crashes with no failure

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140

2 Answers2

4

You have android:onClick="onClick" attribute in your xml. And you have also define sign_up_text.setOnClickListener in java.

You can do only one thing at a time.

1.

If you want to use sign_up_text.setOnClickListener , you simply need to remove android:onClick="onClick" from xml file.(Reference)

2.

If you want to use android:onClick="onClick", you should define new method in your activity like:(Reference)

public void onClick(View view) {

}
Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
3

First solution is to remove android:onClick="onClick" from your XML, according to my perception the reason of crash is the function name called on onClick of your button click define in XML is "onClick" which is also override function of setOnClickListner

@Override
        public void onClick(View v){
         //todo your code
        }

Second Solution is if you use onClick in XMl change function name other that override function onClick like:

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/sign_up_text"
        android:textSize="18dp"
        android:gravity="center"
        android:layout_alignParentBottom="true"
        android:id="@+id/sign_up"
        android:clickable="true"
        android:onClick="myFunction"
        />

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

}

private myFunction(View view){
}
Usama Nasir
  • 134
  • 5