1

I use to work with Activities and I use to take care about my activities when Android kills them (onSaveStateInstance() and onRestoreInstanceState())

Now I'm working with Fragments and sometimes (a few) get NullPointerException when I get back to the Fragment from background. I couldn't get the log because it happened when I was on the train or walking on the street.

What should I take of? how do I handle onSaveStateInstance()? I have a FragmentActivity with 4 tabs that I add to a hashMap.

I've read posts in StackOverflow but I can't understand because they ask for recreate Fragment when rotated screen or other things.

Thanks in advance.


EDIT

03-01 11:58:52.410: E/AndroidRuntime(31948): FATAL EXCEPTION: main 03-01 11:58:52.410: E/AndroidRuntime(31948): java.lang.RuntimeException: Unable to start activity ComponentInfo{creact.placard/creact.placard.MainActivity}: java.lang.NullPointerException 03-01 11:58:52.410: E/AndroidRuntime(31948): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081) 03-01 11:58:52.410: E/AndroidRuntime(31948): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106) 03-01 11:58:52.410: E/AndroidRuntime(31948): at android.app.ActivityThread.access$700(ActivityThread.java:134) 03-01 11:58:52.410: E/AndroidRuntime(31948): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1217) 03-01 11:58:52.410: E/AndroidRuntime(31948): at android.os.Handler.dispatchMessage(Handler.java:99) 03-01 11:58:52.410: E/AndroidRuntime(31948): at android.os.Looper.loop(Looper.java:137) 03-01 11:58:52.410: E/AndroidRuntime(31948): at android.app.ActivityThread.main(ActivityThread.java:4856) 03-01 11:58:52.410: E/AndroidRuntime(31948): at java.lang.reflect.Method.invokeNative(Native Method) 03-01 11:58:52.410: E/AndroidRuntime(31948): at java.lang.reflect.Method.invoke(Method.java:511) 03-01 11:58:52.410: E/AndroidRuntime(31948): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007) 03-01 11:58:52.410: E/AndroidRuntime(31948): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) 03-01 11:58:52.410: E/AndroidRuntime(31948): at dalvik.system.NativeStart.main(Native Method) 03-01 11:58:52.410: E/AndroidRuntime(31948): Caused by: java.lang.NullPointerException 03-01 11:58:52.410: E/AndroidRuntime(31948): at creact.placard.MapFragment.onCreateView(MapFragment.java:59) 03-01 11:58:52.410: E/AndroidRuntime(31948): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500) 03-01 11:58:52.410: E/AndroidRuntime(31948): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927) 03-01 11:58:52.410: E/AndroidRuntime(31948): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 03-01 11:58:52.410: E/AndroidRuntime(31948): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 03-01 11:58:52.410: E/AndroidRuntime(31948): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467) 03-01 11:58:52.410: E/AndroidRuntime(31948): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570) 03-01 11:58:52.410: E/AndroidRuntime(31948): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178) 03-01 11:58:52.410: E/AndroidRuntime(31948): at android.app.Activity.performStart(Activity.java:5057) 03-01 11:58:52.410: E/AndroidRuntime(31948): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2054) 03-01 11:58:52.410: E/AndroidRuntime(31948): ... 11 more

and after that it keeps doing 2 or 3 HttpGet and then stops.

on that line I'm starting to define my buttons or imageviews:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    context = getActivity();
    if (container == null) {
        return null;
    }
    if (layout != null) {
            ViewGroup parent = (ViewGroup) layout.getParent();
            if (parent != null)
                parent.removeView(layout);
     }
     try {
         layout = inflater.inflate(R.layout.mappa, container, false);
     } catch (Exception e) {
         e.printStackTrace();
     }
     mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();

     mMap.getUiSettings().setZoomControlsEnabled(true);
     negoziTab = (ImageView)layout.findViewById(R.id.negoziTab); // LINE 59 WHEN CAUSES ERROR
     eventiTab = (ImageView)layout.findViewById(R.id.eventiTab);
     promoTab = (ImageView)layout.findViewById(R.id.promoTab);
     tuttiTab = (ImageView)layout.findViewById(R.id.tuttiim);

And this is the ERROR LOG on the TRY CATCH on LINE 51:

03-01 12:15:50.919: W/System.err(2849): android.view.InflateException: Binary XML file line #86: Error inflating class fragment 03-01 12:15:50.929: W/System.err(2849): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 03-01 12:15:50.929: W/System.err(2849): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 03-01 12:15:50.929: W/System.err(2849): at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 03-01 12:15:50.929: W/System.err(2849): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 03-01 12:15:50.929: W/System.err(2849): at creact.placard.MapFragment.onCreateView(MapFragment.java:51) 03-01 12:15:50.929: W/System.err(2849): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500) 03-01 12:15:50.929: W/System.err(2849): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927) 03-01 12:15:50.929: W/System.err(2849): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 03-01 12:15:50.929: W/System.err(2849): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 03-01 12:15:50.929: W/System.err(2849): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467) 03-01 12:15:50.929: W/System.err(2849): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570) 03-01 12:15:50.929: W/System.err(2849): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178) 03-01 12:15:50.929: W/System.err(2849): at android.app.Activity.performStart(Activity.java:5057) 03-01 12:15:50.929: W/System.err(2849): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2054) 03-01 12:15:50.929: W/System.err(2849): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106) 03-01 12:15:50.929: W/System.err(2849): at android.app.ActivityThread.access$700(ActivityThread.java:134) 03-01 12:15:50.929: W/System.err(2849): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1217) 03-01 12:15:50.929: W/System.err(2849): at android.os.Handler.dispatchMessage(Handler.java:99) 03-01 12:15:50.929: W/System.err(2849): at android.os.Looper.loop(Looper.java:137) 03-01 12:15:50.929: W/System.err(2849): at android.app.ActivityThread.main(ActivityThread.java:4856) 03-01 12:15:50.929: W/System.err(2849): at java.lang.reflect.Method.invokeNative(Native Method) 03-01 12:15:50.929: W/System.err(2849): at java.lang.reflect.Method.invoke(Method.java:511) 03-01 12:15:50.929: W/System.err(2849): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007) 03-01 12:15:50.939: W/System.err(2849): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) 03-01 12:15:50.939: W/System.err(2849): at dalvik.system.NativeStart.main(Native Method) 03-01 12:15:50.939: W/System.err(2849): Caused by: java.lang.IllegalArgumentException: Binary XML file line #86: Duplicate id 0x7f060057, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment 03-01 12:15:50.939: W/System.err(2849): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:296) 03-01 12:15:50.939: W/System.err(2849): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676) 03-01 12:15:50.939: W/System.err(2849): ... 24 more

It's a SupportMapFragment what there's inside.

Thanks again.



DEFINITELY EDIT

My problem is on my layout when inflating. What is happening is the famous issue of

Duplicate ID, tag null, or parent id with another fragment for com.google.android.gms.maps.MapFragment

and as it says on edits:

Edit: There were some adverse effects, such as when exiting the application and starting it again. Since the application isn't necessarily completely shut down (but just put to sleep in the background), the previous code i submitted would fail upon restarting the application. I've updated the code to something that works for me, both going in & out of the map and exiting and restarting the application, I'm not too happy with the try-catch bit, but it seem to work well enough.

briefly: when I try to inflate, I can't because of that issue, but my layout is null.

How could I handle it??

Community
  • 1
  • 1
Rafael Ruiz Muñoz
  • 5,333
  • 6
  • 46
  • 92

3 Answers3

0

It seems to me somewhere you call getActivity(). Put a null pointer check before it like this:

//some stuff

if(getActivity()!=null){
    //...
}

//other stuff
Blo
  • 11,903
  • 5
  • 45
  • 99
user3057944
  • 701
  • 1
  • 8
  • 19
  • I got again this bug while I was having a beer... lol. But I think you're right, when calling getActivity on activity destroyed could happen. I installed a bug reporter and I will try to do it. As soon as I get the bug, if that solves my problem I will check my answer as GOOD, ok? Thanks ;) – Rafael Ruiz Muñoz Mar 01 '14 at 00:04
  • I had a problem like this when I use fragments, when the app was reopened. I was told, it is because the fragment gets the context from the activity, and when the app goes to background, the fragment is detached from its activity, and that's why the context (getActivity()) returns null. So surround your code with null-check solved it. Please let me know if it solved your problem. Good luck! – user3057944 Mar 01 '14 at 09:27
0

I think the problem is that you are using activity in onCreateView() for MapFragment.

You should move this part of code to onActivityCreated() because it's called when the activity's onCreate() method has returned.

If you are initializing MapFragment inside onCreate(), it may not been created yet and it'll return null when you call it.

You can try it like below.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
   super.onActivityCreated(savedInstanceState);

   mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();

   mMap.getUiSettings().setZoomControlsEnabled(true);

}
Orhan Obut
  • 8,756
  • 5
  • 32
  • 42
0
if(yourfragment !==null)
{
   getActivity().getFragmentManager().beginTransaction().remove(yourfragment).commit();
                                    getFragmentManager().popBackStack();

  }
}

used this in onBackPressed. and it solved my Problem.

Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
Sarvan
  • 61
  • 8