Java 为什么gridView中的onItemClick无法正常工作?
我正在做一个带有自定义适配器和gridview的小测验游戏 出于某种原因,当单击gridView的项时,视图、位置和id返回0或null 当尝试设置项目的背景颜色时,应用程序崩溃Java 为什么gridView中的onItemClick无法正常工作?,java,android,gridview,click,Java,Android,Gridview,Click,我正在做一个带有自定义适配器和gridview的小测验游戏 出于某种原因,当单击gridView的项时,视图、位置和id返回0或null 当尝试设置项目的背景颜色时,应用程序崩溃 Logcat将我链接到以下两行: gridRespuestas.performItemClick(gridRespuestas.getChildAt(0), 0, gridRespuestas.getItemIdAtPosition(0)); & 这是ridView的侦听器: gridRespuestas.se
Logcat将我链接到以下两行:
gridRespuestas.performItemClick(gridRespuestas.getChildAt(0), 0, gridRespuestas.getItemIdAtPosition(0));
&
这是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);
}
});
编辑:我包括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>
//Textview帕拉斯普雷蒙塔斯酒店
//格里德维尤酒店
更改您的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文件中不允许使用大写字母我明白了
我只是把听者里面的一切都包围了
if (view != null){
// Code to execute
}
您是否正确定义了GridRespuesta?你能告诉我你在哪里做的吗?@Coder123
final-GridView-gridRespuestas=findViewById(R.id.gridRespuestas);Adadador Adadador=新Adadador(本,响应);gridRespuestas.setAdapter(Adapter)代码>尝试这样强制转换,GridView gridRespuestas=(GridView)findViewById(R.id.gridRespuestas);你也可以发布gridRespuestas的相关xml部分吗?我编辑了这篇文章。我忘了说这种情况有时会发生,看起来好像是从id上取下盖子。。将其重命名为grid_respuestas并重试,不允许使用大写字母。。我不知道我是如何在检查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>
<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"/>
if (view != null){
// Code to execute
}