1

I have an activity(say, MyActivity) that contains fragments, and i am using fragmentTransaction.replace(XX,YY) for changing fragment. After a period of inactivity when the application is resumed it behaves abnormally, and gimme the following exception (I don't know if the activity died in the meantime or what happened).

"java.lang.RuntimeException: Unable to start activity ComponentInfo{com.platinumapps.facedroid/com.platinumapps.facedroid.facedroid}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
    at android.app.ActivityThread.access$500(ActivityThread.java:122)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:132)
    at android.app.ActivityThread.main(ActivityThread.java:4123)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:491)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
    at com.platinumapps.facedroid.MyFragment.<init>(MyFragment.java:90)
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1301)
    at android.app.Fragment.instantiate(Fragment.java:560)
    at android.app.FragmentState.instantiate(Fragment.java:96)
    at android.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1589)
    at android.app.Activity.onCreate(Activity.java:854)
    at com.platinumapps.facedroid.facedroid.onCreate(facedroid.java:78)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
    ... 11 more
java.lang.NullPointerException
    at com.platinumapps.facedroid.MyFragment.<init>(MyFragment.java:90)
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1301)
    at android.app.Fragment.instantiate(Fragment.java:560)
    at android.app.FragmentState.instantiate(Fragment.java:96)
    at android.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1589)
    at android.app.Activity.onCreate(Activity.java:854)
    at com.platinumapps.facedroid.facedroid.onCreate(facedroid.java:78)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
    at android.app.ActivityThread.access$500(ActivityThread.java:122)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:132)
    at android.app.ActivityThread.main(ActivityThread.java:4123)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:491)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
    at dalvik.system.NativeStart.main(Native Method)

Any suggestions on it will be much appriciated. Thanks

fdmirza
  • 311
  • 1
  • 2
  • 7
  • at MyFragment.java:90 I am trying to get sharedpreference using the context of main (its parent) activity. – fdmirza Feb 02 '12 at 06:45

3 Answers3

3

When stuff like this happen, it's mainly due to the fact that the whole application was closed by the system due to the need of memory, and it forced the system to call onCreate again.

Android does it's best trying to restore the previous state, and tries to open the last activity in the activity stack.

It is possible that a value being passed from another activity (by a Bundle, when using an Intent) is now null.

Try to make sure that you don't have any value that is passed from a different Activity and is not verified for it's integrity (make sure you have the condition if(value!=null) )...

EDIT

You can also override onLowMemory(), save some stuff to shared preferences, and load them when activity loads.

Rotemmiz
  • 7,933
  • 3
  • 36
  • 36
  • Thanks for the reply. Can I make this thing sure that the OS don't kill the actoivity? – fdmirza Feb 02 '12 at 07:17
  • 1
    @fdmirza see if system want memory it will surely kill unwanted or your activity to release memory. and there are preference to release memory or to kill process and activities check that out.you will get it all about stuff – OnkarDhane Feb 02 '12 at 07:20
  • I see. Can the application be restarted once the OS killed an activity? So it should not be started from the last-state BUT restart the whole app – fdmirza Feb 02 '12 at 07:25
  • 1
    yeah OS will restart your activity/service with help of BroadcastReceiver you can achieve it.don't worry. – OnkarDhane Feb 02 '12 at 07:45
0

its a null pointer exception,check the values that you passing through function replace(XX,YY).put break points with debugger you will get solution defiantly. and make sure you have registered your activity in androidmanifest.xml as its sounds unable to start activity.and one important thing if your application is not responding in time for this check it Response time it will helpful for you.

OnkarDhane
  • 1,450
  • 2
  • 14
  • 24
  • Thanks for reply. The error occurs after the application is in background and inactive for several hours. When launching the application is all perfectly alright. I guess the activity is put to death by the OS, or something like that... :( – fdmirza Feb 02 '12 at 07:16
  • you are welcome.then time taken by your activity in DDMS. you will get it.please decrease your application run time or response time. there is no probs with your code.just decrease time complexity and all...it will work fine..:) – OnkarDhane Feb 02 '12 at 07:18
0

you can use the :

import android.preference.PreferenceManager;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
// then you use
prefs.getBoolean("keystring", true);

for more about the shared preference : just follw the question hits 150+ more in stackflow: Shared Preference

Community
  • 1
  • 1
NovusMobile
  • 1,813
  • 2
  • 21
  • 48