Android Studio-Image showing in design view but not in emulator

≡放荡痞女 提交于 2020-07-18 20:46:03

问题


I am learning android app development.So while I was building a simple app to show two images on the screen,I have used ImageView.The images are displayed in the android studio design screen,but when I am trying to run the app using the genymotion emulator,the screen is just blank.I am not able to see the images.here is the xml file.

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="showmebioapp.studio.com.showmebio.MainActivity"
    android:background="@android:color/holo_blue_bright">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/vegeta"
        android:layout_marginTop="32dp"
        android:id="@+id/imageVegetaId"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/imageGokuId"
        android:layout_alignEnd="@+id/imageGokuId"
        android:layout_marginRight="32dp"
        android:layout_marginEnd="32dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/goku"
        android:layout_marginBottom="79dp"
        android:id="@+id/imageGokuId"
        android:layout_marginLeft="62dp"
        android:layout_marginStart="62dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

MAIN ACTIVITY.JAVA

public class MainActivity extends Activity implements View.OnClickListener {

private ImageView vegetaImage;
private ImageView gokuImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    vegetaImage=(ImageView)findViewById(R.id.imageVegetaId);
    gokuImage=(ImageView)findViewById(R.id.imageGokuId);
    vegetaImage.setOnClickListener(this);
    gokuImage.setOnClickListener(this);
}

@Override
public void onClick(View v) {

    switch(v.getId()){

        case R.id.imageVegetaId:
            Intent vegetaIntent=new Intent(MainActivity.this,DetailsActivity.class);
            vegetaIntent.putExtra("vegetaBio","hello from vegeta");
            startActivity(vegetaIntent);
            break;
        case R.id.imageGokuId:
            Intent gokuIntent=new Intent(MainActivity.this,DetailsActivity.class);
            gokuIntent.putExtra("gokuBio","hello from goku");
            startActivity(gokuIntent);
            break;
    }
}

}

Please Help me in finding me the solution.


回答1:


You are using srcCompat instead of using src just like using back goku instead of goku

  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:src="@drawable/vegeta"
    android:layout_marginTop="32dp"
    android:id="@+id/imageVegetaId"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/imageGokuId"
    android:layout_alignEnd="@+id/imageGokuId"
    android:layout_marginRight="32dp"
    android:layout_marginEnd="32dp" />

as it srcCompat attribute is actually defined within AppCompat library.

Important you will need to add appropriate namespace for this.

xmlns:app="http://schemas.android.com/apk/res-auto"

Important

what you are getting it seems like it is just a lint error that can be ignored. I have tried and have the same error, but it is working correctly.for more info

Happy coding :)




回答2:


Just went through this issue for <1 hour - download and try a different emulator and check it. That was my fix after trying the tips mentioned above!



来源:https://stackoverflow.com/questions/39808796/android-studio-image-showing-in-design-view-but-not-in-emulator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!