Android:获取 ImageView imag = (ImageView) findViewById(R.id.image) 的 NullPointerException

Android : getting NullPointerException for ImageView imag = (ImageView) findViewById(R.id.image)(Android:获取 ImageView imag = (ImageView) findViewById(R.id.image) 的 NullPointerException)
本文介绍了Android:获取 ImageView imag = (ImageView) findViewById(R.id.image) 的 NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到 ImageView imag = (ImageView) findViewById(R.id.image) 的空指针异常.我在 R.layout.screen3 中声明了自定义布局,并在 R.layout.custom_row_screen3 中声明了自定义列表.每行都有少量任务,根据其状态,每行右侧会显示一张图片.

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.

请指导我解决这个问题,因为我是 android 新手.

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"
    />

谢谢阿比纳夫·泰吉

推荐答案

谢谢大家你们都为我指出了正确的方向,即我的 imageview 没有指向我的适配器我按照 http://devblogs 中的步骤操作.net/2011/01/04/custom-listview-with-image-using-simpleadapter/它正在工作!只需要将我的图像放入 hashmap 中......我的头撞到了墙上!!!

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!!!

这篇关于Android:获取 ImageView imag = (ImageView) findViewById(R.id.image) 的 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)