【问题标题】:Change ImageView drawable of an inflated layout更改膨胀布局的 ImageView 可绘制对象
【发布时间】:2016-04-03 05:58:41
【问题描述】:

我想更改imageview的drawable,它保存到磁盘,并且可用。但是在设置drawable时,会出现NullPointerException

问题是,如何改变膨胀布局的imageview drawable[Source image is "/sdcard/myappfolder/user.png"],布局膨胀

这是我的部分代码:

    intilt = itilt.inflate(R.layout.pic_container, null, false);    //layout is inflated successfully
    picCheck(); // method to check file availability, working perfect, if file exists, returns 1, else returns 0;
    if(picmark == 1){
        Bitmap pch = BitmapFactory.decodeFile(file.getAbsolutePath());
        imageView = (ImageView)findViewById(R.id.contimg);  //contimg is ImageView id from layout pic_container
        imageView.setImageBitmap(pch);  
        /**
        *  Caused by: java.lang.NullPointerException
        *  at com.package.packagename.javaact.onCreate(javaact.java:83)
        */
    }
    else{
        Toast.makeText(this, "File Not Found, setting Default", Toast.LENGTH_SHORT).show();
    }

【问题讨论】:

  • intilt = itilt.inflate(R.layout.pic_container, null, false);你能看到你膨胀的视图吗?你甚至没有将它添加到任何地方。

标签: android file-handling android-inflate


【解决方案1】:

试试这样:

imageView = (ImageView) intilt .findViewById(R.id.contimg);

【讨论】:

  • 谢谢。 imageView = (ImageView) intilt.findViewById(R.id.contimg); //这行得通。谢谢。
【解决方案2】:

试试这个

intilt = itilt.inflate(R.layout.pic_container, null, false);
picCheck();
if(picmark == 1){
    Bitmap pch = BitmapFactory.decodeFile(file.getAbsolutePath());
    imageView = (ImageView) intilt.findViewById(R.id.contimg); //Change here 
    imageView.setImageBitmap(pch);  
}
else{
    Toast.makeText(this, "File Not Found, setting Default", Toast.LENGTH_SHORT).show();
}

【讨论】:

  • 谢谢。 imageView = (ImageView) intilt.findViewById(R.id.contimg); //这行得通。谢谢。
【解决方案3】:
//you should add it to somewhere. intilt = itilt.inflate(R.layout.pic_container,a existed View, true); 
intilt = itilt.inflate(R.layout.pic_container, null, false);    //layout is inflated successfully
    picCheck(); // method to check file availability, working perfect, if file exists, returns 1, else returns 0;
    if(picmark == 1){
        Bitmap pch = BitmapFactory.decodeFile(file.getAbsolutePath());
        //if you have inflated it into somewhere, you can just use below.
        imageView = (ImageView)intilt.findViewById(R.id.contimg);  //contimg is ImageView id from layout pic_container
        imageView.setImageBitmap(pch);  
        /**
        *  Caused by: java.lang.NullPointerException
        *  at com.package.packagename.javaact.onCreate(javaact.java:83)
        */
    }
    else{
        Toast.makeText(this, "File Not Found, setting Default", Toast.LENGTH_SHORT).show();
    }

【讨论】:

  • 我认为你有一个错字。应该是intilt.findViewById
  • 谢谢。 imageView = (ImageView) intilt.findViewById(R.id.contimg); //这行得通。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多