skip to Main Content

I’m making a small card game at the moment and for some reason anytime I try to open my second Activity, it crashes. With the error being the one mentioned in the title.

I tried various ways and unfortunately it always gives me this error.

This is my latest code:

package com.example.juegocartas3;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Random;

public class Adapter extends ArrayAdapter {

    ArrayList<Cartas> barajaCartas = new ArrayList<>();
    Random random;
    Iterator itr = barajaCartas.iterator();
    public Adapter(@NonNull Context context, int idLayout, @NonNull ArrayList<Cartas> objects) {
        super(context, idLayout, objects);
        barajaCartas = objects;
    }

    @Override
    public int getCount() {
        return super.getCount();
    }

    @SuppressLint("InflateParams")
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        View v = convertView;
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.grid_view_items, null);
        ImageButton carta = v.findViewById(R.id.imageView);
        carta.setImageResource(barajaCartas.get(position).getImagenCarta());
        carta.setTag(barajaCartas.get(position).getPuntaje());

        return v;
    }
}

And it says the error is in this line:

v = inflater.inflate(R.layout.grid_view_items, null);

And it doesn’t make sense to me.

This are the rest of my classes that have to do with the Adapter

package com.example.juegocartas3;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ClipData;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class MainActivity2 extends AppCompatActivity {

    GridView tablero;
    ArrayList<Cartas> barajaCartas = new ArrayList<>();

    /*ArrayList<Integer> myImageList = new ArrayList<>();*/

    Random random;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main2);

        tablero = (GridView) findViewById(R.id.contenedorGrid);
        barajaCartas.add(new Cartas(this,R.drawable.asdecopas,1));
        barajaCartas.add(new Cartas(this,R.drawable.asdeespadas,1));
        barajaCartas.add(new Cartas(this,R.drawable.caballodebastos,11));
        barajaCartas.add(new Cartas(this,R.drawable.caballodeespadas,11));
        barajaCartas.add(new Cartas(this,R.drawable.dosdecopas,2));
        barajaCartas.add(new Cartas(this,R.drawable.reydecopas,12));
        barajaCartas.add(new Cartas(this,R.drawable.sotadeespadas,10));
        barajaCartas.add(new Cartas(this,R.drawable.sotadeoros,0));
        barajaCartas.add(new Cartas(this,R.drawable.tresbastos,3));

        Adapter myAdapter = new Adapter (this,R.layout.grid_view_items,barajaCartas);
        tablero.setAdapter(myAdapter);

        Collections.shuffle(barajaCartas);

    }

    public void onClickCarta(View v) {

    }

}

My class of cards:

package com.example.juegocartas3;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageButton;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;

@SuppressLint("AppCompatCustomView")
public class Cartas extends ImageButton {

    @DrawableRes
    int imagenCarta;
    int puntaje;

    public Cartas(Context context,@DrawableRes int imagenCarta, int puntaje) {
        super(context);
        this.imagenCarta = imagenCarta;
        this.puntaje=puntaje;

    }
    @DrawableRes
    public int getImagenCarta(){
        return imagenCarta;
    }

    public void setImagenCarta() {
        this.imagenCarta = imagenCarta;
    }

    public int getPuntaje() {
        return puntaje;
    }

    public void setPuntaje(int puntaje) {
        this.puntaje = puntaje;
    }
}

And the two xml (the second one being the one I wish to stick on the first):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2"
    android:id="@+id/contenedor">

    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="10sp"
        android:layout_marginLeft="10sp"
        android:layout_marginTop="150dp"
        android:layout_marginEnd="10sp"
        android:layout_marginRight="10sp"
        android:layout_marginBottom="10sp"
        android:numColumns="3"
        android:id="@+id/contenedorGrid"
        android:horizontalSpacing="10dp"
        tools:ignore="UselessParent">
    </GridView>

</RelativeLayout>

The second one:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/grid_view_items">

    <ImageButton
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView"
        android:src="@drawable/trasera"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:onClick="onClickCarta"
        />

</RelativeLayout>

Maybe it’s too much to ask but is something wrong with any of these classes?
Or should I do things a different way?

Edit: This is the error text:

2022-05-25 18:24:23.107 2988-2988/com.example.juegocartas3
E/AndroidRuntime: FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #8: Error inflating class ImageButton
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.example.juegocartas3.Adapter.getView(Adapter.java:41)
at android.widget.AbsListView.obtainView(AbsListView.java:2271)
at android.widget.GridView.onMeasure(GridView.java:1030)
at android.view.View.measure(View.java:15172)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:145)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
at androidx.appcompat.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:496)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15172)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
at android.view.View.measure(View.java:15172)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path):
TypedValue{t=0x1/d=0x7f0700a5 a=-1 r=0x7f0700a5}
at android.content.res.Resources.loadDrawable(Resources.java:1897)
at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
at android.widget.ImageView.(ImageView.java:120)
at android.widget.ImageButton.(ImageButton.java:87)
at androidx.appcompat.widget.AppCompatImageButton.(AppCompatImageButton.java:78)
at androidx.appcompat.widget.AppCompatImageButton.(AppCompatImageButton.java:73)
at androidx.appcompat.app.AppCompatViewInflater.createImageButton(AppCompatViewInflater.java:226)
at androidx.appcompat.app.AppCompatViewInflater.create

And this line is the one I was referring to earlier:

at com.example.juegocartas3.Adapter.getView(Adapter.java:41)

v = inflater.inflate(R.layout.grid_view_items, null);

2

Answers


  1. try replacing

    View v = convertView;
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.grid_view_items, null);
    

    with

    View v = getApplicationContext().getLayoutInflater().inflate(R.layout.grid_view_items, null);
    
    Login or Signup to reply.
  2. It is likely that you do not have your drawable files for your cards in the proper drawable folder based on the API level you are running. For example, if the files are in /drawable-26, when you run your app on API 25 that would be the error you would get.

    If the drawables for your cards are not specific to an API level, move them to the /drawable folder.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search