gpt4 book ai didi

java - 列表中的 ImageView 空指针异常

转载 作者:行者123 更新时间:2023-12-01 23:38:49 25 4
gpt4 key购买 nike

我正在尝试设置一个仅包含一些图像的 ListView ,即没有文本。这是我的文件:

//the Main File:
public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ListView listview1 = (ListView)findViewById(R.id.listView1);
final Integer[] mThumbIds = {
R.drawable.blue, R.drawable.blue2,
R.drawable.blue3, R.drawable.blue4,
R.drawable.blue5, R.drawable.blue6
};
listview1.setAdapter(new ImageAdapter(this,mThumbIds));
listview1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Upon Clicking
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class ImageAdapter extends ArrayAdapter<Integer> {
private Context mContext;
Integer [] resources;
public ImageAdapter (Context c, Integer[] resources) {
super(c, R.layout.activity_main, resources);
//System.out.println("Set up of Image Adapter");
mContext = c;
this.resources=resources;
for(Integer resource:resources){
System.out.println(resource);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.activity_main, parent, false);
ImageView imageView = (ImageView) rowView.findViewById(R.layout.image_view);
imageView.setImageResource(resources[position]);
return rowView;
}
}
}

XML 如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/export_folder_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Formation Name:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ListView
android:id="@+id/listView1"
android:layout_below="@+id/color_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:background="#ff000000" >
</ListView>
</RelativeLayout>

最后,定义 ImageView:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:minHeight="64dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="9dp"
android:layout_alignParentTop="true"/>
</RelativeLayout>

不幸的是,我在 ImageView 上设置图像资源时遇到 NullPointerExceptions。图像资源存在 - 我已经确定。我不确定我做错了什么,而且我是 Android 新手。谢谢。

最佳答案

看起来您在 getView() 中夸大了错误的 xml,

View rowView = inflater.inflate(R.layout.activity_main, parent, false);

这里的 R.layout.activity_main 应替换为包含 ImageView 的 xml 文件。

从你的代码中,我认为包含 ImageView 的 xml 文件的名称是,R.layout.image_view

所以,你的最终代码应该是这样的,

View rowView = inflater.inflate(R.layout.image_view, parent, false);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);
imageView.setImageResource(resources[position]);

关于java - 列表中的 ImageView 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18266127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com