1

My Android App Will Crashing when i clicked the button. I am running this on my device using android device. i not sure if my code are at fault or something else Device: Xiomi Note 4G android ver. 4.4 Android KitKat API 19 Android studio 2.2.3

Screenshot1 Screenshot2

1.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.pckreatif.buttonclicked2.MainActivity">
<Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:onClick="onClickButton (MainActivity)" />
<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/textView" />
</RelativeLayout>

2.MainActivity.java

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void onClickButton(View v){
    TextView text1;
    text1 = (TextView)findViewById(R.id.textView);
    text1.setText("TEST");
}
}
pckreatif
  • 49
  • 5
  • 1
    check this answer http://stackoverflow.com/questions/4153517/how-exactly-does-the-androidonclick-xml-attribute-differ-from-setonclicklistene – Noorul Jan 07 '17 at 09:49

4 Answers4

3

You have to specify method reference as following:

android:onClick="onClickButton"

So your Button should be:

<Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:onClick="onClickButton" />
Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
  • this code here there are auto add by Android Studio IDE when i pick onclick under button property. Never suspect that part to causing my app to crash. – pckreatif Jan 07 '17 at 10:09
  • @pckreatif looks strange. Android Studio never suggested such `onClick` property to me – Andrii Abramov Jan 07 '17 at 10:10
2

try with just... why do you need params there, no need to write that way.

onClick="onClickButton"
SRB Bans
  • 3,096
  • 1
  • 10
  • 21
0

Try changing

android:onClick="onClickButton (MainActivity)"

to

android:onClick="onClickButton"
Jagruttam Panchal
  • 3,152
  • 2
  • 15
  • 21
0

You just need to replace your onClick method name from onClickButton (MainActivity) to onClickButton Because space is not allowed in onClick

Just Replace your activity_main.xml with this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.pckreatif.buttonclicked2.MainActivity">
    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:onClick="onClickButton"/>
    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/textView" />
</RelativeLayout>
Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40