2011-09-14 5 views
0

:ArrayList를이 오류가 계속

활동 ComponentInfo {com.Nostradamus/com.Nostradamus.Contactenlijst} 시작할 수 없습니다 : java.lang.NullPointerException이

을 그리고 그렇게하지를 내가 뭘 잘못했는지 알아. 그것은 마지막 코드와 관련이 있습니다 : lv.setAdapter (new SimpleAdapter (this, mylist, R.layout.list_item, from, to));

내 자바 코드 :

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)); 

} 

} 

그리고이 내 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> 
,451,515,

및을 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> 

답변

4

당신이

setContentView(R.layout.yourlayout); 

그렇게하지 않고 당신이 NullPointerException이 점점 왜 당신의 목록보기를 사용하는 레이아웃을 추가하여 레이아웃을 설정하지 않았다.

super.onCreate(savedInstanceState); 

후이 위의 라인을 추가하거나 사용 목록보기 개체

0

전에 당신은 당신의 레이아웃 XML 파일을 팽창 setContentView(...)

setContentView(...)lv = (ListView) findViewById(R.id.list); 전에 호출해야합니다. 이 작업을 수행하지 않으면 lv이 null이됩니다.

관련 문제