Android 如何设置ImageView[]数组?

Android 如何设置ImageView[]数组?,android,arrays,imageview,Android,Arrays,Imageview,为了设置ImageView动态,我使用ImageView[]数组。如果我尝试使用阵列中的ImageView,活动将崩溃。我的错在哪里 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" and

为了设置ImageView动态,我使用ImageView[]数组。如果我尝试使用阵列中的ImageView,活动将崩溃。我的错在哪里

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>
请看setImages()。A工作,B和C使我的活动崩溃。我的错在哪里

短暂性脑缺血发作 托拜厄斯


您从未将iv1、iv2和iv3分配给数组

您的
ImageView[]IMGS={iv1、iv2、iv3}在循环时只包含3个null元素

尝试通过以下操作在IMG中添加值

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

使用下面的代码,这对我有用

ImageView[] IMGS= new ImageView[3];

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

错误是什么?(Logcat)你能解释一下你想用B做什么吗?嗨,对不起,我必须使用列表而不是数组。对不起,我提出了不必要的问题。我是否应该删除这个不必要的问题?托拜厄斯
IMGS[0] = iv1;
IMGS[1] = iv2;
IMGS[2] = iv3;
ImageView[] IMGS= new ImageView[3];

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