-1
它似乎运行良好,但我只是想知道为什么它显示错误时,我尝试实施类型转换。为什么在示例中的类型转换很重要?
例如,当ratingBar作为(RatingBar)进行类型转换时,以及editText作为(EditText)进行类型转换时。
有什么建议吗?指针?在你的XML声明
package com.example.benmohammad.contacts;
import android.app.Activity;
import android.widget.EditText;
import android.widget.RatingBar;
class StudentFormViewHelper {
private final Activity activity;
public StudentFormViewHelper(Activity activity) {
this.activity = activity;
}
private String getName() {
return getTextFieldValue(R.id.student_form_name);
}
private String getTextFieldValue(int fieldId) {
EditText field = activity.findViewById(fieldId);
String value = field.getText().toString();
return value;
}
private String getAddress() {
return getTextFieldValue(R.id.student_form_address);
}
private String getPhoneNumber() {
return getTextFieldValue(R.id.student_form_phonenumber);
}
private String getWebsite() {
return getTextFieldValue(R.id.student_form_website);
}
private String getEmail() {
return getTextFieldValue(R.id.student_form_email);
}
public Student createAStudent() {
return new Student(getName(), getAddress(), getPhoneNumber(), getWebsite(), getEmail(), getGrading());
}
private double getGrading() {
RatingBar rating = activity.findViewById(R.id.student_form_grading);
return rating.getRating();
}
}
你会得到什么错误? – Srijith
你在想什么,在哪里? – Juan
它运行没有类型转换,这是我不明白 例如。 private double getGrading(){ RatingBar rating = activity.findViewById(R.id.student_form_grading); 看到,我还没有在activity.findViewId之前类型强制转换,但如果我这样做...... 私人双getGrading(){ 的RatingBar等级=(的RatingBar)activity.findViewById ......这夹头下面的语法错误typecast“(RatingBar)”... 非常感谢 –