简体   繁体   中英

App Keeps Stopping when pressing a button

app keeps crashing when ever the button is pressed, tried on emulator as well as on a phone. I'm using Android Studio 3.1.2. How can i fix it, i've tried most of other solutions with same problems over here but didnt work

public class MainActivity extends AppCompatActivity {

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

    public void onButton(View v) {
        display(1);
    }

    public void display(int n) {
        TextView qt =  findViewById(R.id.quantity;
        qt.setText(n);
    }
}

here's its xml code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="Quantity"
        android:textAllCaps="true"
        android:textSize="32sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="123dp"
        android:id="@+id/quantity"
        android:text="0"
        android:textAllCaps="true"
        android:textSize="32sp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ORDER"
        android:textSize="32sp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="215dp"
        android:onClick="onButton"/>
</RelativeLayout>

do the below:

qt.setText(String.valueOf(n));
// or use qt.setText("" + n);

instead of

qt.setText(n);

It is because

setText(@StringRes int resid) 

takes the parameter as "int resId" which it expects to be the id of the String value from the Strings.xml file. But here you want to display the "int" value in the textview, so you need to use the overloaded method:

setText(CharSequence text) 

method instead, and convert your int value to the String using

String.valueOf(n) //or by ("" + n)
Convert String to Integer
qt.setText.String.valueOf(n));

Change this:

public void display(int n) {
    TextView qt =  findViewById(R.id.quantity;
    qt.setText(n);
}

To this:

public void display(int n) {
    TextView qt =  findViewById(R.id.quantity); //add closing bracket
    qt.setText("" + n); //makes 'n' a string
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM