1

I have a bug that seems crazy???

I am starting a fragment from my activity.

In the fragment I call startActivityForResult, trying to get image from gallery.

Intent intent = new Intent();
intent.setType("image/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Image"), SELECT_IMAGE);

When I come back from gallery, my application crashed. I followed the problem and I saw that when I return to my activity I see that the onCreate() of my fragment was launched again. I don't understand why my fragment was created another time, and my webservice object was deleted.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);   
    // TODO: Bug mWebServicesManager.getEditingBlogItem() return null sometimes, only when i open first time get image from galerie

    Log.i("EditImageSectionFragment","mWebServicesManager.getEditingBlogItem() ="+mWebServicesManager.getEditingBlogItem());    
    mSection = (ImagesSection) mWebServicesManager.getEditingBlogItem().getSections().get(mSectionPosition);
}

When i run tests on HTC mobile, all goes ok and I don't get this bug. But when I run the app on my Nexus S (android 4.1), it always crashes when I try to get images.

I want to know why my fragment was created again? And why my webServiceManager object was deleted?

logcat:

10-02 12:34:32.609: E/AndroidRuntime(32344): FATAL EXCEPTION: main
10-02 12:34:32.609: E/AndroidRuntime(32344): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.overblog/com.overblog.activities.EditActivity}: java.lang.NullPointerException
10-02 12:34:32.609: E/AndroidRuntime(32344):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-02 12:34:32.609: E/AndroidRuntime(32344):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-02 12:34:32.609: E/AndroidRuntime(32344):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-02 12:34:32.609: E/AndroidRuntime(32344):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-02 12:34:32.609: E/AndroidRuntime(32344):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-02 12:34:32.609: E/AndroidRuntime(32344):    at android.os.Looper.loop(Looper.java:137)
10-02 12:34:32.609: E/AndroidRuntime(32344):    at android.app.ActivityThread.main(ActivityThread.java:4745)
10-02 12:34:32.609: E/AndroidRuntime(32344):    at java.lang.reflect.Method.invokeNative(Native Method)
10-02 12:34:32.609: E/AndroidRuntime(32344):    at java.lang.reflect.Method.invoke(Method.java:511)
10-02 12:34:32.609: E/AndroidRuntime(32344):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-02 12:34:32.609: E/AndroidRuntime(32344):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-02 12:34:32.609: E/AndroidRuntime(32344):    at dalvik.system.NativeStart.main(Native Method)
10-02 12:34:32.609: E/AndroidRuntime(32344): Caused by: java.lang.NullPointerException
10-02 12:34:32.609: E/AndroidRuntime(32344):    at com.overblog.fragments.EditImageSectionFragment.onCreateView(EditImageSectionFragment.java:174)
10-02 12:34:32.609: E/AndroidRuntime(32344):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:870)
10-02 12:34:32.609: E/AndroidRuntime(32344):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
10-02 12:34:32.609: E/AndroidRuntime(32344):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
10-02 12:34:32.609: E/AndroidRuntime(32344):    at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1810)
10-02 12:34:32.609: E/AndroidRuntime(32344):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:501)
10-02 12:34:32.609: E/AndroidRuntime(32344):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
10-02 12:34:32.609: E/AndroidRuntime(32344):    at android.app.Activity.performStart(Activity.java:5018)
10-02 12:34:32.609: E/AndroidRuntime(32344):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
10-02 12:34:32.609: E/AndroidRuntime(32344):    ... 11 more
haythem souissi
  • 3,263
  • 7
  • 50
  • 77
  • i think you should use onActivityCreated() method instead of onCreate() because in onCreate() your activity not created yet. – Rishabh Agrawal Oct 02 '12 at 10:29
  • i tryed onActivityCreated(), i doesn't work – haythem souissi Oct 02 '12 at 10:41
  • please check the question, i have added the logcat. Thanks – haythem souissi Oct 02 '12 at 10:43
  • Request Parent Activity to start another activity, and get results, then find the fragment, and pass the results to it. Your activity might get destroyed while you are in gallery app, hence, remember to save and restore activity state. – S.D. Oct 02 '12 at 10:54
  • no i added log in onDestroy() of activity, and i verified that my activity was not destroyed. – haythem souissi Oct 02 '12 at 11:03
  • my singleton object become NULL when reentering to my activity. i get this subject: http://stackoverflow.com/questions/7503997/when-why-does-my-java-singleton-instance-get-destroyed i thing they are talking of the same problem i have – haythem souissi Oct 03 '12 at 09:10

0 Answers0