在这个活动中,我填充了一个ListView,并设置了一个onClick方法,当我们按下一个ListView项时,它会被调用。这种重新定位的方法接收的AdapterView与实际列表的大小不同(通常是最后一个和最后一个项目在AdapterView上不存在)。
在每个ListView项目的侧面,我有一个RadioButton。我还在getView()函数中声明了这个RadioButton的onClick方法。
当我按下ListView项目时,我收到此错误(意味着在该位置不存在孩子)(我通过按RadioButton而不是项目得到相同的错误):
02-16 11:54:05.312: E/AndroidRuntime(1175): FATAL EXCEPTION: main
02-16 11:54:05.312: E/AndroidRuntime(1175): Process: com.tfg.webquest, PID: 1175
02-16 11:54:05.312: E/AndroidRuntime(1175): java.lang.NullPointerException
02-16 11:54:05.312: E/AndroidRuntime(1175): at com.tfg.webquest.QuestActivity$1.onItemClick(QuestActivity.java:126)
(这一行:adapterView.getChildAt(i).setBackgroundColor(Color.GRAY);)
02-16 11:54:05.312: E/AndroidRuntime(1175): at android.widget.AdapterView.performItemClick(AdapterView.java:299)
02-16 11:54:05.312: E/AndroidRuntime(1175): at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
02-16 11:54:05.312: E/AndroidRuntime(1175): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
02-16 11:54:05.312: E/AndroidRuntime(1175): at android.widget.AbsListView$3.run(AbsListView.java:3638)
02-16 11:54:05.312: E/AndroidRuntime(1175): at android.os.Handler.handleCallback(Handler.java:733)
02-16 11:54:05.312: E/AndroidRuntime(1175): at android.os.Handler.dispatchMessage(Handler.java:95)
02-16 11:54:05.312: E/AndroidRuntime(1175): at android.os.Looper.loop(Looper.java:136)
02-16 11:54:05.312: E/AndroidRuntime(1175): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-16 11:54:05.312: E/AndroidRuntime(1175): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 11:54:05.312: E/AndroidRuntime(1175): at java.lang.reflect.Method.invoke(Method.java:515)
02-16 11:54:05.312: E/AndroidRuntime(1175): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-16 11:54:05.312: E/AndroidRuntime(1175): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-16 11:54:05.312: E/AndroidRuntime(1175): at dalvik.system.NativeStart.main(Native Method)
public class QuestActivity extends Activity {
private ListView lv=null; //It contains the answer list
private String[] question;
private ArrayList<AnswerModel> answerList = new ArrayList<AnswerModel>();
MyCustomAdapter dataAdapter = null;
private RadioButton lastRBListSelected;
final Handler handler = new Handler(Looper.getMainLooper());
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_quest);
//Receive the parameters sent in the Intent of the previous activity (RegisterActivity)
Bundle extras = getIntent().getExtras();
if (extras != null) {
question = extras.getStringArray("question");
}else{
Log.println(1,"QuestActivity.java, onCreate","Bundle recibido es nulo");
}
//Fill the model and inflate the adapter
displayListView();
//Run the listener for detecting and answer chosen by pressing the listView
lv = (ListView) findViewById(R.id.answer_list);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
adapterView.getChildAt(i).setBackgroundColor(Color.GRAY);
RadioButton rb = (RadioButton) view.findViewById(R.id.checkBox_answer);
rb.setChecked(true);
answerList.get(i).setSelected(true);
if(lastItemListSelected!=-1){
answerList.get(lastItemListSelected).setSelected(false);
adapterView.getChildAt(lastItemListSelected).setBackgroundColor(getResources().getColor(0x0106000d));
lastRBListSelected.setChecked(false);
}
lastItemListSelected=i;
lastRBListSelected=rb;
}
});
}
/**
* Fill the model and create the adapter that fills the listView with the course data
*/
private void displayListView() {
//Fill the Model with the names
for(int i=0;i<question.length;i++){
AnswerModel answerModel = new AnswerModel(question[i],false);
answerList.add(answerModel);
}
dataAdapter = new MyCustomAdapter(this,R.layout.answer,answerList);
lv = (ListView) findViewById(R.id.answer_list);
lv.setAdapter(dataAdapter);
}
private class MyCustomAdapter extends ArrayAdapter<AnswerModel> {
private LayoutInflater mLayoutInflater;
private int mResourceId=0;
private int mSelectedPosition=-1;
public MyCustomAdapter(Context context, int textViewResourceId, ArrayList<AnswerModel> answerList) {
super(context, textViewResourceId, answerList);
mResourceId=textViewResourceId;
mLayoutInflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
private class ViewHolder {
TextView name;
RadioButton selected;
}
/**
* It is retrocalled one time for each element of the list
*/
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
View view = convertView;
//Is the first time that getView is called
if (view == null) {
view = mLayoutInflater.inflate(mResourceId,parent,false);
holder = new ViewHolder();
holder.selected = (RadioButton) view.findViewById(R.id.checkBox_answer);
holder.name = (TextView) view.findViewById(R.id.code_answer);
view.setTag(holder);
holder.selected.setChecked(false);
holder.selected.setTag(position);
//Listener called when an answer is selected
holder.selected.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioButton rb;
rb = (RadioButton) v.findViewById(R.id.checkBox_answer);
//Second, third... time we select a RB
if(position != mSelectedPosition && lastRBListSelected != null){
lv.getChildAt(lastItemListSelected).setBackgroundColor(getResources().getColor(0x0106000d));
lastRBListSelected.setChecked(false);
answerList.get(lastItemListSelected).setSelected(false);
answerList.get(position).setSelected(true);
lv.getChildAt(position).setBackgroundColor(Color.GRAY);
rb.setChecked(true);
}else{ //First time we select a RB
rb = (RadioButton) v.findViewById(R.id.checkBox_answer);
rb.setChecked(true);
answerList.get(position).setSelected(true);
((ListView)findViewById(R.id.answer_list)).getChildAt(position).setBackgroundColor(Color.GRAY);
}
mSelectedPosition = position;
lastItemListSelected=position;
lastRBListSelected=rb;
}
});
} else {
holder = (ViewHolder) view.getTag();
}
//Set the values from the list into the adapter
AnswerModel answerModel = answerList.get(position);
holder.name.setText(answerModel.getName());
holder.selected.setChecked(answerModel.isSelected());//
holder.selected.setTag(answerModel);//
return view;
}
}
}
这是XML活动文件(fragment_quest.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:background="@drawable/webquest_background"
tools:context="com.tfg.webquest.MainActivity$PlaceholderFragment"
android:weightSum="23" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2">
<TextView android:id="@+id/countdown_quest"
android:padding="5sp"
android:textSize="23sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/main_center13"
style="@style/entry_style"/>
</RelativeLayout>
<RelativeLayout
android:layout_weight="18"
android:layout_width="wrap_content"
android:layout_height="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="5"
android:orientation="vertical">
<RelativeLayout
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbarStyle="outsideInset" >
<TextView android:id="@+id/quest_question_text"
android:padding="5sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rectangle_quest"
style="@style/entry_style"/>
</ScrollView>
</RelativeLayout>
</LinearLayout>
每个ListView项目(answer.xml)的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dip" >
<RadioButton
android:id="@+id/checkBox_answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="onRadioButtonClicked"/>
<TextView
android:id="@+id/code_answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="false"
android:maxLines="10"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/checkBox_answer" />
</RelativeLayout>
感谢您的时间。
(某些元素已被简化,可能是所显示代码的一些未定义的引用)