我正在尝试使用Android Studio创建大学GPA计算器应用。
主要是这个应用程序有两个EditText
视图,其中包括" credit"和"成绩"作为输入,当用户点击" Okay"时,将其显示在动态创建的TextView
中。按钮。
我已经定义了列表,用于存储用户在EditText
视图中输入的值。并调用方法来计算GPA,当"计算GPA"按下按钮。
问题是我的应用程序崩溃了,但代码或logcat中没有明显的错误。
这是XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.parul.gpacalculator.MainActivity"
android:id="@+id/linearLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_margin="2dp">
<EditText
android:id="@+id/txt_credits"
android:layout_width="0dp"
android:layout_height="44dp"
android:layout_weight="1"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Input Credits"
android:inputType="number"
android:textSize="22sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.505"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.048" />
<EditText
android:id="@+id/txt_grade"
android:layout_width="0dp"
android:layout_height="44dp"
android:layout_weight="1"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="@string/input_grade"
android:inputType="textPersonName"
android:maxLength="1"
android:textSize="22sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.188"
app:layout_constraintHorizontal_bias="0.505" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:padding="2dp"
android:layout_margin="2dp"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/button_okay"
android:layout_gravity="center_horizontal|center"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/calculate_gpa"
android:onClick="calcGpa"
android:layout_gravity="center_horizontal|center"/>
</LinearLayout>
</LinearLayout>`
logcat:View Logcat
这是java代码:
public class MainActivity extends AppCompatActivity {
private LinearLayout mLayout;
private EditText mEditCreditText;
private EditText mEditGradeText;
private Button mButton;
private int perSubjectPoint=0;
private int totalCredits=0;
List<EditText> allCredits = new ArrayList<EditText>();
List<EditText> allGrades = new ArrayList<EditText>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayout = (LinearLayout) findViewById(R.id.linearLayout);
mEditCreditText = (EditText) findViewById(R.id.txt_credits);
mEditGradeText = (EditText) findViewById(R.id.txt_grade);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(onClick());
TextView textView = new TextView(this);
}
private View.OnClickListener onClick() {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
mLayout.addView( createNewTextView(mEditCreditText.getText().toString(), mEditGradeText.getText().toString()));
allCredits.add(mEditCreditText);
allGrades.add(mEditGradeText);
}
};
}
private TextView createNewTextView(String text1,String text2) {
final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText("Credit: "+text1+ "\nGrade: "+ text2);
return textView;
}
public int pointPerSubj(int number,String grade) {
int point;
if(grade.equalsIgnoreCase("S"))
point = 10;
else if (grade.equalsIgnoreCase("A"))
point = 9;
else if (grade.equalsIgnoreCase("B"))
point = 8;
else if (grade.equalsIgnoreCase("C"))
point = 7;
else if (grade.equalsIgnoreCase("D"))
point = 6;
else if (grade.equalsIgnoreCase("E"))
point = 5;
else
point = 0;
int perSubjectPoint = number * point;
return perSubjectPoint;
}
public void calcGpa() {
int size = allCredits.size();
String[] stringsGrade = new String[size];
Integer[] intCredits = new Integer[size];
for(int i = 0; i < size; i++){
stringsGrade[i] = allGrades.get(i).getText().toString();
intCredits[i] = Integer.parseInt(allCredits.get(i).getText().toString());
totalCredits = totalCredits + intCredits[i];
perSubjectPoint = perSubjectPoint + pointPerSubj(intCredits[i], stringsGrade[i]);
}
float gpa = perSubjectPoint/totalCredits;
Toast.makeText(this,"Your GPA is: "+gpa, Toast.LENGTH_LONG);
}
}
答案 0 :(得分:0)
您的calcGpa()
方法用作onClick
属性 - 正确的签名为calcGpa(View v)
。如果你这样做,它将起作用