为什么gridView的onItemClick无法正常工作?

我正在做一个带有自定义适配器和gridview的小型问答游戏。

由于某种原因,当单击gridView的项目时,视图,位置和id返回0或null。

尝试设置项目的背景色时,应用程序崩溃。


Logcat将我链接到这两行:

gridRespuestas.performItemClick(gridRespuestas.getchildAt(0),gridRespuestas.getItemIdAtPosition(0));

LinearLayout respuesta = (LinearLayout) view;
respuesta.getchildAt(0).setBackgroundColor(Color.parseColor("#5e3535"));

这里是ridView的侦听器:

 gridRespuestas.setOnItemClicklistener(new AdapterView.OnItemClicklistener() {
            @Override
            public void onItemClick(AdapterView<?> parent,final View view,int position,long id) {

                contador.cancel();

                gridRespuestas.setEnabled(false);
                LinearLayout respuesta = (LinearLayout) view;

                // Si la respuesta seleccionada es correcta
                boolean repuestaCorrecta = preguntasFiltradas.get(preguntamostrada).getRespuestas().get(position).isEsCorrecta();

                if(repuestaCorrecta ){

                    // Pone el fondo de la respuesta en verde
                    respuesta.getchildAt(0).setBackgroundColor(Color.parseColor("#355e4e"));
                }
                // Si es incorrecta
                else{
                    // Pone el fondo de la respuesta pulsada en rojo
                    respuesta.getchildAt(0).setBackgroundColor(Color.parseColor("#5e3535"));

                    // Por cada view dentro de la gridview
                    GridView grid = (GridView) parent;
                    for (int i = 0 ; i < grid.getchildCount() ; i++){

                        // Coge cada elemento dentro de parent > LinearLayour > Textview
                        TextView text = (TextView)((LinearLayout)grid.getchildAt(i)).getchildAt(0);

                        // Si la pregunta es correcta
                        if(preguntasFiltradas.get(preguntamostrada).getRespuestas().get(i).isEsCorrecta()){

                            // Cambia el fondo de color verde
                            text.setBackgroundColor(Color.parseColor("#355e4e"));
                        }
                    }
                }

                juego(position,juegoLayout,gridRespuestas,contador);

            }
        });

适配器:

public class Adaptador extends BaseAdapter {


    private List<Respuesta> respuestas;
    private activity context;

    public Adaptador(activity _context,List<Respuesta> _respuestas) {

        context = _context;
        respuestas = _respuestas;
    }


    public View getView(int position,View convertView,ViewGroup parent) {

        View view = convertView;
        if (view == null){
            LayoutInflater li = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INflaTER_SERVICE);
            view = li.inflate(R.layout.place_respuesta,null);
        }

        final TextView respuesta = view.findViewById(R.id.placeRespuesta);
        respuesta.setText(respuestas.get(position).getRespuesta());

        return view;
    }


    @Override
    public int getcount() {
        int retorno = 0;

        if(respuestas != null)
            retorno = respuestas.size();

        return retorno;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


}

Logcat:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.LinearLayout.getchildAt(int)' on a null object reference
        at com.example.joc.Juego$3.onItemClick(Juego.java:136)
        at android.widget.AdapterView.performItemClick(AdapterView.java:310)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1156)
        at com.example.joc.Juego$1.onFinish(Juego.java:82)

编辑:我包括XML文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/grisOscuro"
    android:id="@+id/juegoLayout">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/joc"/>

    <ImageButton
        android:id="@id/inicio"
        android:layout_width="220dp"
        android:layout_height="220dp"
        android:layout_margin="7dp"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="#00000000"
        android:scaleType="fitCenter"
        android:src="@drawable/home" />

    //Textview para las preguntas
    <TextView
        android:id="@+id/pregunta"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textSize="50sp"
        android:textColor="@color/Blanco"
        android:layout_marginTop="365dp"/>

    //GridView para las respuestas
    <GridView
        android:id="@+id/gridRespuestas"
        android:layout_width="match_parent"
        android:layout_height="780dp"
        android:numColumns="2"
        android:layout_alignParentBottom="true"
        android:stretchMode="columnWidth"
        android:gravity="center"/>

    <TextView
        android:id="@+id/tiempo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:textSize="150sp"
        android:layout_marginTop="50dp"
        android:textAlignment="center"
        android:text="30"
        android:textColor="@color/Blanco"/>
</RelativeLayout>
fgtfer 回答:为什么gridView的onItemClick无法正常工作?

更改您的ID:

   <GridView
        android:id="@+id/grid_respuestas"
        android:layout_width="match_parent"
        android:layout_height="780dp"
        android:numColumns="2"
        android:layout_alignParentBottom="true"
        android:stretchMode="columnWidth"
        android:gravity="center"/>

因为Android xml文件中不允许使用大写字母

,

我明白了。

我只是用

包围了OnItemClickListener中的所有内容
if (view != null){
// Code to execute
}
本文链接:https://www.f2er.com/3064345.html

大家都在问