Android 我需要在自定义列表视图中显示此图像和文本

Android 我需要在自定义列表视图中显示此图像和文本,android,Android,我需要在自定义列表视图中显示此图像和文本 错误是 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)” 在com.example.majdaldawoud.appshow.applistview$customAdapter.getView(applistview.java:61)上 再次检查声明textview id Check的li

我需要在自定义列表视图中显示此图像和文本 错误是

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


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

customAdapter中有一个错误,如下所示:

@Override
public long getItemId(int i) {
    return i; //not return 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>


希望这能解决您的问题。

获取视图方法中的错误发布您的布局
R.layout.listviewrow
,看起来您没有id为
TextView
TextView
文本视图。将一些数据传递给适配器朋友似乎您的texview id错误。在com.example.majdaldawoud.appshow.applistview$customAdapter.getView(applistview.java:55)在imageview行中。setimageresource@MajdAlDawood根据我的问题,您最初的问题已经解决。现在您将面临从drawable显示图像的问题。我只是更新我的答案。请看一看。
<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>