代码之家  ›  专栏  ›  技术社区  ›  abc

不幸的是,程序已停止[重复]

  •  -1
  • abc  · 技术社区  · 7 年前

    很遗憾,程序已停止

    我在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 回复  |  直到 5 年前
        1
  •  2
  •   Pavneet_Singh    7 年前

    您有一个错误的签名来设置click listener,应该是

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

    From Docs

    在android:onClick属性中声明的方法必须具有如上所示的签名。具体而言,该方法必须:

    • 公开
    • 退货作废
    • 定义 View as its only parameter (这将是已单击的视图)
        2
  •  0
  •   compor    7 年前

    这是因为 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()方法。为此,您需要删除 onClick公司 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);
    }