java - 一个 ImageButton 可以工作,其他的则崩溃应用程序

标签 java android-imagebutton

我在一个 Activity 中有 6 个 ImageButton,第一个可以正常工作,但在设备上单击其他按钮时,应用程序会崩溃。 我对其余的 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 错误

最佳答案

布局文件看起来很完美。使用此代码。我用过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);}}

关于java - 一个 ImageButton 可以工作,其他的则崩溃应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43120823/

相关文章:

java - 如何使 ImageButton 随机放置在线性布局中? (安卓)

java - 更改 ImageButton Src 图像 OnClick

android java.langNullPointerException 空对象引用

java - 共享元素转换后如何使用动画

javascript - 通过Ajax POST方法发送FormData返回403

java - 如何测试带有键码的修饰符是否已关闭

Android设置背景颜色按钮按下

android - 更改 Android imageButton 在旧图像后面绘制新图像

java - 如何在 firebase auth 和 firebase - 实时数据库中获取相同的 id?

java - 我无法将 Java 连接到 MySQL