0
我正在爲用戶創建一個設置選項卡,並使用CheckBox進行檢查。這是代碼:無法在選項卡上使用onClick?
public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.Lang_Vie:
if (checked){
mCBLangEng.setChecked(false);
Constants.STR_LANG = "vi";
setLocale("vi");
}
break;
case R.id.Lang_Eng:
if (checked){
mCBLangViet.setChecked(false);
Constants.STR_LANG = "en";
setLocale("en");
}
case R.id.locHanoi:
if (checked){
mCBLocDN.setChecked(false);
Constants.STR_LOC = "hn";
Constants.API_TYPE_AQI = 1;
Constants.API_TYPE_TMP = 2;
Constants.API_TYPE_HUM = 3;
}
case R.id.locDanang:
if (checked){
mCBLocHN.setChecked(false);
Constants.STR_LOC = "dn";
Constants.API_TYPE_AQI = 7;
Constants.API_TYPE_TMP = 8;
Constants.API_TYPE_HUM = 9;
}
break;
case R.id.SensCheck:
if (checked){
Constants.STR_SENS = "yes";
Constants.USER_LIMITATION = 200;
}
else{
Constants.STR_SENS = "no";
Constants.USER_LIMITATION = 150;
}
break;
}
}
而且我在SettingTab使用此代碼:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.view_setting, container, false);
Sens = (CheckBox) view.findViewById(R.id.SensCheck);
mCBLangEng = (CheckBox) view.findViewById(R.id.Lang_Eng);
mCBLangViet = (CheckBox) view.findViewById(R.id.Lang_Vie);
mCBLocHN = (CheckBox) view.findViewById(R.id.locHanoi);
mCBLocDN = (CheckBox) view.findViewById(R.id.locDanang);
Save = (Button) view.findViewById(R.id.save);
onCheckboxClicked(view);
return view;
但我有此錯誤:
11-14 15:50:24.574 16794-16794/com.journaldev.viewpager E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.CheckBox
at com.journaldev.viewpager.MyApplication.MyApp.TabSetting.onCheckboxClicked(TabSetting.java:155)
at com.journaldev.viewpager.MyApplication.MyApp.TabSetting.onCreateView(TabSetting.java:140)
也許有什麼不對?我試圖用this.getActivity()替換視圖,但它不起作用。
你能幫助我嗎?謝謝 !。
這是我的佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Setting"
android:textSize="35dp"
android:textColor="#faf7f7"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Language"
android:textSize="30sp"
android:textColor="#faf7f7"
android:layout_marginTop="20dp"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="English"
android:textSize="20sp"
android:textColor="#faf7f7"
android:checked="false"
android:id="@+id/Lang_Eng"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Tiếng Việt"
android:textSize="20sp"
android:textColor="#faf7f7"
android:checked="true"
android:id="@+id/Lang_Vie"
android:onClick="onCheckboxClicked"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Location"
android:textSize="30sp"
android:textColor="#faf7f7"
android:layout_marginTop="20dp"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/Danang"
android:textSize="20sp"
android:textColor="#faf7f7"
android:checked="false"
android:id="@+id/locDanang"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/Hanoi"
android:textSize="20sp"
android:textColor="#faf7f7"
android:checked="true"
android:id="@+id/locHanoi"
android:onClick="onCheckboxClicked"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/User"
android:textSize="30sp"
android:textColor="#faf7f7"
android:layout_marginTop="20dp"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false"
android:textSize="20sp"
android:textColor="#faf7f7"
android:text="@string/Sensitivegroup"
android:id="@+id/SensCheck"
android:onClick="onCheckboxClicked"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginTop="10dp"
android:textStyle="italic"
android:textColor="#faf7f7"
android:text="@string/Note"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginTop="10dp"
android:textStyle="italic"
android:textColor="#faf7f7"
android:text="@string/Example"/>
</LinearLayout>
</ScrollView>
<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#bfbebe"
android:text="SAVE SETTING"
android:textColor="#000000"
android:id="@+id/save"/>
</LinearLayout>
顯示您view_setting佈局文件 –
請張貼您的XML佈局。 –
我看到你有ClassCastException,請檢查Exception ClassCastException:android.widget.LinearLayout不能轉換爲android.widget.CheckBox,你嘗試將LinearLayout轉換爲Checkbox。檢查你的layout.xml文件。 – katmanco