Android:获取 ImageView imag = (ImageView) findViewById(R.id.image) 的 NullPointerException
我收到 ImageView imag = (ImageView) findViewById(R.id.image) 的空指针异常。我在 R.layout.screen3 中声明了自定义布局,并在 R.layout.custom_row_screen3 中声明了自定义列表。每行都有很少的任务,根据任务的状态,每行的右侧都会显示一个图像。
请指导我解决这个问题,因为我是 Android 新手。
这是我的代码:::
public class screen3 extends ListActivity{
final ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
...
...
...
TextView tv;
ImageView imag;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen3);
System.out.println("Screen3");
...
...
...
SimpleAdapter adapter = new SimpleAdapter(this, list,
R.layout.custom_row_screen3,
new String[] { "tname","schedule", "note", "time"}, new int[] { R.id.text1Screen3task,
R.id.text2Screen3task, R.id.text3Screen3task, R.id.text4Screen3task});
populateList();
setListAdapter(adapter);
}
public void populateList() {
for(int i = 0; i <numberOfTasks; i++)
{
HashMap<String, String> h = new HashMap<String, String>();
HashMap<String, String> hm = new HashMap<String, String>();
h = tasks.get(i);
...
...
hm.put("tname", h.get("taskName"));
hm.put("schedule", h.get("STime")+" - "+h.get("ETime"));
if(h.get("taskStatus").trim().equals("1")){
imag = (ImageView) findViewById(R.id.image);// <<---- NULL RETURNED
imag.setImageResource(R.drawable.done);
...
...
}
else{
imag = (ImageView) findViewById(R.id.image);// <<---- NULL RETURNED
imag.setImageResource(R.drawable.not);
...
...
}
list.add(hm);
}
...
...
}
screen3 xml
<ListView android:id="@id/android:list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#000000"
android:drawSelectorOnTop="false">
</ListView>
custom_row_screen3 xml
<LinearLayout android:orientation="vertical"
android:layout_width="0dip" android:layout_weight="1"
android:layout_height="wrap_content">
<TextView android:id="@+id/text1Screen3task"
android:layout_width="wrap_content" android:layout_height="0dip"
android:layout_weight="1" android:textSize="21sp" android:textStyle="bold"
android:gravity="clip_horizontal" />
<TextView android:id="@+id/text2Screen3task"
android:layout_width="wrap_content" android:layout_height="0dip"
android:layout_weight="1" android:textSize="16sp" android:gravity="clip_horizontal" />
<TextView android:id="@+id/text3Screen3task"
android:layout_width="wrap_content" android:layout_height="0dip"
android:layout_weight="1" android:textSize="16sp" android:gravity="clip_horizontal" />
<TextView android:id="@+id/text4Screen3task"
android:layout_width="wrap_content" android:layout_height="0dip"
android:layout_weight="1" android:textSize="16sp" android:gravity="clip_horizontal" />
</LinearLayout>
<ImageView android:id="@+id/image" android:layout_width="wrap_content" android:paddingBottom="51dip"
android:layout_height="wrap_content" android:layout_marginRight="16dip"
/>
谢谢 阿比纳夫·蒂亚吉
I am getting Null Pointer Exception for ImageView imag = (ImageView) findViewById(R.id.image). I have custome layout declared in R.layout.screen3 with custome list declared in R.layout.custom_row_screen3. Each row has few tasks and depending on their status an image is shown to right of each row.
Please guide me in getting this resolved as I am new to android.
here is my code below :::
public class screen3 extends ListActivity{
final ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
...
...
...
TextView tv;
ImageView imag;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen3);
System.out.println("Screen3");
...
...
...
SimpleAdapter adapter = new SimpleAdapter(this, list,
R.layout.custom_row_screen3,
new String[] { "tname","schedule", "note", "time"}, new int[] { R.id.text1Screen3task,
R.id.text2Screen3task, R.id.text3Screen3task, R.id.text4Screen3task});
populateList();
setListAdapter(adapter);
}
public void populateList() {
for(int i = 0; i <numberOfTasks; i++)
{
HashMap<String, String> h = new HashMap<String, String>();
HashMap<String, String> hm = new HashMap<String, String>();
h = tasks.get(i);
...
...
hm.put("tname", h.get("taskName"));
hm.put("schedule", h.get("STime")+" - "+h.get("ETime"));
if(h.get("taskStatus").trim().equals("1")){
imag = (ImageView) findViewById(R.id.image);// <<---- NULL RETURNED
imag.setImageResource(R.drawable.done);
...
...
}
else{
imag = (ImageView) findViewById(R.id.image);// <<---- NULL RETURNED
imag.setImageResource(R.drawable.not);
...
...
}
list.add(hm);
}
...
...
}
screen3 xml
<ListView android:id="@id/android:list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#000000"
android:drawSelectorOnTop="false">
</ListView>
custom_row_screen3 xml
<LinearLayout android:orientation="vertical"
android:layout_width="0dip" android:layout_weight="1"
android:layout_height="wrap_content">
<TextView android:id="@+id/text1Screen3task"
android:layout_width="wrap_content" android:layout_height="0dip"
android:layout_weight="1" android:textSize="21sp" android:textStyle="bold"
android:gravity="clip_horizontal" />
<TextView android:id="@+id/text2Screen3task"
android:layout_width="wrap_content" android:layout_height="0dip"
android:layout_weight="1" android:textSize="16sp" android:gravity="clip_horizontal" />
<TextView android:id="@+id/text3Screen3task"
android:layout_width="wrap_content" android:layout_height="0dip"
android:layout_weight="1" android:textSize="16sp" android:gravity="clip_horizontal" />
<TextView android:id="@+id/text4Screen3task"
android:layout_width="wrap_content" android:layout_height="0dip"
android:layout_weight="1" android:textSize="16sp" android:gravity="clip_horizontal" />
</LinearLayout>
<ImageView android:id="@+id/image" android:layout_width="wrap_content" android:paddingBottom="51dip"
android:layout_height="wrap_content" android:layout_marginRight="16dip"
/>
Thanks
Abhinav Tyagi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简而言之:您应该编写一个 ListAdapter 来绘制列表中的视图。您似乎在错误的视图/上下文(在 screen3 xml 布局上)上使用了
findViewById()
。包含 ListAdapter 的示例可用在 Android 开发者网站。getView() 方法基本上应该如下所示:
In short: You should write a ListAdapter which draws the views in the list. You seem to use
findViewById()
on the wrong view/context (on the screen3 xml layout). An example containing a ListAdapter is available at the android developer site.The
getView()
method should basically look something like this:检查你的lading的资源在你的res文件夹中是否可用。
check whether the resource which your laoding is available in your res folder or not.
看来您没有在布局内的 imageview 中指定 android:id="@+id/image"
编辑:我认为问题是您在 ListActivity 中的 ListView 包含您的 custom_row 之前使用 findViewById ,它是谁拥有图像视图。
尝试在 populateList() 之前执行 setListAdapter
It seems that you haven't specified android:id="@+id/image" in the imageview inside your layout
Edit: I think the problem is you are using findViewById before the ListView in your ListActivity contains your custom_row which is who has the ImageView.
Try doing setListAdapter before populateList()
populateList() 在 ListView 渲染到屏幕之前运行,因此 Activity 中不存在 custom_row_screen3。
您可以继承 SimpleAdapter 并覆盖 getView() 或 setViewImage(),但我经常发现匿名实现 SimpleAdapter.ViewBinder 并用 SimpleAdapter#setViewBinder()。
在 ViewBinder 中,如果传入的 View 不是 ImageView,则返回 false。如果是,您可以使用数据映射(作为对象传入)来检查“taskStatus”键的值。
确保在调用 setListAdapter() 之前执行此操作。
populateList() runs before the ListView is rendered to the screen, so the custom_row_screen3 doesn't exist in the Activity.
You could subclass SimpleAdapter and override getView() or setViewImage(), but I often find it simpler to anonymously implement a SimpleAdapter.ViewBinder and assign it with SimpleAdapter#setViewBinder().
In the ViewBinder, return false if the View that's passed in isn't an ImageView. If it is, you can use the map of data (passed in as an Object) to check the value of the "taskStatus" key.
Make sure to do this before calling setListAdapter().
谢谢大家
你们都给我指出了正确的方向,我的图像视图没有指向我的适配器
我按照 http://devblogs 中的步骤操作。净/2011/01/04/custom-listview-with-image-using-simpleapter/
它正在工作!
只需要将我的图像放入哈希图中...我正在用头撞墙!
Thanks all of you
You all all pointed me in right direction that my imageview is not pointing to my adapter
I followed the steps as in http://devblogs.net/2011/01/04/custom-listview-with-image-using-simpleadapter/
and it was working!!!
just needed to put my image in hashmap... I was banging my head to the wall!!!