【发布时间】:2020-03-01 09:06:41
【问题描述】:
目标
当没有从服务器下载图像时显示默认图像。
问题
我有一个带有图像视图的列表视图(以及一些文本框,但这并不重要)。 我的 imageview 为学生下载图像,但是当学生没有图像时,我试图显示默认图像。 我已经尝试了两件我认为应该可行的事情,设置默认图像或下面的代码。 此代码取自一个活动文件,我将数据库列中的值写入变量(仅显示 img 以保持简单性)
//Image path returned
if (javaRealObject.getString("img").equals(""))
{
imgv = (ImageView)findViewById(R.id.ivImage);
imgv.setImageResource(R.drawable.emptyhead);
Log.d("Test", "Empty");
}
else//No image found in column
{
student.setImage(javaRealObject.getString("img"));
Log.d("Test","Not Empty");
}
但是我在imgv = (ImageView)findViewById(R.id.ivImage); 上得到了一个空引用,我不知道为什么,因为我的图像视图被声明了。
当没有默认图像时,任何帮助都可以实现默认图像的效果
从列提供将不胜感激。
对于更多的上下文,上面的代码是一个调用 listview.xml 的活动,然后调用 row.xml。有问题的图像视图在 row.xml 文件中。
ROW.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/ivImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:src="@drawable/empty_head" /> //default image here
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tvFirstName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/primary"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
调用行的列表
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="450dp"
tools:listitem="@layout/row" >
</ListView>
</LinearLayout>
适配器:
public class DriverAdapter extends ArrayAdapter<Drivers> {
ArrayList<Drivers> ArrayListDrivers;
int Resource;
Context context;
LayoutInflater vi;
public DriverAdapter(Context context, int resource, ArrayList<Drivers> objects) {
super(context, resource, objects);
ArrayListDrivers = objects;
Resource = resource;
this.context = context;
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = vi.inflate(Resource, null);
holder = new ViewHolder();
holder.imageview = (ImageView) convertView.findViewById(R.id.ivImage);
holder.tvName = (TextView) convertView.findViewById(R.id.tvFirstName);
holder.tvDescription = (TextView) convertView.findViewById(R.id.tvLastName);
holder.tvClientid = (TextView) convertView.findViewById(R.id.tvid);
holder.tvExpires = (TextView) convertView.findViewById(R.id.tv_expdate);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.imageview.setImageResource(R.drawable.ic_launcher);
new DownloadImageTask(holder.imageview).execute(ArrayListDrivers.get(position).getImage());
Glide.with(holder.imageview.getContext())
.load(new DownloadImageTask(holder.imageview).execute(ArrayListDrivers.get(position).getImage()) )
.centerCrop()
.placeholder(R.drawable.ic_launcher)
.crossFade()
.into(holder.imageview);
holder.tvName.setText(ArrayListDrivers.get(position).getFirstname());
holder.tvDescription.setText(ArrayListDrivers.get(position).getLastname());
holder.tvClientid.setText(ArrayListDrivers.get(position).getClient_id());
holder.tvExpires.setText("Expiry Date:"+ArrayListDrivers.get(position).getExpires());
return convertView;
}
static class ViewHolder {
public ImageView imageview;
public TextView tvName;
public TextView tvDescription;
public TextView tvClientid;
public TextView tvExpires;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap cImg1 = null;
try {
byte[] decodedString = Base64.decode(urldisplay, Base64.DEFAULT);
cImg1 = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return cImg1;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}
【问题讨论】:
-
请发布您的 XML 代码。
-
上面的代码来自你的适配器类..?
-
发布 xml 并记录。如果其他条件,您还必须声明上面的 findViewbyId 行。
-
@Tanimreja 这不是适配器类,它是 doinbackground 方法中代码的一部分,该方法从数据库中的列中获取的值分配字符串。
-
哦,您已经在 Async 内部初始化了 imageView,这就是它给您 NullPointer 的原因。在 Async 之外初始化 ImageView。
标签: android