我正在尝试将可绘制文件夹中的照片提取到ImageView数组中,并将数组中的视图作为子项添加到linearlayout,这是horizontalscrollview的一部分。
然而,当我启动应用程序时,它会意外停止。有谁知道我的代码是否有任何问题。感谢你的帮助。
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;
}
答案 0 :(得分:0)
如果您使用的是Eclipse,请参阅logcat错误消息。我认为你因为错误的转换而得到了ClassCastException:
LinearLayout gallery = (LinearLayout) findViewById(R.id.mygallery).
您确定要投射正确的视图吗?