不幸的是,程序已停止
Unfortunately, program has stopped
很遗憾,程序已停止
我试图在 android 工作室计算年龄。它没有显示任何错误消息,但是当我单击“按钮”时,应用程序崩溃并显示 “不幸的是,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>
您设置点击侦听器的签名有误,应该是
public void hi(View v){//...code}
// ^^^^^^
您在 android:onClick 属性中声明的方法必须具有如上所示的签名。具体来说,该方法必须:
- 成为public
- Return无效
- 定义一个
View as its only parameter
(这将是被点击的视图)
这是因为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);
}
很遗憾,程序已停止
我试图在 android 工作室计算年龄。它没有显示任何错误消息,但是当我单击“按钮”时,应用程序崩溃并显示 “不幸的是,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>
您设置点击侦听器的签名有误,应该是
public void hi(View v){//...code}
// ^^^^^^
您在 android:onClick 属性中声明的方法必须具有如上所示的签名。具体来说,该方法必须:
- 成为public
- Return无效
- 定义一个
View as its only parameter
(这将是被点击的视图)
这是因为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);
}