【问题标题】:ArrayList of Hashmaps to ListView nullpointerexceptionHashmaps 的 ArrayList 到 ListView nullpointerexception
【发布时间】:2011-11-17 00:48:44
【问题描述】:

我不断收到此错误:

无法启动活动 ComponentInfo{com.Nostradamus/com.Nostradamus.Contactenlijst}:java.lang.NullPointerException

而且我不知道我做错了什么。 它与最后一个代码有关: lv.setAdapter(new SimpleAdapter(this, mylist, R.layout.list_item, from, to));

这是我的java代码:

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("email", 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));

}

}

这些是我的 xml:

联系方式

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

联系方式

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

和列表项

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

【问题讨论】:

    标签: android listview arraylist hashmap nullpointerexception


    【解决方案1】:

    你没有使用

    设置布局
    setContentView(R.layout.yourlayout);
    

    所以如果没有添加布局,您正在使用您的列表视图,这就是您得到 NullPointerException 的原因。

    之后添加上一行
    super.onCreate(savedInstanceState);
    

    或者在使用listview对象之前

    【讨论】:

      【解决方案2】:

      你需要在lv = (ListView) findViewById(R.id.list);之前调用setContentView(...)

      setContentView(...) 为您的布局扩展 XML 文件。如果你不这样做,那么lv 将为空。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-03
        • 1970-01-01
        • 2013-04-05
        • 2018-12-10
        • 2013-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多