【发布时间】:2017-03-18 10:29:00
【问题描述】:
我创建了一个复选框列表,并尝试根据所选复选框的数量按比例增加一个 int 值。相反,每次我尝试检查它们(通过 .isChecked())时,应用程序都会崩溃。我试图到处搜索一些解决方案,但没有任何效果。我真的不明白问题出在哪里。
这是代码
XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.example.deadlybanana.myfirstapp.DisplayPicture">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:layout_marginRight="0dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="0dp">
<LinearLayout
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/unicorn" />
<TextView
android:id="@+id/frasenome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Are you Beatiful?" />
<CheckBox
android:id="@+id/checkbox2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Are you Fabolous?" />
<CheckBox
android:id="@+id/checkbox3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Are you Unique?" />
<CheckBox
android:id="@+id/checkbox4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Are you a horse?" />
<CheckBox
android:id="@+id/checkbox5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Do you eat grass?" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="checkbutton"
android:text="Check if you are a unicorn" />
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
Java 文件
package com.example.deadlybanana.myfirstapp;
public class DisplayPicture extends AppCompatActivity {
List<Boolean> checkboxes = new ArrayList<Boolean>();
CheckBox press1;
CheckBox press2;
CheckBox press3;
CheckBox press4;
CheckBox press5;
int points = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_picture);
Intent intent = getIntent();
String name = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
CheckBox press1 = (CheckBox)findViewById(R.id.checkbox1);
CheckBox press2 = (CheckBox)findViewById(R.id.checkbox2);
CheckBox press3 = (CheckBox)findViewById(R.id.checkbox3);
CheckBox press4 = (CheckBox)findViewById(R.id.checkbox4);
CheckBox press5 = (CheckBox)findViewById(R.id.checkbox5);
TextView NameString = (TextView) findViewById(R.id.frasenome);
String phrase = name + ", are you some of these things?";
NameString.setText(phrase);
}
public void checkbutton(View view){
if (press1.isChecked()){
points += 4;
}
if (points > 3){
Toast.makeText(this, "You are a unicorn", Toast.LENGTH_LONG).show();
} else{
Toast.makeText(this, "You are not a unicorn", Toast.LENGTH_LONG).show();
}
}
}
(现在只检查第一个复选框以简化代码)
【问题讨论】:
标签: java android xml android-layout