ArrayList of Hashmaps to ListView nullpointerexception
I keep getting this error:
Unable to start activity ComponentInfo{com.Nostradamus/com.Nostradamus.Contactenlijst}: java.lang.NullPointerException
And I don't know what I do wrong. It has something to do with the last code: lv.setAdapter(new SimpleAdapter(this, mylist, R.layout.list_item, from, to));
This is my java code:
public class Contactenlijst extends Activity {
ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] from = new String[] {"voornaam", "achternaam", "geboortedatum", "adres", "postcode", "woonplaats", "email", "telefoon"};
int[] to = new int[]{R.id.voornaam, R.id.achternaam, R.id.geboortedatum, R.id.adres, R.id.postcode, R.id.woonplaats, R.id.email, R.id.telefoon};
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
Log.d("test","test2");
// Get the data (see above)
JSONObject json = Database
.getJSONfromURL("http://fabian.nostradamus.nu/Android/getcontactinfo.php");
Log.d("test","test3");
try {
JSONArray contactinfo = json.getJSONArray("contactlijst");
Log.d("test","test4");
// Loop the Array
for (int i = 0; i < contactinfo.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = contactinfo.getJSONObject(i);
map.put("voornaam", e.getString("staff_name"));
map.put("achternaam", e.getString("staff_lastname"));
map.put("geboortedatum", e.getString("staff_dateofbirth"));
map.put("adres", e.getString("staff_address"));
map.put("postcode", e.getString("staff_address_postal"));
map.put("woonplaats", e.getString("staff_address_city"));
map.put("e开发者_运维知识库mail", e.getString("staff_email"));
map.put("telefoon", e.getString("staff_phone"));
mylist.add(map);
Log.e("test",map.toString());
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
lv = (ListView) findViewById(R.id.list);
lv.setAdapter(new SimpleAdapter(this, mylist, R.layout.list_item, from, to));
}
}
And these are my xmls:
Contactenlijst
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/voornaam"
android:layout_width="50dip"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/achternaam"
android:layout_width="70dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
<TextView android:id="@+id/geboortedatum"
android:layout_width="60dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
<TextView android:id="@+id/adres"
android:layout_width="50dip"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/postcode"
android:layout_width="70dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
<TextView android:id="@+id/woonplaats"
android:layout_width="60dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
<TextView android:id="@+id/email"
android:layout_width="60dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
<TextView android:id="@+id/telefoon"
android:layout_width="60dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
Contactview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:drawSelectorOnTop="false"/>
<TextView android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No data">
</TextView>
</LinearLayout>
and Listitem
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
you didn't set the layout using
setContentView(R.layout.yourlayout);
so without add layout you are using your listview that's why you getting NullPointerException.
add this above line after
super.onCreate(savedInstanceState);
or before the use listview object
You need to call setContentView(...)
before lv = (ListView) findViewById(R.id.list);
setContentView(...)
inflates the XML file for your layout. If you don't do this, then lv
will be null.
精彩评论