Java 如何在android中更改ImageView源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19535571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:09:29  来源:igfitidea点击:

How to change ImageView source in android

javaandroidxmlandroid-imageview

提问by CENT1PEDE

This is my xml, this is located inside a fragment that appears in my activity.

这是我的 xml,它位于出现在我的活动中的片段内。

<FrameLayout
                    android:id="@+id/frame1"
                    android:layout_width="wrap_content"
                    android:layout_height="115dp"
                    android:layout_margin="2dp"
                    android:layout_weight="0.33">

                    <ImageView
                        android:id="@+id/whoamiwith"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:scaleType="fitCenter"
                        android:src="@drawable/default_image" />
                </FrameLayout>

And this is my java code :

这是我的 Java 代码:

@Override
public void onClick(View click) {
    if (click == profileBtn) {
        whoamiwith.setBackgroundResource(R.drawable.image_i_wanna_put);
    }
}

I am trying to change the image source of the image view. There are no syntax errors but when I click the button the emulator is giving me a force close and on the logcat it says:

我正在尝试更改图像视图的图像源。没有语法错误,但是当我单击按钮时,模拟器给我强制关闭,并且在 logcat 上它说:

java.lang.NullPointerException

java.lang.NullPointerException

It's pointing to the line:

它指向这条线:

whoamiwith.setBackgroundResource(R.drawable.loginbtn);

采纳答案by Bhanu Sharma

whoamiwith.setImageResource(R.drawable.loginbtn);

回答by SweetWisher ツ

Initialize image view :

初始化图像视图:

whoamiwith = findViewByid(R.id.whoamiwith);

Then on Click method, write this lines to change the image resource :

然后在 Click 方法上,编写以下行来更改图像资源:

 if(android.os.Build.VERSION.SDK_INT > 15)
    {
        // for API above 15
        whoamiwith.setBackground(getResources().getDrawable(R.drawable.loginbtn));
    }
    else
    {
        // for API below 15
        whoamiwith.setBackgroundDrawable(getResources().getDrawable(R.drawable.loginbtn));
    }

回答by Manivannan

Exception is whoamiwithis null. did u initialise whoamiwithlike, ImageView whoamiwith = (ImageView)findViewById(R.id.whoamiwith)

异常whoamiwith为空。你初始化了whoamiwith,ImageView whoamiwith = (ImageView)findViewById(R.id.whoamiwith)

Refer Using setImageDrawable dynamically to set image in an ImageView

参考使用 setImageDrawable 动态设置 ImageView 中的图像

回答by Eloytxo

 ImageView whoamiwith = (ImageView)findViewById(R.id.whoamiwith)  
 Drawable new_image= getResources().getDrawable(R.drawable.loginbtn);   
    whoamiwith.setBackgroundDrawable(new_image);

回答by Jitesh Dalsaniya

Just try

你试一试

ImageView whoamiwith = (ImageView)findViewById(R.id.whoamiwith)  
whoamiwith.setImageResource(R.id.new_image);

回答by RussVirtuoso

ImageView whoamiwith = (ImageView)findViewById(R.id.whoamiwith);
whoamiwith.setImageResource(R.drawable.image_i_wanna_put);