不幸的是,程序已经停止

时间:2018-01-31 17:58:33

标签: java android crash

很遗憾,程序已停止

我试图在android studio中计算年龄。它没有显示任何错误信息,但是当我点击"按钮" ,应用程序崩溃并显示"不幸的是,Test1已停止。"

为什么我的程序崩溃,即使它在编译期间没有显示任何错误消息?

java代码如下所示

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);
}

}

xml代码如下:

  <?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>

2 个答案:

答案 0 :(得分:2)

您有一个错误的签名来设置点击监听器,它应该是

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

From Docs

您在android:onClick属性中声明的方法必须具有完全如上所示的签名。具体来说,该方法必须:

  • 公开
  • 返回无效
  • 定义View as its only parameter(这将是点击的视图)

答案 1 :(得分:0)

这是因为onClick方法必须使用接收视图作为参数的方法。

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);
}

另一种方法是转换按钮并覆盖onClick方法以调用hi()方法。为此,您需要删除XML的onClick属性。

    @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);
}