Show image in AndroidStudio simulator

1

Good morning,

It's my first time in this forum, so if there is a protocol and I skipped it, I apologize haha.

My doubt is that when I simulate my app in Android Studio, the images associated with the buttons are not displayed. However, I do not see any errors when compiling, it simply takes me to a blank page when I press the button.

Thanks in advance, greetings.

PS: Here you have the code:

From one of the XML images:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/javi"
    android:id="@+id/imageView6" ></ImageView>
</LinearLayout>

From the same image, but Java:

  public class javi extends Activity {
    @Override
     protected void onCreate(Bundle savedInstanceState){
       super.onCreate(savedInstanceState);
       setContentView(R.layout.javi);
    }
  }

From the Main code: public class MainActivity extends Activity {

//Objetos

private Button boton1, boton2, boton3, boton4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Buscamos los botones en nuestro programa

    boton1 = (Button) findViewById (R.id.button);
    boton2 = (Button) findViewById (R.id.button2);
    boton3 = (Button) findViewById (R.id.button3);
    boton4 = (Button) findViewById (R.id.button4);

    //Damos uso a los botones

    boton1.setOnClickListener(new Button.OnClickListener() {
                                  public void onClick(View view) {
                                      mostrarRafa(null);
                                  }
                              }
        );
    boton2.setOnClickListener(new Button.OnClickListener() {
                                  public void onClick(View view) {
                                      mostrarJavi(null);
                                  }
                              }
        );

    boton3.setOnClickListener(new Button.OnClickListener() {
                                  public void onClick (View view){
                                         mostrarMario(null);
                                  }
                                }
        );

    boton4.setOnClickListener(new Button.OnClickListener() {
                                  public void onClick(View view) {
                                      mostrarJuanmi(null);
                                  }
                              }
        );

    }

public void mostrarRafa(View view){
    Intent kwh= new Intent(this, rafa. class);
    startActivity(kwh);
}
public void mostrarJavi(View view){
    Intent kwh= new Intent(this, javi. class);
    startActivity(kwh);
}
public void mostrarMario(View view){
    Intent kwh= new Intent(this, mario. class);
    startActivity(kwh);
}
public void mostrarJuanmi(View view){
    Intent kwh= new Intent(this, juanmi. class);
    startActivity(kwh);
}

   }

Fixed, thank you very much everyone. Greetings

    
asked by J.Soto 07.11.2016 в 13:55
source

1 answer

2

I've seen what the problem is, in your layout you are using app:srcCompat to load the image, if so you should use the compatibility class android.support.v7.widget.AppCompatImageView :

    <android.support.v7.widget.AppCompatImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/javi"
        android:id="@+id/imageView6">
</android.support.v7.widget.AppCompatImageView>

Otherwise using the class ImageView , you should use only the property android:src to assign the image:

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/javi"
        android:id="@+id/imageView6">
   </ImageView>
  • In both cases, it ensures that the specified image is actually inside the folder /drawable

You should have no problem loading the image with any of the two classes

    
answered by 07.11.2016 / 18:53
source