2013-04-16 203 views
1

我試圖填充一個列表視圖與一個TextView,但我得到的第60行是無法啓動活動ComponentInfo ... NullPointerException異常

listView.setAdapter(adapter);         

這裏是我完整的日誌下面的錯誤。

04-16 10:51:38.953: E/AndroidRuntime(3068): FATAL EXCEPTION: main 
04-16 10:51:38.953: E/AndroidRuntime(3068): java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.lang.NullPointerException 
04-16 10:51:38.953: E/AndroidRuntime(3068):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
04-16 10:51:38.953: E/AndroidRuntime(3068):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
04-16 10:51:38.953: E/AndroidRuntime(3068):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 
04-16 10:51:38.953: E/AndroidRuntime(3068):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-16 10:51:38.953: E/AndroidRuntime(3068):  at android.os.Looper.loop(Looper.java:130) 
04-16 10:51:38.953: E/AndroidRuntime(3068):  at android.app.ActivityThread.main(ActivityThread.java:3687) 
04-16 10:51:38.953: E/AndroidRuntime(3068):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-16 10:51:38.953: E/AndroidRuntime(3068):  at java.lang.reflect.Method.invoke(Method.java:507) 
04-16 10:51:38.953: E/AndroidRuntime(3068):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) 
04-16 10:51:38.953: E/AndroidRuntime(3068):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
04-16 10:51:38.953: E/AndroidRuntime(3068):  at dalvik.system.NativeStart.main(Native Method) 
04-16 10:51:38.953: E/AndroidRuntime(3068): Caused by: java.lang.NullPointerException 
04-16 10:51:38.953: E/AndroidRuntime(3068):  at s..onCreate(Urgence.java:60) 
04-16 10:51:38.953: E/AndroidRuntime(3068):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
04-16 10:51:38.953: E/AndroidRuntime(3068):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 
04-16 10:51:38.953: E/AndroidRuntime(3068):  ... 11 more 

這是我的List類

public class ListClass extends Activity 
{ 



    // Array of strings storing country names 
    String[] countries = new String[] { 
      "Services opposition", "Service Carte bancaire a l'etranger" 
    }; 

    // Array of integers points to images stored in /res/drawable-ldpi/ 
    int[] flags = new int[]{ 
      R.drawable.arrow, 
      R.drawable.arrow 
    }; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.urgence); 

     // Each row in the list stores country name, currency and flag 
     List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();   

     for(int i=0;i<2;i++){ 
      HashMap<String, String> hm = new HashMap<String,String>(); 
      hm.put("txt", countries[i]); 
      hm.put("flag", Integer.toString(flags[i]));    
      aList.add(hm);   
     } 

     // Keys used in Hashmap 
     String[] from = { "txt","flag"}; 

     // Ids of views in listview_layout 
     int[] to = { R.id.single_text,R.id.single_image};   

     // Instantiating an adapter to store each items 
     // R.layout.listview_layout defines the layout of each item 
     SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to); 

     // Getting a reference to listview of main.xml layout file 
     ListView listView = (ListView) findViewById(R.id.list); 

     // Setting the adapter to the listView 
     listView.setAdapter(adapter);         
    } 



} 

任何人都知道,爲什麼我的適配器是空

更新: 這是我的列表視圖從我的XML文件

<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 
+1

R.layout.urgence後這個文件也 – Triode

+0

可您發佈'SimpleAdapter'代碼? – Jaguar

+0

爲什麼你convering Integer.toString(標誌[1])..? –

回答

1

此錯誤。

更改以下行

android:id="@android:id/list" 

android:id="@+id/list" 

在XML文件中。

3

這不是你的適配器,這是null;它是listView。 (空adapter不會在該行立即產生一個例外。)您的佈局— layout/urgence.xml —沒有定義ID爲list的視圖。修復你的佈局,然後該行應該工作。

編輯根據您的更新,問題是,你正在努力尋找與ID R.id.list一個觀點,你應該試圖找到ID android.R.id.list的視圖。 Randroid.R是兩回事。

或者,你可以改變你的佈局使用自己id/list:當在活動XML的ID是不同的,以在Java類稱作看到

<ListView 
    android:id="@+id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 
+0

我更新了我的問題,我發佈列表視圖 – yakusha

+0

@yakusha) - 我更新了我的答案。 –

相關問題