首页 > 解决方案 > 为什么来自 gridView 的 onItemClick 无法正常工作?

问题描述

我正在做一个带有自定义适配器和网格视图的小测验游戏。

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

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


Logcat 将我链接到以下两行:

gridRespuestas.performItemClick(gridRespuestas.getChildAt(0), 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;
    }


}

日志猫:

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>

标签: javaandroidgridviewclick

解决方案


更改您的身份证:

   <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 文件中不允许使用大写字母


推荐阅读