开发者

NullPointerException with simpleAdapter in android

I am using a simpleAdapter to create a listview, however I get this error:

Caused by: java.lang.NullPointerException
at com.tab.Activity.onCreate(Activity.java:36)

and this the code that I am using:

ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
 private SimpleAdapter notes;

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

    notes = new SimpleAdapter( 
            this, 
            list,
            R.layout.display_layout,
            new String[] { "line1" },
            new int[] { R.id.text1}  );
   ListView lv = (ListView) findViewById(R.id.listview);
   lv.setAdapter( notes );
}

line 36 would be

new int[] { R.id.text1}  );

text1 is defined, so why I am gett开发者_如何转开发ing nullpointer error?


If the evaluation of

new int[] { R.id.text1 }

results in a NullPointerException it is due to the fact that R.id is null.

The exception would also arise if R.id happened to be an Integer which was null.


Try this:

    notes = new SimpleAdapter(this, list, R.layout.display_layout,
            new String[] { "line1" }, new int[] { android.R.id.text1 });


I think the value of R.id is returning null. Try:

new int[] { ((R.id !=null && R.id.text1 >-1)?R.id.text1 :-1)};

Edit

I am using -1 to assume zero could be an valid value.


Finally, I solved the issue by implementing new SimpleAdapter following this tutorial: http://kurtchen.com/blog/2010/07/30/use-simpleadapter-to-make-a-list-with-icons/

I think the problem was that "ArrayList list" variable was null and therefore SimpleAdapter could not map any value to the "R.id.text1" column.

Thank you all for your answers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜