実行時にimageButtonの画像を更新しようとしています。私は別のアクティビティから渡されたIDをチェックするswitch文を持っています。私は、正しいIDがTextViewに渡されるので、switch文が動作していることを知っています。実行時にimageButtonで画像を変更する際に問題が発生しました
ImageViewを使用していて、ImageButtonを使用している例がいくつかあります。あなたが下に見ることができるように、私は両方を試してみたことがあります。
XMLレイアウト:
<ImageButton android:visibility="gone" android:id="@+id/imageButton" android:src="@drawable/defaultimage" android:layout_width="97dp" android:layout_height="95dp"></ImageButton>
<TextView android:visibility="gone" android:text="TextView" android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true"></TextView>
Javaコード:
は、それが動作するようになった
case 1:{
// Location 1
ImageView ImageButton = (ImageView)findViewById(R.id.imageButton);
ImageButton .setImageResource(R.drawable.image1);
ImageButton .setVisibility(0);
TextView Test = (TextView)findViewById(R.id.textView);
Test.setVisibility(0);
Test.setText("ID passed is" + id);
break;
}
case 2:{
// Location 2
ImageButton ImageButton = (ImageButton)findViewById(R.id.imageButtonGhostCamLocation);
ImageButton .setBackgroundResource(R.drawable.image2);
ImageButton .setVisibility(0);
TextView Test = (TextView)findViewById(R.id.textView);
Test.setVisibility(0);
Test.setText("ID passed is" + id);
break;
UPDATE!私はちょうどXMLレイアウトでImageButtonからアンドロイドsrcを削除し、今は正常に動作しています。助けてくれてありがとう!
まず、Java命名規則(http://java.about.com/od/javasyntax/a/nameconventions.htm)をチェックする必要があります。 'ImageButton ImageButton =(ImageButton)findViewById(..)'は意味をなさない。 – Stephan
どのようにコンパイルしますか? – Blackbelt
ここでは読みやすくするために名前を変更しました。ImageButton ImageButtonは実際にはImageButton myImageButtonNameです。申し訳ありません。 :-) –