LayoutInflater を使用して、配列リストからボタンを膨らませようとしています。基本的に、3 つの配列リストがあります。1 つはボタン テキスト用、もう 1 つはボタン イメージ用、もう 1 つはクリックしたときに移動するクラス ファイル用です。ボタンを膨らませるとテキストと画像が機能しますが、クリックしてもクラスファイルを切り替えることができません。解決策を探してみましたが、自分のニーズに合ったものが見つかりませんでした。どんな助けでも本当に感謝しています。
これが私のコードです:
ViewGroup parent;
LayoutInflater inflater;
TextView bText;
ImageView bImage;
parent = (ViewGroup) findViewById(R.id.container);
inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
String[] text = {"News","Chat"};
String[] image = {"news","chat"};
String[] link = {"Main","Chat"};
int size = text.length;
for(int i = 0; i < size; i++) {
View view = inflater.inflate(R.drawable.button, null);
int imageID = getResources().getIdentifier(image[i], "drawable", getPackageName());
bImage = (ImageView) view.findViewById(R.id.ihome);
bImage.setImageResource(imageID);
bText = (TextView) view.findViewById(R.id.thome);
bText.setText(text[i]);
parent.addView(view);
final String theClass = link[i];
bText.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent blink = new Intent();
blink.setClassName(getApplicationContext(), theClass);
startActivity(blink);
}
});
}
編集:ボタンのいずれかをクリックすると、ボタンが見つからないと言ってクラッシュし、それが AndroidManifest.xml で宣言されているかどうかを確認するために明確にします。