5
Attempt to invoke virtual method 'android.os.Handler android.support.v4.app.FragmentHostCallback.getHandler()' on a null object reference
  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1588)
  at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330)
  at android.support.v4.app.FragmentActivity.onResume(FragmentActivity.java:441)
  at com.app.rare10.activities.ViewAllMemberActivity.onResume(ViewAllMemberActivity.java:192)
  at com.app.rare10.activities.ViewAllMemberActivity$1.onReceive(ViewAllMemberActivity.java:74)
  at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297)
  at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)
  at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:135)
  at android.app.ActivityThread.main(ActivityThread.java:5254)
  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:903)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698

I have used push notifications in my project and I'm getting this error above whenever I click on my notification. Help?

knocte
  • 16,941
  • 11
  • 79
  • 125
Bhoomi Zalavadiya
  • 689
  • 12
  • 26

7 Answers7

2

I used a boolean to prevent whenever activity has Destroyed and has resumed.

boolean hasStop;
.
.
.
.
//here is when I need to prevent
if(!hasStop)
   //Statement
.
.
.
@Override
protected void onDestroy() {
    super.onDestroy();
    hasStop=true;
}

@Override
protected void onResume() { 
    super.onResume();
    hasStop=false;
}
  • you can get even more precise info when you call activity.isDestroyed() - Returns true if the final onDestroy() call has been made on the Activity, so this instance is now dead. PS: API 17 required – vanomart Jan 19 '17 at 11:35
0

I Think That You Should Implement YourFragment.OnFragmentInteractionListener Inside your Activity , Because the Container of Fragment need a context and a Handler , The Context Is your Activity and The Handler is The Interface And That's resolve this issue .

0

If related to https://code.google.com/p/android/issues/detail?id=216157

I work around this way:
@Override
  protected void onDestroy() {
        Log.d(TAG, "onDestroy");
          try {
            super.onDestroy();
        } catch (NullPointerException npe) {
            Log.e(TAG, "NPE: Bug workaround");
        }
0

Just check if your overriden methods have the correct super methods. I mean that

 @Override
public void onPause() {
    super.onPause();

}

should have super.onPause(); only not some other method.

user4204805
  • 123
  • 2
  • 14
0

Just in case any future people have this issue.... if you are calling onDestroy() in your onStop(), it will throw the cannot destroy activity error

@Override
protected void onStop() {
    super.onStop();
    onDestroy(); //Don't call this!!!
}

so i.e. Don't call on onDestroy() in onStop()

Pythogen
  • 591
  • 5
  • 25
0

I had same problem. The cause was that I created PagerAdapter that holds link to Context inside of retained Fragment. And the solution - is reset Adapter's link to Context after Fragment's view recreation.

Adapter Class:

class TrackingDaysAdapter : PagerAdapter() {

var context: Context? = null

var trackingDays: List<TrackingDay> = emptyList()
    set(value) {
        field = value
        notifyDataSetChanged()
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val trackingDayViewImpl = TrackingDayViewImpl(context!!, trackingDays[position].date)
        container.addView(trackingDayViewImpl)
        return trackingDayViewImpl
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any?) {
        container.removeView(`object` as View)
    }

    override fun isViewFromObject(view: View?, `object`: Any?) = view == `object`

    override fun getCount() = trackingDays.size
}

And Fragment Class:

class TrackingsFragment : BackStackFragment<TrackingsView, TrackingsPresenter>(), TrackingsView {
@Inject lateinit var adapter: TrackingDaysAdapter

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    adapter.context = context
}

}

0
Not a great solution. But You use this until google fixes
@Override
public void finishUpdate(ViewGroup container) {
    try{
        super.finishUpdate(container);
    } catch (NullPointerException nullPointerException){
        System.out.println(" NPE in FragmentPagerAdapter.finishUpdate");
    }
}
Prakhs
  • 579
  • 1
  • 3
  • 8