Leila R
Leila R

Reputation: 11

Error on set ImageView resource in android

I am new to Android. I am working on an app. It has a 3*3 grid layout with an image view in each cell. I want to set resource of images randomly from an array of drawables. I used .setImageResource() for each one but when I run the app it crashes.

Here's the error message from log cat.

And here is my XML and java code.

activity_game.xml

<GridLayout 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"
    tools:context="com.example.leila.makesquare.GameActivity"
    android:rowCount="3"
    android:columnCount="3">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="110dp"
    android:layout_height="110dp"
    android:layout_row="0"
    android:layout_column="0"
    android:gravity="fill"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="100dp">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView0"/>

</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="110dp"
    android:layout_height="110dp"
    android:layout_row="0"
    android:layout_column="1"
    android:gravity="fill"
    android:layout_marginTop="100dp">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView1" />
</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="110dp"
    android:layout_height="110dp"
    android:layout_row="0"
    android:layout_column="2"
    android:gravity="fill"
    android:layout_marginTop="100dp">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView2" />
</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="110dp"
    android:layout_height="110dp"
    android:layout_row="1"
    android:layout_column="0"
    android:gravity="fill"
    android:layout_marginLeft="20dp">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView3" />
</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="110dp"
    android:layout_height="110dp"
    android:layout_row="1"
    android:layout_column="1"
    android:gravity="fill">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView4" />
</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="110dp"
    android:layout_height="110dp"
    android:layout_row="1"
    android:layout_column="2"
    android:gravity="fill">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView5" />
</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="110dp"
    android:layout_height="110dp"
    android:layout_row="2"
    android:layout_column="0"
    android:gravity="fill"
    android:layout_marginLeft="20dp">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView6" />
</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="110dp"
    android:layout_height="110dp"
    android:layout_row="2"
    android:layout_column="1"
    android:gravity="fill">

          <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView7" />
</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="110dp"
    android:layout_height="110dp"
    android:layout_row="2"
    android:layout_column="2"
    android:gravity="fill">

         <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView8" />
    </LinearLayout>
</GridLayout>

GameActivity.java

package com.example.leila.makesquare;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class GameActivity extends ActionBarActivity {

    int[] squares=new int[]
    { 
            R.drawable.brgy,
            R.drawable.bygr,
            R.drawable.bygr2,
            R.drawable.gbry,
            R.drawable.gyrb,
            R.drawable.gyrb2,
            R.drawable.rbyg,
            R.drawable.rgby,
            R.drawable.rybg
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        ArrayList<Integer> jj=new ArrayList<Integer>();
        for (int i=0;i<9;i++) {
            jj.add(i);
        }

        Collections.shuffle(jj);

        ImageView imageView0=(ImageView)findViewById(R.id.imageView0);
        ImageView imageView1=(ImageView)findViewById(R.id.imageView1);
        ImageView imageView2=(ImageView)findViewById(R.id.imageView2);
        ImageView imageView3=(ImageView)findViewById(R.id.imageView3);
        ImageView imageView4=(ImageView)findViewById(R.id.imageView4);
        ImageView imageView5=(ImageView)findViewById(R.id.imageView5);
        ImageView imageView6=(ImageView)findViewById(R.id.imageView6);
        ImageView imageView7=(ImageView)findViewById(R.id.imageView7);
        ImageView imageView8=(ImageView)findViewById(R.id.imageView8);


        int q0=squares[jj.get(0)];
        int q1=squares[jj.get(1)];
        int q2=squares[jj.get(2)];
        int q3=squares[jj.get(3)];
        int q4=squares[jj.get(4)];
        int q5=squares[jj.get(5)];
        int q6=squares[jj.get(6)];
        int q7=squares[jj.get(7)];
        int q8=squares[jj.get(8)];

        imageView0.setImageResource(q0);
        imageView1.setImageResource(q1);
        imageView2.setImageResource(q2);
        imageView3.setImageResource(q3);
        imageView4.setImageResource(q4);
        imageView5.setImageResource(q5);
        imageView6.setImageResource(q6);
        imageView7.setImageResource(q7);
        imageView8.setImageResource(q8);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_game, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Upvotes: 1

Views: 1389

Answers (3)

Marcel Hofgesang
Marcel Hofgesang

Reputation: 1040

You forgot a closing angle bracket at your 5th linear layout:

<LinearLayout
android:orientation="vertical"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_row="1"
android:layout_column="1"
android:gravity="fill"

Upvotes: 0

Dana
Dana

Reputation: 85

Your ImageView tag isn't closed. Try

    <ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/imageView0"/>

And try giving us the logs

[EDIT] As from the edit you Activity code is very bad, try reading more about GridViews and the coding practice in Java and Android to improve your code and coding skills.

Upvotes: 1

Mohd Saquib
Mohd Saquib

Reputation: 590

Here You have not close ImageView closing bracket i.e. "/>"
<LinearLayout
    android:orientation="vertical"
    android:layout_width="110dp"
    android:layout_height="110dp"
    android:layout_row="0"
    android:layout_column="0"
    android:gravity="fill"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="100dp">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView0"

</LinearLayout>
And here you have not close Linear Layout closing bracket i.e. "/>"
<LinearLayout
    android:orientation="vertical"
    android:layout_width="110dp"
    android:layout_height="110dp"
    android:layout_row="1"
    android:layout_column="1"
    android:gravity="fill"

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView4" />
</LinearLayout>

Upvotes: 1

Related Questions