yakusha
yakusha

Reputation: 815

Unable to start activity ComponentInfo...NullPointerException

I am trying to populate a single listview with a textview but i am getting the following error on line 60 that is

    listView.setAdapter(adapter);                                

Here is my complete log.

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

This is my List class

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



}

Anyone know why my adapter is null

Update : This is my listview from my xml file

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

Upvotes: 3

Views: 2380

Answers (2)

Kameswari
Kameswari

Reputation: 758

This error is seen when the id in the activity xml is different to that referred to in the java class.

Change the below line

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

to

android:id="@+id/list"

in your XML file.

Upvotes: 1

Ted Hopp
Ted Hopp

Reputation: 234807

It's not your adapter that's null; it's listView. (A null adapter would not generate an exception immediately at that line.) Your layout—layout/urgence.xml— does not define a view with id list. Fix your layout and that line should then work.

EDIT Based on your update, the problem is that you are trying to find a view with id R.id.list and you should be trying to find a view with id android.R.id.list. R and android.R are two different things.

Alternatively, you can change your layout to use your own id/list:

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

Upvotes: 3

Related Questions