Reputation: 2010
So I am messing around with using ViewPager
in tandem with Navigation Drawer
. I have the Navigation Drawer
working.
However, attempting to call viewPager.setAdapter(pagerAdapter)
is throwing a NullPointerException
.
I am checking both viewPager
and pagerAdapter
for null, but here is what happens when I attempt to Log
calls to their respective toString()
methods:
03-11 12:31:54.156: D/My Debug Bitches(2893): android.support.v4.view.ViewPager{b1e75710 VFED.... ......I. 0,0-0,0 #7f090000 app:id/pager}
03-11 12:31:54.156: D/MyDebugBitches(2893): com.sweidenkopf.discretesuite.DataSetViewActivity$DataSetViewPagerAdapter@b1e7b4c8
Notice how the viewPager
toString()
returns some weird android.support.v4
information? I think that may have something to do with this issue.
Here is the initialization of my viewPager
and pagerAdapter
. I marked the line that is causing the exception
@Override
protected void onCreate(Bundle args) {
super.onCreate(args);
setContentView(R.layout.activity_data_set_view);
super.onCreateDrawer();
//initialize the pager adapter, giving it access to the fragment manager
pagerAdapter = new DataSetViewPagerAdapter(super.getSupportFragmentManager());
final ActionBar actionBar = getActionBar();
// Specify that we will be displaying tabs in the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//initialize the view pager!
viewPager = (ViewPager) findViewById(R.id.pager);
Log.d("My Debug Bitches", viewPager.toString());
Log.d("MyDebugBitches", pagerAdapter.toString());
//here we give the pager its adapter
viewPager.setAdapter(pagerAdapter);//<-- this is causing exception
}
Here is the layout xml R.layout.activity_data_set_view
that is being used by this activity.
<?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" >
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view. -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
Here is the logcat:
03-11 12:31:54.476: E/AndroidRuntime(2893): FATAL EXCEPTION: main
03-11 12:31:54.476: E/AndroidRuntime(2893): Process: com.sweidenkopf.discretesuite, PID: 2893
03-11 12:31:54.476: E/AndroidRuntime(2893): java.lang.NullPointerException
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.support.v4.view.ViewPager.populate(ViewPager.java:982)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.View.measure(View.java:16497)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.View.measure(View.java:16497)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.View.measure(View.java:16497)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-11 12:31:54.476: E/AndroidRuntime(2893): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.View.measure(View.java:16497)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-11 12:31:54.476: E/AndroidRuntime(2893): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.View.measure(View.java:16497)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.Choreographer.doFrame(Choreographer.java:544)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.os.Handler.handleCallback(Handler.java:733)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.os.Handler.dispatchMessage(Handler.java:95)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.os.Looper.loop(Looper.java:136)
03-11 12:31:54.476: E/AndroidRuntime(2893): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-11 12:31:54.476: E/AndroidRuntime(2893): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 12:31:54.476: E/AndroidRuntime(2893): at java.lang.reflect.Method.invoke(Method.java:515)
03-11 12:31:54.476: E/AndroidRuntime(2893): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-11 12:31:54.476: E/AndroidRuntime(2893): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-11 12:31:54.476: E/AndroidRuntime(2893): at dalvik.system.NativeStart.main(Native Method)
Edit:
I changed my null
checking to this:
//initialize the view pager!
viewPager = (ViewPager) findViewById(R.id.pager);
if(viewPager == null){
Log.d("My Debug Bitches", "viewPager is null");
}
if(pagerAdapter == null){
Log.d("MyDebugBitches", "pagerAdapter is null");
}
Neither of the Log
calls were triggered, which makes me think neither the viewPager
nor the pagerAdapter
are null
and here is my pager adapter:
private class DataSetViewPagerAdapter extends FragmentPagerAdapter{
public DataSetViewPagerAdapter(FragmentManager fm){
super(fm);
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
/**
* This method must return the number of tabs, which is the length of the
* view_pager_tabs string array (it contains the names of all of the tabs)
*/
@Override
public int getCount() {
return getResources().getStringArray(R.array.view_pager_tabs).length;
}
/**
* returns the element with in the view_pager_tabs array that corresponds with
* the given position
*/
@Override
public CharSequence getPageTitle(int position) {
return getResources().getStringArray(R.array.view_pager_tabs)[position];
}
}
Upvotes: 1
Views: 1731
Reputation: 157437
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
your problem is here. Your getItem
can't return null. Internally the ViewPager
creates some transaction for the fragment you return in getItem
, and you can't add a null fragment to a transaction
Upvotes: 3