4

in order to set ImageView dynamic i take an ImageView[] array. If i try to work on a ImageView from my array the activity crashes. Where is my mistake?

activity_main.xml:

<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"
tools:context=".MainActivity" >

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" >

    <ImageView
        android:id="@+id/iv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitStart" />

    <ImageView
        android:id="@+id/iv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitStart" />

    <ImageView
        android:id="@+id/iv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitStart" />
</TableRow>

</RelativeLayout>

MainActivity.java:

    package com.example.test;

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.ImageView;

    public class MainActivity extends Activity {

    private ImageView iv1, iv2, iv3;
    private ImageView[] IMGS = { iv1, iv3, iv3 };

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

    iv1 = (ImageView) findViewById(R.id.iv1);
    iv2 = (ImageView) findViewById(R.id.iv2);
    iv3 = (ImageView) findViewById(R.id.iv3);

    setImages();
}

private void setImages() {

    // A) this works
    iv1.setImageResource(R.drawable.ic_launcher);
    iv2.setImageResource(R.drawable.ic_launcher);
    iv3.setImageResource(R.drawable.ic_launcher);

    // B) at the beginning i need to set all images in a row
    for (ImageView img : IMGS) {
        img.setImageResource(R.drawable.ic_launcher);
    }

    // C) later on i need to set an particular image
    IMGS[1].setImageResource(R.drawable.ic_launcher);
}

    }

Look at setImages(). A works, B and C crashes my activity. Where is my mistake?

TIA Tobias

3
  • 1
    What is the error? (Logcat) Commented Feb 27, 2013 at 13:02
  • can you explain what you are trying to do with B? Commented Feb 27, 2013 at 13:04
  • Hi, sorry all, i have to use i.e. a List<ImageView> not an array. Sorry for my needless question. Should i delete this whole needless question, or not? Tobias Commented Feb 27, 2013 at 13:19

3 Answers 3

12

Your ImageView[] IMGS = { iv1, iv2, iv3 }; just contains 3 nulls elements at the moment that you are looping

try to add values in your IMGS by doing

IMGS[0] = iv1;
IMGS[1] = iv2;
IMGS[2] = iv3;
Sign up to request clarification or add additional context in comments.

Comments

8
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     iv1 = (ImageView) findViewById(R.id.iv1);
     iv2 = (ImageView) findViewById(R.id.iv2);
     iv3 = (ImageView) findViewById(R.id.iv3);

     IMGS[0] = iv1;
     IMGS[1] = iv2;
     IMGS[2] = iv3;

     setImages();
}

You never assign iv1, iv2 and iv3 to the array

Comments

6

use the following code, this works for me

ImageView[] IMGS= new ImageView[3];

IMGS[0]=findViewById(R.id.iv1);
IMGS[1]=findViewById(R.id.iv2);
IMGS[2]=findViewById(R.id.iv3);

Comments

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.