0

描画可能なフォルダーから ImageView 配列に写真を抽出し、配列からのビューを子として、水平スクロール ビューの一部である linearlayout に追加しようとしています。

しかし、アプリを起動すると予期せず停止します。私のコードに問題があるかどうかは誰にもわかりません。助けてくれてありがとう。

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;
}
4

1 に答える 1

0

Eclipse を使用している場合は、logcat エラー メッセージを参照してください。間違ったキャストが原因で ClassCastException が発生したと思います:

LinearLayout gallery = (LinearLayout) findViewById(R.id.mygallery). 

正しいビューをキャストしていますか?

于 2013-04-03T08:13:00.190 に答える