当我点击复选框时,我的应用程序崩溃似乎有问题。以前没有这个问题很奇怪。我有另一个工作区,在那里我试验了它并且工作正常。
我有一种感觉,因为这次复选框位于片段而不是活动中。我已经提供了一些代码来帮助。
SuikodenFragment.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ExpandableListView;
public class SuikodenFragment extends Fragment implements OnCheckedChangeListener{
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.suikoden_main_activity1, container, false);
// you can use findViewById() using the above 'view'
// get the listview
expListView = (ExpandableListView) view.findViewById(R.id.suikodenList1);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox_tick);
checkBox.setOnCheckedChangedListener(this);
return view;
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Riou");
listDataHeader.add("Shu");
listDataHeader.add("Humphrey");
listDataHeader.add("Kiba");
listDataHeader.add("Sierra");
// Adding child data
List<String> Riou = new ArrayList<String>();
Riou.add("Joins automatically at the start of the game.");
List<String> Shu = new ArrayList<String>();
Shu.add("Joins automatically.");
List<String> Humphrey = new ArrayList<String>();
Humphrey.add("Can be found in Highway Town. You must agree to help him find the missing child in the mountains.");
List<String> Kiba = new ArrayList<String>();
Kiba.add("After the battle with Kiba and his son, offer to spare his life and he will join.");
List<String> Sierra = new ArrayList<String>();
Sierra.add("Joins automatically.");
listDataChild.put(listDataHeader.get(0), Riou); // Header, Child data
listDataChild.put(listDataHeader.get(1), Shu);
listDataChild.put(listDataHeader.get(2), Humphrey);
listDataChild.put(listDataHeader.get(3), Kiba);
listDataChild.put(listDataHeader.get(4), Sierra);
}
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
// Check which checkbox was clicked
switch(buttonView.getId()) {
case R.id.checkbox_tick:
if (checked);
// Add the tick to the box
else
// Remove the tick in the box
break;
// TODO: Veggie sandwich
}
}
}
suikode_list_group1.xml
<TextView
android:id="@+id/lblListHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="26sp"
android:layout_marginLeft="50dp" />
<CheckBox android:id="@+id/checkbox_tick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-30dp"
android:layout_marginLeft="20dp"
android:focusable="false"
android:focusableInTouchMode="false" />
修改<!/强>
我已经实现了第一个答案中的建议。很抱歉要求更多帮助,但似乎我有两个错误。我已经使用第一个答案中建议的内容更新了这篇文章中的代码。
下面这一行给出了'未定义类型CheckBox'
checkBox.setOnCheckedChangedListener(this);
以下这一行给出了“无法解析为变量”的错误
if (checked);
答案 0 :(得分:2)
android:onClick="onClickListener"
没有名为onClickListener的方法。您应该尝试删除该行并实现检查侦听器,如此
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox_tick);
checkBox.setOnCheckedChangeListener(this);
让您的班级implement OnCheckedChangeListener
并实施方法
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (isChecked)
// Add the tick to the box
Log.d(TAG, "Tick the box");
else
// Remove the tick in the box
Log.d(TAG), "Untick the box");
}
我假设您已为自己的班级设置了日志标记,例如private final String TAG = getClass().getSimpleName();
- 编辑 -
抱歉错过了你正在做的事情,从我看来你可能需要改变
\\ expListView = (ExpandableListView) view.findViewById(R.id.suikodenList1);
\\ to
expListView = (ExpandableListView) view.findViewById(R.id.suikoden_list_group1);
然后改变
\\ CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox_tick);
\\ to
CheckBox checkBox = (CheckBox) expListView.findViewById(R.id.checkbox_tick);
原因是因为带有id checkbox_tick的复选框包含在suikoden_list_group1.xml而不是suikodenList1.xml中。但是,如果suikodenList1.xml确实是您要用于列表的布局,则需要将复选框移动到该布局中。