2

I have been trying to use getResources().getIdentifier() methods to assign ids to an array of images. However, when I consoled the array, I only got 2 valid elements and the rest was null. Could somebody please explain to me why after the onCreate method is executed, the program only consoles the values of the first 2 loops and returns null values afterward?

Currently: [ImageView0, ImageView1, null, null, null, null, null, null, null]

What should I do to make it fully loop through the array and return an array with valid values of [ImageView1, ImageView2, ImageView3, ImageView4, ImageView5, ImageView6, ImageView7, and ImageView8] instead ?

Many thanks! I am appreciated any help!

INPUT

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;    
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {
ImageView[] computerImages;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    computerImages = new ImageView[9];
    for (int i=0; i<computerImages.length; i++){
        String computerImageID = "ImageView"+i;
        int resID = getResources().getIdentifier(computerImageID,"id",getPackageName());
        computerImages[i] = findViewById(resID);
    } Log.i("ON CREATE", Arrays.toString(computerImages));
}

OUTPUT

I/ON CREATE: [android.support.v7.widget.AppCompatImageView{fe40fe0 VFED..C.. ......ID 0,0-0,0 #7f070003 app:id/ImageView0}, android.support.v7.widget.AppCompatImageView{8313299 VFED..C.. ......ID 0,0-0,0 #7f070004 app:id/ImageView1}, null, null, null, null, null, null, null]

XML LAYOUT

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button"
            android:layout_width="172dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:text="Computer" />

        <Button
            android:id="@+id/button2"
            android:layout_width="171dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:text="Player 02" />
    </LinearLayout>

    <GridLayout 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:id="@+id/gridLayout"
        android:layout_width="match_parent"
        android:layout_height="360dp"
        android:layout_alignParentRight="true"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/board"
        android:columnCount="3"
        android:rowCount="3">

        <ImageView
            android:id="@+id/imageView0"
            android:layout_width="95dp"
            android:layout_height="95dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="15dp"
            android:alpha="0"
            android:onClick="clickToPlay"
            android:tag="0" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="95dp"
            android:layout_height="95dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="0dp"
            android:layout_marginTop="15dp"
            android:alpha="0"
            android:onClick="clickToPlay"
            android:tag="1" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="95dp"
            android:layout_height="95dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="40dp"
            android:layout_marginTop="15dp"
            android:alpha="0"
            android:onClick="clickToPlay"
            android:tag="2" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="95dp"
            android:layout_height="95dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="16dp"
            android:alpha="0"
            android:onClick="clickToPlay"
            android:tag="3" />

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="95dp"
            android:layout_height="95dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="1dp"
            android:layout_marginTop="16dp"
            android:alpha="0"
            android:onClick="clickToPlay"
            android:tag="4" />

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="95dp"
            android:layout_height="95dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="40dp"
            android:layout_marginTop="16dp"
            android:alpha="0"
            android:onClick="clickToPlay"
            android:tag="5" />

        <ImageView
            android:id="@+id/imageView6"
            android:layout_width="95dp"
            android:layout_height="95dp"
            android:layout_marginBottom="0dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="16dp"
            android:alpha="0"
            android:onClick="clickToPlay"
            android:tag="6" />

        <ImageView
            android:id="@+id/imageView7"
            android:layout_width="95dp"
            android:layout_height="95dp"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="1dp"
            android:layout_marginTop="16dp"
            android:alpha="0"
            android:onClick="clickToPlay"
            android:tag="7" />

        <ImageView
            android:id="@+id/imageView8"
            android:layout_width="95dp"
            android:layout_height="95dp"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="40dp"
            android:layout_marginTop="16dp"
            android:alpha="0"
            android:onClick="clickToPlay"
            android:tag="8" />
    </GridLayout>

    <Button
        android:id="@+id/playAgainButton"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="14dp"
        android:onClick="clickReset"
        android:text="Play Again"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/winnerMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_weight="1"
        android:background="@android:color/holo_orange_dark"
        android:padding="5dp"
        android:text="TextView"
        android:textColor="@android:color/white"
        android:textSize="25sp"
        android:visibility="invisible" />
</RelativeLayout>
3
  • What are the contents of res directory of your application? Commented Jun 29, 2018 at 4:19
  • Thank you for your quick response! I have added the XML layout file for your view. The other folders in the res directory are just drawable with some photos, mipmap and values. Thanks again for your help! Commented Jun 29, 2018 at 4:28
  • UPDATE: Thanks for pointing it out! The Id I had did not match the ones in the resource file. I changed it from ImageView to imageView and it works now!! Thanks a lot for your help! Commented Jun 29, 2018 at 4:37

1 Answer 1

1

It returning null because "ImageView3, ImageView4, ImageView5, ImageView6, ImageView7, and ImageView8" views are not present in resource file. Only ImageView0, ImageView1 are present in resource file.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! I just realized that the ID string did not match the one I had in the res directory. I changed it to "imageView" and it works! Thank you for your time and help!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.