我需要在自定义listView中显示此图像和文本

时间:2018-09-03 12:14:31

标签: android

我需要在自定义listView中显示此图像和文本 错误是.....

  

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'                         在com.example.majdaldawoud.appshow.applistview $ customAdapter.getView(applistview.java:61)

    ListView lstv;
    String[] DESC = {"hi", "majd"};
    int[] IMAGES = {R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_applistview);

        lstv=findViewById(R.id.lv);
        customAdapter custom = new customAdapter();
        lstv.setAdapter(custom);

    }
    class customAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return IMAGES.length;
        }
        @Override
        public Object getItem(int i) {
            return null;
        }
        @Override
        public long getItemId(int i) {
            return 0;
        }
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view=getLayoutInflater().inflate(R.layout.listviewrow,null);
            ImageView imageView = view.findViewById(R.id.imageView);
            TextView textView = view.findViewById(R.id.textView);
            imageView.setImageResource(IMAGES[i]);
            textView.setText(DESC[i]);
            return view;
        }
    }

}

3 个答案:

答案 0 :(得分:1)

customAdapter中出现错误,如下所示:

@Override
public long getItemId(int i) {
    return i; //not return 0;
}

答案 1 :(得分:0)

检查您声明了textview id的listviewrow.xml文件,再次检查两者是否相同。

答案 2 :(得分:0)

尝试一下:

    ListView lstv;
    String[] DESC = {"hi", "majd"};
    int[] IMAGES = {R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_applistview);

        lstv = findViewById(R.id.lv);
        customAdapter custom = new customAdapter(this);
        lstv.setAdapter(custom);

    }
    class customAdapter extends BaseAdapter {
       Context mContext;
       public customAdapter(Context context){
         this.mContext = context;
        }
        @Override
        public int getCount() {
            return IMAGES.length;
        }
        @Override
        public Object getItem(int i) {
            return null;
        }
        @Override
        public long getItemId(int i) {
            return i;//return item id 
        }
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = LayoutInflater.from(mContext).inflate(R.layout.listviewrow, null);
            ImageView imageView = view.findViewById(R.id.imageView);
            TextView textView = view.findViewById(R.id.textView);
            imageView.setImageDrawable(getResources().getDrawable(IMAGES[i]));
            textView.setText(DESC[i]);
            return view;
        }
    }
}

确保R.layout.listviewrow具有imageView和textView id。否则,问题将无法解决。 您的listviewrow看起来像。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="horizontal" >
    <ImageView
      android:id="@+id/imageView"
      android:layout_width="50dp"
      android:layout_height="50dp"
      android:layout_marginBottom="5dp"
      android:layout_marginLeft="5dp"
      android:layout_marginRight="5dp"
      android:layout_marginTop="5dp"
      android:src="@drawable/ic_launcher" />
   <TextView
      android:id="@+id/textView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textSize="20sp" 
      android:paddingTop="5dp"/>
</LinearLayout>

希望这可以解决您的问题。