我正在尝试将我的可绘制文件夹中的照片提取到 ImageView 数组中,并将该数组中的视图作为子元素添加到作为水平滚动视图的一部分的线性布局中。
但是,当我启动该应用程序时,它会意外停止。有谁知道我的代码是否有问题。谢谢你的帮助。
package com.example.myfirstapp;
import java.lang.reflect.Field;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.text.Layout;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class NewGallery extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_gallery);
LinearLayout gallery = (LinearLayout) findViewById(R.id.mygallery);
Field[] field = R.drawable.class.getFields();
final int arraylength = field.length;
ImageView[] images = new ImageView[arraylength];
for (int i=0; i<=arraylength; i=i+1)
{
images[i] = new ImageView(getApplicationContext());
images[i].setImageDrawable(getResources().getDrawable(getResources().getIdentifier("icon"+i, "drawable", getPackageName())));
gallery.addView(images[i]);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_new_gallery, menu);
return true;
}