-3

I have an activity where it will display a ListView in a one of the 5 tabs. This is the MainActivity class:

public class MainActivity extends ActionBarActivity {

    Toolbar toolbar;
    ViewPager pager;
    ViewPagerAdapter adapter;
    SlidingTabLayout tabs;
    CharSequence Titles[]={"Home","Courses","Jobs","Me","More"};
    int Numboftabs =5;

    MaterialTabHost tabHost;

    ListView listView;

    String[]me = new String[] {
            "My Profile",
            "My Bookmarks",
            "My Application History"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Creating The Toolbar and setting it as the Toolbar for the activity

        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);


        // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
        adapter =  new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);

        // Assigning ViewPager View and setting the adapter
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);

        // Assigning the Sliding Tab Layout View
        tabs = (SlidingTabLayout) findViewById(R.id.tabs);
        tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

        // Setting Custom Color for the Scroll bar indicator of the Tab View
        tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
            @Override
            public int getIndicatorColor(int position) {
                return getResources().getColor(R.color.tabsScrollColor);
            }
        });

        // Setting the ViewPager For the SlidingTabsLayout
        tabs.setViewPager(pager);


        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, me);

        listView = (ListView)findViewById(R.id.listMe);
        listView.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

This is the activity_main XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <include
        layout="@layout/tool_bar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        />

    <itp231.dba.sit.nyp.com.tab_2.SlidingTabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="2dp"
        android:background="@color/ColorPrimary"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_weight="1">
    </android.support.v4.view.ViewPager>

</LinearLayout>

This is the layout tab_4 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

<ListView
    android:id="@+id/listMe"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:entries="@array/Me"
    />

</RelativeLayout>

When I attempt to run the app, the app crashes and displayed the following errors in the logcat:

06-30 01:39:24.105    1632-1632/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: itp231.dba.sit.nyp.com.tab_2, PID: 1632
    java.lang.RuntimeException: Unable to start activity ComponentInfo{itp231.dba.sit.nyp.com.tab_2/itp231.dba.sit.nyp.com.tab_2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
            at itp231.dba.sit.nyp.com.tab_2.MainActivity.onCreate(MainActivity.java:76)
            at android.app.Activity.performCreate(Activity.java:5937)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Vee
  • 3
  • 2
  • possible duplicate of [NullPointerException in custom adapter getView](http://stackoverflow.com/questions/10204262/nullpointerexception-in-custom-adapter-getview) – Anand Savjani Jun 30 '15 at 05:31
  • 1
    you use activity_main as your layout. There is no listMe in it. so findViewById(R.id.listMe) returns null. Where do you use the layout with the ListView? – Roi Divon Jun 30 '15 at 05:33
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – almightyGOSU Jun 30 '15 at 05:35
  • maybe this is due to name confliction. You have declared PagerAdapter and ArrayAdapter with same name "adapter". so check which you are using to set in ListView. – Balvinder Singh Jun 30 '15 at 05:39
  • If you are including a layout which having `listview` then you have to declare a `view` and initialise it by `findViewById()` and then use `view.findViewById` to intialise listview. – Pankaj Jun 30 '15 at 05:53
  • Is listview containing xml is your `tool_bar` layout which you have added as include layout in main xml – Pankaj Jun 30 '15 at 06:02

1 Answers1

4

See here:

setContentView(R.layout.activity_main);

you are setting "activity_main" as Layout. But in your Layout xml file there is no ListView with id listMe.

So shift this code to activity_main:

<ListView
    android:id="@+id/listMe"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:entries="@array/Me"
    />

I hope it will work then.

Anand Singh
  • 5,672
  • 2
  • 23
  • 33