无法启动活动ComponentInfo...NullPointerException

3

我正在尝试用一个TextView填充一个单一的ListView,但是在第60行出现了以下错误:

    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

这是我的列表类。
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
你能发一下你的SimpleAdapter代码吗? - Jainendra
为什么你要使用 Integer.toString(flags[i])..? - Mehul Ranpara
1
没有简单的适配器代码,伙计。 - yakusha
没关系,反正只有15分,别在意 :) - Triode
显示剩余2条评论
2个回答

3
你的适配器不是“null”,而是“listView”。(一个空的“adapter”不会立即在该行生成一个异常。)你的布局—“layout/urgence.xml”—没有定义一个id为“list”的视图。修复你的布局,那行代码就会正常工作。
编辑:根据你的更新,问题在于你试图查找id为“R.id.list”的视图,但你应该尝试查找id为“android.R.id.list”的视图。“R”和“android.R”是两个不同的东西。
或者,你可以更改你的布局来使用自己的“id/list”。
<ListView 
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

1
当活动XML中的ID与Java类中引用的ID不同时,会出现此错误。
请更改以下行:
android:id="@android:id/list"

android:id="@+id/list"

在你的 XML 文件中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接