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 getting 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.