Question

I am trying to setup ListView in one of the tabs but getting an error in setAdapter. I am unable to figure out what the problem is , please guide me . Here is the code

public class TabView extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        ListView listview1 = (ListView) findViewById(R.id.list1);
        setContentView(R.layout.tview);
        TabHost th = (TabHost) findViewById(android.R.id.tabhost);
        th.setup();
        List<String> list1strings = new ArrayList<String>();
        list1strings.add("Programming Language");
        list1strings.add("Database");
        list1strings.add("Java technologies");
        list1strings.add("Web Teechnologies");
        list1strings.add("Application/Web Server");
        list1strings.add("operating Systems");
        list1strings.add("PHP framework");
        list1strings.add("PHP CMS");
        listview1.setAdapter(new ArrayAdapter<String>(TabView.this, android.R.layout.simple_list_item_1, list1strings));


        th.addTab(th.newTabSpec("Tab1").setIndicator("Training").setContent(R.id.list1));

        TabSpec specs = th.newTabSpec("Tab2");
        specs.setContent(R.id.tab1);
        specs.setIndicator("Workshops");
        th.addTab(specs);

        specs = th.newTabSpec("Tab 3");
        specs.setContent(R.id.tab1);
        specs.setIndicator("Reach Us");
        th.addTab(specs);

    }

}

Here is the Logcat which shows error in SetAdapter method Which in my code is line 32.

05-13 04:11:54.426: E/AndroidRuntime(1137): java.lang.RuntimeException: Unable to start activity ComponentInfo{in.brainwave.industrialtraining/in.brainwave.industrialtraining.TabView}: java.lang.NullPointerException
05-13 04:11:54.426: E/AndroidRuntime(1137):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-13 04:11:54.426: E/AndroidRuntime(1137):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-13 04:11:54.426: E/AndroidRuntime(1137):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-13 04:11:54.426: E/AndroidRuntime(1137):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-13 04:11:54.426: E/AndroidRuntime(1137):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-13 04:11:54.426: E/AndroidRuntime(1137):     at android.os.Looper.loop(Looper.java:136)
05-13 04:11:54.426: E/AndroidRuntime(1137):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-13 04:11:54.426: E/AndroidRuntime(1137):     at java.lang.reflect.Method.invokeNative(Native Method)
05-13 04:11:54.426: E/AndroidRuntime(1137):     at java.lang.reflect.Method.invoke(Method.java:515)
05-13 04:11:54.426: E/AndroidRuntime(1137):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-13 04:11:54.426: E/AndroidRuntime(1137):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-13 04:11:54.426: E/AndroidRuntime(1137):     at dalvik.system.NativeStart.main(Native Method)
05-13 04:11:54.426: E/AndroidRuntime(1137): Caused by: java.lang.NullPointerException
05-13 04:11:54.426: E/AndroidRuntime(1137):     at in.brainwave.industrialtraining.TabView.onCreate(TabView.java:32)
05-13 04:11:54.426: E/AndroidRuntime(1137):     at android.app.Activity.performCreate(Activity.java:5231)
05-13 04:11:54.426: E/AndroidRuntime(1137):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-13 04:11:54.426: E/AndroidRuntime(1137):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-13 04:11:54.426: E/AndroidRuntime(1137):     ... 11 more

Please guide me .

Was it helpful?

Solution

You need to change the order like

setContentView(R.layout.tview);
ListView listview1 = (ListView) findViewById(R.id.list1);

Reference ListView after setContentView(R.layout.tview);

OTHER TIPS

If you want to access any layout from your xml then first you have to do

setContentView(R.layout.your_view);

After that you can access views like

ListView listview1 = (ListView) findViewById(R.id.list1);

Accessing a view before setContentView will give you nullPointer exception.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top