0

So uh... i have a ViewPager Activity (MasterActivity) with 3 Fragments (Master1Fragment, Master2Fragment, Master3Fragment) which store values in excel file. I have no problem saving the data collected from controls (EditText, Spinner, CheckBox etc) in all 3 fragments (i place the Save Button on third fragment, Master3Fragment) with OnClick Event in 3rd Fragment.

Problem arise when i try to load data from the OnCreate event in the MasterActivity (I suspect it's because the fragments haven't finished created) if that is so, in what event should i put my LoadData Function? I want to load the data automatically when the user open the Activity.

Error generated at here:

...
} else {
    //non empty cells handler
    switch (cn) {
        case 0:
            Master2Fragment.etKrg1.setText((int) c.getNumericCellValue());
            break;
        case 1:
...

Part of LogCat:

08-29 14:31:17.591  12886-12886/com.bakemono.fertilizerchecking.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.bakemono.fertilizerchecking.app, PID: 12886
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bakemono.fertilizerchecking.app/com.bakemono.fertilizerchecking.app.MasterActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2200)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
        at android.app.ActivityThread.access$800(ActivityThread.java:139)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5105)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.bakemono.fertilizerchecking.app.MasterActivity.loadMaster(MasterActivity.java:575)
        at com.bakemono.fertilizerchecking.app.MasterActivity.onCreate(MasterActivity.java:120)
        at android.app.Activity.performCreate(Activity.java:5275)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164)
jww
  • 97,681
  • 90
  • 411
  • 885
Milanor
  • 257
  • 1
  • 2
  • 10
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – jww Aug 29 '14 at 08:49
  • What i seek here is an explanation related to ViewPager and Fragments lifecycle, i can't found any relevance with the possible duplicate stated above since i'm fully aware what a Null Pointer Exception is and i'm not asking for an explanation about Null Pointer Exception. – Milanor Aug 29 '14 at 09:26
  • 1
    maybe i need to change the title though.. now that i read the title again.. it really seems off. – Milanor Aug 29 '14 at 09:27

1 Answers1

3

Yes, you're right: The fragment lifecycle hasn't yet started when you're running onCreate() of your activity.

First, get rid of the public static (or package-private static) members in your fragments and make them private and non-static. Other classes should have no business touching a fragment's internals, and making them static causes its set of problems, too.

Then, access and set up fragment views in onCreateView() or onViewCreated() callbacks in the fragment itself.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • so it means i should load the data on creation of each fragments rather than load it all and assign the values to the fragments' controls from the activity? – Milanor Aug 29 '14 at 08:34
  • Fragments should be relatively independent pieces of UI functionality that manage their own data and so on. – laalto Aug 29 '14 at 08:35
  • I see.. yes, in my other application i load the data from the fragment itself. Just want to try out new ideas on this one. But it seems it doesn't work ~_~ or rather i've not followed the lifecycle. – Milanor Aug 29 '14 at 08:38
  • Anyway, thanks for the explanation. At least you make it clearer to me. – Milanor Aug 29 '14 at 08:42