【问题标题】:How to change ImageView source in android如何在android中更改ImageView源
【发布时间】:2013-11-01 08:50:20
【问题描述】:

这是我的 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>

这是我的java代码:

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

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

java.lang.NullPointerException

它指向线:

whoamiwith.setBackgroundResource(R.drawable.loginbtn);

【问题讨论】:

  • 您需要像 click.getId() == profileBtn 一样检查。并分享您的代码如何初始化您的 whoamiwith ?
  • 你初始化你的whoamiwith了吗?
  • 你的初始化有问题。检查您的图像视图 ID。
  • ImageView whereami = (ImageView) findViewById(R.id.whereami);伙计们,我是这样理解的..
  • 你是如何处理“whoamiwith”的 你只初始化了whoamiwith rite?哪里呢?并且您正在尝试通过初始化设置背景图像!

标签: java android xml android-imageview


【解决方案1】:
whoamiwith.setImageResource(R.drawable.loginbtn);

【讨论】:

    【解决方案2】:
     ImageView whoamiwith = (ImageView)findViewById(R.id.whoamiwith)  
     Drawable new_image= getResources().getDrawable(R.drawable.loginbtn);   
        whoamiwith.setBackgroundDrawable(new_image);
    

    【讨论】:

    • setBackgroundDrawable 现在不幸被弃用了
    【解决方案3】:

    试试吧

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

    【讨论】:

      【解决方案4】:

      初始化图像视图:

      whoamiwith = findViewByid(R.id.whoamiwith);
      

      然后在 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));
          }
      

      【讨论】:

        【解决方案5】:
        ImageView whoamiwith = (ImageView)findViewById(R.id.whoamiwith);
        whoamiwith.setImageResource(R.drawable.image_i_wanna_put);
        

        【讨论】:

        • 开枪了。哈哈
        【解决方案6】:

        异常是 whoamiwith 为空。 你有没有初始化whoamiwith 喜欢, ImageView whoamiwith = (ImageView)findViewById(R.id.whoamiwith)

        参考Using setImageDrawable dynamically to set image in an ImageView

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-02-27
          • 2015-04-05
          • 2012-06-21
          • 1970-01-01
          • 1970-01-01
          • 2014-09-13
          • 2013-12-21
          • 1970-01-01
          相关资源
          最近更新 更多