我在一个活动中有6个ImageButtons,第一个工作没有任何问题,但其他人在点击设备时会崩溃应用程序。 我对ImageButtons的其余部分使用了相同的代码 - 我错过了什么?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF54561E"
android:orientation="vertical"
android:textAlignment="center">
<ImageView
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_margin="8dp"
android:layout_weight="1"
android:contentDescription="@string/Logo"
android:src="@drawable/idailment"
android:textAlignment="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_weight="1"
android:text="@string/IdDisease"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:textStyle="italic" />
<TextView
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:gravity="center"
android:text="@string/BlackRotButton"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold" />
<ImageButton
android:id="@+id/BlackRotAnim"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:background="@drawable/black_rot_anim"
android:contentDescription="@string/BlackRotButton"
android:gravity="center"
android:onClick="goToDiseaseBlackRot"
android:scaleType="centerCrop" />
<TextView
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:gravity="center"
android:text="@string/BacterialBrownSpotButton"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold" />
<ImageButton
android:id="@+id/BacterialBrownSpotAnim"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:background="@drawable/bacterial_brown_spot_anim"
android:contentDescription="@string/BacterialBrownSpotButton"
android:gravity="center"
android:onClick="goToDiseaseBacterialBrownSpot"
android:scaleType="centerCrop" />
<TextView
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:gravity="center"
android:text="@string/BudBlastButton"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold" />
<ImageButton
android:id="@+id/BudBlastAnim"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:background="@drawable/bud_blast_anim"
android:contentDescription="@string/BudBlastButton"
android:gravity="center"
android:onClick="goToDiseaseBudBlast"
android:scaleType="centerCrop" />
<TextView
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:gravity="center"
android:text="@string/RotButton"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold" />
<ImageButton
android:id="@+id/RotAnim"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:background="@drawable/rot_anim"
android:contentDescription="@string/RotButton"
android:gravity="center"
android:onClick="goToDiseaseRot"
android:scaleType="centerCrop" />
<TextView
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:gravity="center"
android:text="@string/SunBurnButton"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold" />
<ImageButton
android:id="@+id/SunBurnAnim"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:background="@drawable/sun_burn_anim"
android:contentDescription="@string/SunBurnButton"
android:gravity="center"
android:onClick="goToDiseaseSunBurn"
android:scaleType="centerCrop" />
<TextView
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:gravity="center"
android:text="@string/VirusButton"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold" />
<ImageButton
android:id="@+id/VirusAnim"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:background="@drawable/virus_anim"
android:contentDescription="@string/VirusButton"
android:gravity="center"
android:onClick="goToDiseaseVirus"
android:scaleType="centerCrop" />
</LinearLayout>
和
// Load the ImageView that will host the animation and
// set its background to our AnimationDrawable XML resource.
ImageView blackRotButton = (ImageView)findViewById(BlackRotAnim);
blackRotButton.setBackgroundResource(R.drawable.black_rot_anim);
// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable diseaseButton1Animation = (AnimationDrawable) blackRotButton.getBackground();
// Start the animation (looped playback by default).
diseaseButton1Animation.start();
// Load the ImageView that will host the animation and
// set its background to our AnimationDrawable XML resource.
ImageView bacterialBrownSpotButton = (ImageView)findViewById(BacterialBrownSpotAnim);
bacterialBrownSpotButton.setBackgroundResource(R.drawable.bacterial_brown_spot_anim);
// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable diseaseButton2Animation = (AnimationDrawable) bacterialBrownSpotButton.getBackground();
// Start the animation (looped playback by default).
diseaseButton2Animation.start();
// Load the ImageView that will host the animation and
// set its background to our AnimationDrawable XML resource.
ImageView budBlasttButton = (ImageView)findViewById(BudBlastAnim);
budBlasttButton.setBackgroundResource(R.drawable.bud_blast_anim);
// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable diseaseButton3Animation = (AnimationDrawable) budBlasttButton.getBackground();
// Start the animation (looped playback by default).
diseaseButton3Animation.start();
// Load the ImageView that will host the animation and
// set its background to our AnimationDrawable XML resource.
ImageView rotButton = (ImageView)findViewById(RotAnim);
rotButton.setBackgroundResource(R.drawable.rot_anim);
// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable diseaseButton4Animation = (AnimationDrawable) rotButton.getBackground();
// Start the animation (looped playback by default).
diseaseButton4Animation.start();
// Load the ImageView that will host the animation and
// set its background to our AnimationDrawable XML resource.
ImageView sunBurnButton = (ImageView)findViewById(SunBurnAnim);
sunBurnButton.setBackgroundResource(R.drawable.sun_burn_anim);
// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable diseaseButton5Animation = (AnimationDrawable) sunBurnButton.getBackground();
// Start the animation (looped playback by default).
diseaseButton5Animation.start();
// Load the ImageView that will host the animation and
// set its background to our AnimationDrawable XML resource.
ImageView virusButton = (ImageView)findViewById(VirusAnim);
virusButton.setBackgroundResource(R.drawable.virus_anim);
// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable diseaseButton6Animation = (AnimationDrawable) virusButton.getBackground();
// Start the animation (looped playback by default).
diseaseButton6Animation.start();}
/**
* This method is called when BlackRot button is clicked.
*/
public void goToDiseaseBlackRot (View view){
Intent intent = new Intent (this, DiseaseBlackRot.class);
startActivity(intent);}
/**
* This method is called when BlackRot button is clicked.
*/
public void goToDiseaseBacterialBrownSpot (View view){
Intent intent = new Intent (this, DiseaseBacterialBrownSpot.class);
startActivity(intent);}
/**
* This method is called when BlackRot button is clicked.
*/
public void goToDiseaseRot (View view){
Intent intent = new Intent (this, DiseaseRot.class);
startActivity(intent);}
/**
* This method is called when BlackRot button is clicked.
*/
public void goToDiseaseVirus (View view){
Intent intent = new Intent (this, DiseaseVirus.class);
startActivity(intent);}
/**
* This method is called when BlackRot button is clicked.
*/
public void goToDiseaseSunBurn (View view){
Intent intent = new Intent (this, DiseaseSunBurn.class);
startActivity(intent);}
/**
* This method is called when BlackRot button is clicked.
*/
public void goToDiseaseBudBlast (View view){
Intent intent = new Intent (this, DiseaseBudBlast.class);
startActivity(intent);}}
实际上,最初有几个logcat错误 非常感谢你的所有努力!
04-03 18:50:46.461 140-10698 /? W / SocketClient:写错误(断管) W / ADB_SERVICES:终止JDWP 9676连接:I / O错误
答案 0 :(得分:1)
布局文件看起来很完美。使用此代码。我用过R.id:
// Load the ImageView that will host the animation and
// set its background to our AnimationDrawable XML resource.
ImageView blackRotButton = (ImageView)findViewById(R.id.BlackRotAnim);
blackRotButton.setBackgroundResource(R.drawable.black_rot_anim);
// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable diseaseButton1Animation = (AnimationDrawable) blackRotButton.getBackground();
// Start the animation (looped playback by default).
diseaseButton1Animation.start();
// Load the ImageView that will host the animation and
// set its background to our AnimationDrawable XML resource.
ImageView bacterialBrownSpotButton = (ImageView)findViewById(R.id.BacterialBrownSpotAnim);
bacterialBrownSpotButton.setBackgroundResource(R.drawable.bacterial_brown_spot_anim);
// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable diseaseButton2Animation = (AnimationDrawable) bacterialBrownSpotButton.getBackground();
// Start the animation (looped playback by default).
diseaseButton2Animation.start();
// Load the ImageView that will host the animation and
// set its background to our AnimationDrawable XML resource.
ImageView budBlasttButton = (ImageView)findViewById(R.id.BudBlastAnim);
budBlasttButton.setBackgroundResource(R.drawable.bud_blast_anim);
// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable diseaseButton3Animation = (AnimationDrawable) budBlasttButton.getBackground();
// Start the animation (looped playback by default).
diseaseButton3Animation.start();
// Load the ImageView that will host the animation and
// set its background to our AnimationDrawable XML resource.
ImageView rotButton = (ImageView)findViewById(R.id.RotAnim);
rotButton.setBackgroundResource(R.drawable.rot_anim);
// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable diseaseButton4Animation = (AnimationDrawable) rotButton.getBackground();
// Start the animation (looped playback by default).
diseaseButton4Animation.start();
// Load the ImageView that will host the animation and
// set its background to our AnimationDrawable XML resource.
ImageView sunBurnButton = (ImageView)findViewById(R.id.SunBurnAnim);
sunBurnButton.setBackgroundResource(R.drawable.sun_burn_anim);
// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable diseaseButton5Animation = (AnimationDrawable) sunBurnButton.getBackground();
// Start the animation (looped playback by default).
diseaseButton5Animation.start();
// Load the ImageView that will host the animation and
// set its background to our AnimationDrawable XML resource.
ImageView virusButton = (ImageView)findViewById(R.id.VirusAnim);
virusButton.setBackgroundResource(R.drawable.virus_anim);
// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable diseaseButton6Animation = (AnimationDrawable) virusButton.getBackground();
// Start the animation (looped playback by default).
diseaseButton6Animation.start();}
/**
* This method is called when BlackRot button is clicked.
*/
public void goToDiseaseBlackRot (View view){
Intent intent = new Intent (this, DiseaseBlackRot.class);
startActivity(intent);}
/**
* This method is called when BlackRot button is clicked.
*/
public void goToDiseaseBacterialBrownSpot (View view){
Intent intent = new Intent (this, DiseaseBacterialBrownSpot.class);
startActivity(intent);}
/**
* This method is called when BlackRot button is clicked.
*/
public void goToDiseaseRot (View view){
Intent intent = new Intent (this, DiseaseRot.class);
startActivity(intent);}
/**
* This method is called when BlackRot button is clicked.
*/
public void goToDiseaseVirus (View view){
Intent intent = new Intent (this, DiseaseVirus.class);
startActivity(intent);}
/**
* This method is called when BlackRot button is clicked.
*/
public void goToDiseaseSunBurn (View view){
Intent intent = new Intent (this, DiseaseSunBurn.class);
startActivity(intent);}
/**
* This method is called when BlackRot button is clicked.
*/
public void goToDiseaseBudBlast (View view){
Intent intent = new Intent (this, DiseaseBudBlast.class);
startActivity(intent);}}