-1

Unfortunately, program has stopped

I was trying to calculate age in android studio. It doesn't show any error message but when I click on the "Button" , the app crashes and shows "Unfortunately, Test1 has stopped."

Why my program is crashing even though it does not show any error message during compiling?

The java code is like below

package android.example.com.test1;

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

import java.util.Calendar;
import java.util.GregorianCalendar;

public class MainActivity extends AppCompatActivity {

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


}

public void hi() {

    Calendar now = new GregorianCalendar(2016, 3, 1);
    Calendar cal = new GregorianCalendar(2016, 2, 28);

    int res = now.get(Calendar.YEAR) - cal.get(Calendar.YEAR);
    if ((cal.get(Calendar.MONTH) > now.get(Calendar.MONTH))
            || (cal.get(Calendar.MONTH) == now.get(Calendar.MONTH) && cal.get(Calendar.DAY_OF_MONTH) > now
            .get(Calendar.DAY_OF_MONTH))) {
        res--;
    }


    TextView s = (TextView) findViewById(R.id.message);
    s.setText(" " + res);
}

}

The xml code is like following:

  <?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="android.example.com.test1.MainActivity">

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/message"
        android:onClick="hi"
        android:text="Button" />


</RelativeLayout>
Community
  • 1
  • 1
abc
  • 275
  • 3
  • 11
  • 2
    Compiling only shows the syntax is correct. It does not mean the code is correct or doesn't contain logic errors or problems. Compiling does not mean *works*. – Ken White Jan 31 '18 at 18:05

2 Answers2

2

You have a wrong signature to setup click listener, it should be

public void hi(View v){//...code}
//             ^^^^^^

From Docs

The method you declare in the android:onClick attribute must have a signature exactly as shown above. Specifically, the method must:

  • Be public
  • Return void
  • Define a View as its only parameter (this will be the View that was clicked)
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
0

This is because the onClick method must use a method that receives a view as a parameter.

public void hi(View view) {

    Calendar now = new GregorianCalendar(2016, 3, 1);
    Calendar cal = new GregorianCalendar(2016, 2, 28);

    int res = now.get(Calendar.YEAR) - cal.get(Calendar.YEAR);
    if ((cal.get(Calendar.MONTH) > now.get(Calendar.MONTH))
            || (cal.get(Calendar.MONTH) == now.get(Calendar.MONTH) && cal.get(Calendar.DAY_OF_MONTH) > now
            .get(Calendar.DAY_OF_MONTH))) {
        res--;
    }


    TextView s = (TextView) findViewById(R.id.message);
    s.setText(" " + res);
}

Another way is to cast the button and overwrite the onClick method to call the hi () method. For this, you need to delete the onClick attribute of the XML.

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


    Button button  = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            hi();
        }
    });
}

public void hi() {

    Calendar now = new GregorianCalendar(2016, 3, 1);
    Calendar cal = new GregorianCalendar(2016, 2, 28);

    int res = now.get(Calendar.YEAR) - cal.get(Calendar.YEAR);
    if ((cal.get(Calendar.MONTH) > now.get(Calendar.MONTH))
            || (cal.get(Calendar.MONTH) == now.get(Calendar.MONTH) && cal.get(Calendar.DAY_OF_MONTH) > now
            .get(Calendar.DAY_OF_MONTH))) {
        res--;
    }


    TextView s = (TextView) findViewById(R.id.message);
    s.setText(" " + res);
}
compor
  • 2,239
  • 1
  • 19
  • 29
Daniel Encina
  • 36
  • 1
  • 4