Irina
Irina

Reputation: 1

One ImageButton works, the others crash app

I have 6 ImageButtons in one activity, the first one works without any issues but the others when clicked on the device crash the app. I have used the same code for the rest of the ImageButtons - am I missing something?

<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>

And

    // 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);}}

First few logcat errors are these actually Thank you so much for all your effort!

04-03 18:50:46.461 140-10698/? W/SocketClient: write error (Broken pipe) W/ADB_SERVICES: terminating JDWP 9676 connection: I/O error

Upvotes: 0

Views: 40

Answers (1)

Arvindsinc2
Arvindsinc2

Reputation: 714

The layout file seems perfect. Use this code. I Have used 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);}}

Upvotes: 1

Related Questions