-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)」... 非常感謝 –