The initial activity from the fragment raises a NullPointerException

I am stuck here with the problem of starting activity from a button inside a fragment. Here is my code:

ViewPagerAdapter.java

import java.util.List; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; public class ViewPagerAdapter extends FragmentPagerAdapter { private List<Fragment> fragments; public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments) { super(fm); this.fragments = fragments; } @Override public int getCount() { return this.fragments.size(); } @Override public Fragment getItem(int position) { return this.fragments.get(position); } } 

ViewPagerActivity.java

 import java.util.List; import java.util.Vector; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.view.ViewPager; import android.util.Log; import com.package.here.Class1Overview; import com.package.here.Class1Overview; import com.package.here.Class1Overview; public class ViewPagerActivity extends FragmentActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.viewpager); ViewPager pager = (ViewPager)super.findViewById(R.id.my_viewpager); pager.setAdapter(initialisePaging()); } private ViewPagerAdapter initialisePaging() { List<Fragment> fragments = new Vector<Fragment>(); fragments.add(Fragment.instantiate(this, Class1Overview.class.getName())); fragments.add(Fragment.instantiate(this, Class2Overview.class.getName())); fragments.add(Fragment.instantiate(this, Class3Overview.class.getName())); return new ViewPagerAdapter(super.getSupportFragmentManager(), fragments); } } 

Class1Overview.java (here is the button)

 import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.LinearLayout; import com.package.here.R; public class FuelOverview extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (container == null) { return null; } View v = (LinearLayout)inflater.inflate(R.layout.class_one_layout, container, false); View AddBtn = v.findViewById(R.id.btn_add); ((Button) AddBtn).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(getActivity(), ActivityToStart.class); startActivity(intent); } }); return v; } } 

And this is my LogCat :

 01-05 19:39:00.913: E/AndroidRuntime(10557): Uncaught handler: thread main exiting due to uncaught exception 01-05 19:39:01.034: E/AndroidRuntime(10557): java.lang.RuntimeException: Unable to pause activity {com.package.here/com.package.here.ViewPagerActivity}: java.lang.NullPointerException 01-05 19:39:01.034: E/AndroidRuntime(10557): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3162) 01-05 19:39:01.034: E/AndroidRuntime(10557): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3119) 01-05 19:39:01.034: E/AndroidRuntime(10557): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3102) 01-05 19:39:01.034: E/AndroidRuntime(10557): at android.app.ActivityThread.access$2400(ActivityThread.java:119) 01-05 19:39:01.034: E/AndroidRuntime(10557): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1870) 01-05 19:39:01.034: E/AndroidRuntime(10557): at android.os.Handler.dispatchMessage(Handler.java:99) 01-05 19:39:01.034: E/AndroidRuntime(10557): at android.os.Looper.loop(Looper.java:123) 01-05 19:39:01.034: E/AndroidRuntime(10557): at android.app.ActivityThread.main(ActivityThread.java:4363) 01-05 19:39:01.034: E/AndroidRuntime(10557): at java.lang.reflect.Method.invokeNative(Native Method) 01-05 19:39:01.034: E/AndroidRuntime(10557): at java.lang.reflect.Method.invoke(Method.java:521) 01-05 19:39:01.034: E/AndroidRuntime(10557): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 01-05 19:39:01.034: E/AndroidRuntime(10557): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 01-05 19:39:01.034: E/AndroidRuntime(10557): at dalvik.system.NativeStart.main(Native Method) 01-05 19:39:01.034: E/AndroidRuntime(10557): Caused by: java.lang.NullPointerException 01-05 19:39:01.034: E/AndroidRuntime(10557): at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1576) 01-05 19:39:01.034: E/AndroidRuntime(10557): at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1617) 01-05 19:39:01.034: E/AndroidRuntime(10557): at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:481) 01-05 19:39:01.034: E/AndroidRuntime(10557): at android.app.Activity.performSaveInstanceState(Activity.java:1022) 01-05 19:39:01.034: E/AndroidRuntime(10557): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1180) 01-05 19:39:01.034: E/AndroidRuntime(10557): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3144) 01-05 19:39:01.034: E/AndroidRuntime(10557): ... 12 more 01-05 19:39:01.423: I/dalvikvm(10557): threadid=7: reacting to signal 3 

ViewPager itself is working fine. The error seems to pause the FragmentActivity, but I can't figure it out. The only thing I found was a google post http://code.google.com/p/android/issues/detail?id=19917

I also got a Google example code to make sure I mostly use the same technique ...

Edit: To make sure ClickListener works, I tried Toast in the onClick method - it works fine.

+56
android android-activity android-fragments android-viewpager
Jan 05 '12 at 18:51
source share
4 answers

This is a known issue, and I am shocked to find that no one has filed an error on the public error tracker for this.

Your problem is related to this:

 if (f.mSavedViewState != null) { if (result == null) { result = new Bundle(); } result.putSparseParcelableArray( FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState); } if (!f.mUserVisibleHint) { // Only add this if it not the default value result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint); } 

You should notice that if result is null that goes into this code and the first if does not run, you will get a NullPointerException if we introduce the second if .

He should read:

 if (f.mSavedViewState != null) { if (result == null) { result = new Bundle(); } result.putSparseParcelableArray( FragmentManagerImpl.VIEW_STATE_TAG, f.mSavedViewState); } if (!f.mUserVisibleHint) { if (result == null) { result = new Bundle(); } // Only add this if it not the default value result.putBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG, f.mUserVisibleHint); } 

I sent a patch for this week or two ago: https://android-review.googlesource.com/31261

+48
Jan 26 '12 at 16:29
source share

Quick fix

 class MyFragment extends Fragment { @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); setUserVisibleHint(true); } 

And wait for the update in safe mode :)

+62
Feb 25 '12 at 17:39
source share

According to the post here , you should use the FragmentStatePagerAdapter, not the FragmentPagerAdapter. I had the same problem that you described and that this switch worked for me.

+32
May 03 '12 at 19:03
source share

I had the same problem - the problem was caused for me when I added a new fragment to my project with an empty layout. It appears that if nothing is stored in the package during the Fragment.onSaveInstanceState(Bundle outState) , it throws a null pointer exception in FragmentManagerImpl.saveFragmentBasicState .

Adding some views to my layouts fixed the problem (after the hours spent my head on the wall). Using the support library v4 v4 version for Android.

Also, when using ListView as the root for the fragment layout, if there are no elements in the list, you will also fail. In the fragment class, override onSaveInstanceState as this to put some data in the package:

 @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("DO NOT CRASH", "OK"); } 
+4
Sep 05 '12 at 4:29
source share



All Articles