Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 单击项目时收到的AdapterView尚未';大小合适吗_Android_Android Listview_Onclicklistener_Android Adapterview - Fatal编程技术网

Android 单击项目时收到的AdapterView尚未';大小合适吗

Android 单击项目时收到的AdapterView尚未';大小合适吗,android,android-listview,onclicklistener,android-adapterview,Android,Android Listview,Onclicklistener,Android Adapterview,在这个活动中,我填充了一个ListView,并设置了一个onClick方法,当我们按下一个ListView项时会调用它。此retrocalled方法接收AdapterView,它的大小与实际列表的大小不同(AdapterView上通常不存在最后一个和第二个最后一个项目) 在每个ListView项的侧面都有一个单选按钮。对于这个单选按钮,我还有一个onClick方法,它在getView()函数中声明 当我按下ListView项时,我得到了这个错误(意味着该位置不存在子项)(我通过按下单选按钮而不是

在这个活动中,我填充了一个ListView,并设置了一个onClick方法,当我们按下一个ListView项时会调用它。此retrocalled方法接收AdapterView,它的大小与实际列表的大小不同(AdapterView上通常不存在最后一个和第二个最后一个项目)

在每个ListView项的侧面都有一个单选按钮。对于这个单选按钮,我还有一个onClick方法,它在getView()函数中声明

当我按下ListView项时,我得到了这个错误(意味着该位置不存在子项)(我通过按下单选按钮而不是该项得到了相同的错误):

(此行:adapterView.getChildAt(i).setBackgroundColor(Color.GRAY);)

公共类QuestActivity扩展活动{
private ListView lv=null;//它包含答案列表
私人字符串[]问题;
private ArrayList answerList=new ArrayList();
MyCustomAdapter dataAdapter=null;
已选择专用单选按钮;
最终处理程序=新处理程序(Looper.getMainLooper());
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_quest);
//接收前一个活动(RegisterActivity)中发送的参数
Bundle extras=getIntent().getExtras();
如果(附加值!=null){
问题=extras.getStringArray(“问题”);
}否则{
println(1,“QuestActivity.java,onCreate”,“Bundle recibido es nulo”);
}  
//加注该型号并为适配器充气
displayListView();
//运行侦听器以检测并回答通过按listView选择的问题
lv=(ListView)findViewById(R.id.answer\u列表);
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共无效onItemClick(AdapterView AdapterView、View视图、int i、long l){
adapterView.getChildAt(i).setBackgroundColor(颜色.灰色);
RadioButton rb=(RadioButton)视图.findViewById(R.id.checkBox\u答案);
rb.setChecked(真);
回答列表.get(i).setSelected(true);
如果(lastItemListSelected!=-1){
answerList.get(lastItemListSelected).setSelected(false);
adapterView.getChildAt(lastItemListSelected).setBackgroundColor(getResources().getColor(0x0106000d));
lastRBListSelected.setChecked(false);
}
lastItemListSelected=i;
lastRBListSelected=rb;
}
});
}   
/**
*填充模型并创建用课程数据填充listView的适配器
*/
私有void displayListView(){
//用名称填充模型

对于(int i=0;iReplace
adapterView.getChildAt(i)
by
view
非常感谢@SherifelKhatib修复了错误
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)
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 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>
<?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>