【问题标题】:Image in database to listview android数据库中的图像到listview android
【发布时间】:2019-03-14 18:05:05
【问题描述】:

我正在尝试在 android 的列表视图中显示我在数据库中的一些图像以及这部分

@Override
    public View getView (int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater ();
        View listViewItem = inflater.inflate (R.layout.layout, null, true);
        TextView androids = (TextView) listViewItem.findViewById (R.id.tvandroidosnames);
        // TextView textView = (TextView) listViewItem.findViewById (R.id.tvurl);
        // textView.setText (urls [position]);
        androidos.setText (androidosnames [position]);
        ImageView image = (ImageView) listViewItem.findViewById (R.id.imgvw);
        image.setImageBitmap (Bitmap.createScaledBitmap (bitmaps [position], 100, 50, false));
        return listViewItem;
    }

它给了我以下错误:

java.lang.NullPointerException:尝试调用虚拟方法'int android.graphics.Bitmap.getWidth()' 在空对象引用上

【问题讨论】:

标签: android nullpointerexception android-bitmap


【解决方案1】:

import android.app.Activity;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class Customadapter extends ArrayAdapter<String> {
    private String[] androidosnames;
    //private String[] urls;
    private Bitmap[] bitmaps;
    private Activity context;
    public Customadapter(Activity context,    String[] androidosnames,  Bitmap[] bitmaps  ) {
        super(context, R.layout.activity_delivery, androidosnames);
        this.context = context;
        // this.urls = urls;
        this.bitmaps = bitmaps;
        this.androidosnames = androidosnames;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View listViewItem = inflater.inflate(R.layout.layout, null, true);
        TextView  androidos = (TextView) listViewItem.findViewById(R.id.tvandroidosnames);
        // TextView textView = (TextView) listViewItem.findViewById(R.id.tvurl);
        //  textView.setText(urls[position] );
        androidos.setText(androidosnames[position] );
        ImageView image = (ImageView) listViewItem.findViewById(R.id.imgvw);
        image.setImageBitmap(Bitmap.createScaledBitmap(bitmaps[position], 100, 50, false));
        return  listViewItem;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    相关资源
    最近更新 更多