Question

I'm getting a fatal exception on the line final ViewPager mPager = (ViewPager) findViewById(R.id.view_pager);

however I cannot understand why this is happening. I have declated the viewPager in XML with the correct ID (shown below) however the app still crashes when attempting to istantiate it.

Any suggesions are greatly appreciated.

LogCat:

02-17 11:54:22.547: E/AndroidRuntime(13618): FATAL EXCEPTION: main
02-17 11:54:22.547: E/AndroidRuntime(13618): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.idg.omv/com.idg.omv.ui.phone.SAXParserAsyncTaskActivity}: java.lang.NullPointerException
02-17 11:54:22.547: E/AndroidRuntime(13618):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2232)
02-17 11:54:22.547: E/AndroidRuntime(13618):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
02-17 11:54:22.547: E/AndroidRuntime(13618):    at android.app.ActivityThread.access$600(ActivityThread.java:156)
02-17 11:54:22.547: E/AndroidRuntime(13618):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
02-17 11:54:22.547: E/AndroidRuntime(13618):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 11:54:22.547: E/AndroidRuntime(13618):    at android.os.Looper.loop(Looper.java:153)
02-17 11:54:22.547: E/AndroidRuntime(13618):    at android.app.ActivityThread.main(ActivityThread.java:5297)
02-17 11:54:22.547: E/AndroidRuntime(13618):    at java.lang.reflect.Method.invokeNative(Native Method)
02-17 11:54:22.547: E/AndroidRuntime(13618):    at java.lang.reflect.Method.invoke(Method.java:511)
02-17 11:54:22.547: E/AndroidRuntime(13618):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
02-17 11:54:22.547: E/AndroidRuntime(13618):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
02-17 11:54:22.547: E/AndroidRuntime(13618):    at dalvik.system.NativeStart.main(Native Method)
02-17 11:54:22.547: E/AndroidRuntime(13618): Caused by: java.lang.NullPointerException
02-17 11:54:22.547: E/AndroidRuntime(13618):    at android.app.Activity.findViewById(Activity.java:1852)
02-17 11:54:22.547: E/AndroidRuntime(13618):    at com.idg.omv.ui.phone.SAXParserAsyncTaskActivity.<init>(SAXParserAsyncTaskActivity.java:67)

XML

<?xml version="1.0" encoding="UTF-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/content_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:focusable="true"
        android:orientation="vertical" >
    </LinearLayout>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="220dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/darkgrey"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:textStyle="bold" />

    <ListView
        android:id="@+id/videosListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:id="@+id/rl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#AAFFFFFF" >
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="50dip"
        android:layout_alignParentBottom="true" >

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />

        <ImageButton
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="15dp"
            android:focusable="false"
            android:src="@drawable/scroll_lt_arrow" />

        <ImageButton
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="15dp"
            android:focusable="false"
            android:src="@drawable/scroll_rt_arrow" />
    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

Java:

public class SAXParserAsyncTaskActivity extends Activity implements
OnClickListener, OnItemClickListener {

    ListView listView;
    List<Cmd> videos = new ArrayList<Cmd>();

    CustomListViewAdapter listViewAdapter;

    static final String URL = "http://exampleurl.com";
    public static final String LIBRARY = "Library";
    private ActionBarDrawerToggle actionBarDrawerToggle;
    public static final String API_KEY = "AIzaSyC0Te2pyooXzuyLaE6_SsFlITKCwjj55fI";
    public static final String VIDEO_ID = "o7VVHhK9zf0";
    private int mCurrentTabPosition = NO_CURRENT_POSITION;
    private static final int NO_CURRENT_POSITION = -1;
    private DrawerLayout drawerLayout;
    private ListView drawerListView;
    private String[] drawerListViewItems;
     ViewPager mPager = (ViewPager) findViewById(R.id.view_pager);
Was it helpful?

Solution

Declare the ViewPager object before onCreate() method and initialize that object inside the onCreate() method as follows...

ViewPager mPager; 

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

    mPager = (ViewPager) findViewById(R.id.view_pager);
}

OTHER TIPS

You're calling findViewById() too early in the activity lifecycle. Call it in onCreate() after setContentView().

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