NullPointerException calling Flow.get(this) in the onCreate method.
See original GitHub issueI am getting the following error when I call Flow.get(this) inside the onCreate() method.
Caused by: java.lang.NullPointerException: Attempt to read from field 'flow.Flow flow.InternalLifecycleIntegration.flow' on a null object reference
at flow.InternalContextWrapper.getSystemService(InternalContextWrapper.java:54)
at android.view.ContextThemeWrapper.getSystemService(ContextThemeWrapper.java:123)
at android.app.Activity.getSystemService(Activity.java:5263)
at flow.InternalContextWrapper.getFlow(InternalContextWrapper.java:31)
at flow.Flow.get(Flow.java:46)
at flow.sample.basic.BasicSampleActivity.onCreate(BasicSampleActivity.java:30)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
The code:
public class BasicSampleActivity extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_activity_frame);
Flow flow = Flow.get(this); // <============== ERROR!!! NPE
System.out.println(flow);
}
@Override protected void attachBaseContext(Context baseContext) {
baseContext = Flow.configure(baseContext, this) //
.dispatcher(new BasicDispatcher(this)) //
.defaultKey(new WelcomeScreen()) //
.keyParceler(new BasicKeyParceler()) //
.install();
super.attachBaseContext(baseContext);
}
@Override public void onBackPressed() {
if (!Flow.get(this).goBack()) {
super.onBackPressed();
}
}
}
Somebody can explain me why this is happening in the onCreate() method? If I do the same in onStart() or onResume() everything works well.
I am not sure if this is a bug or not, but I think a wiki/documentation is required for this awesome library.
Issue Analytics
- State:
- Created 7 years ago
- Comments:9
Top Results From Across the Web
NullPointerException accessing views in onCreate()
I get NullPointerException when attempting to call a method on View s obtained with findViewById() in my activity onCreate() .
Read more >Getting Error: java.lang.NullPointerException in t... - ServiceNow
Hi, I created a catalog items with two choice fields and one is dependent on another. 1. category -- values are database and...
Read more >Null Safety Tutorial in Kotlin: Best Practices - Kodeco
In this tutorial, you'll look at Kotlin's nullability best practices. You'll learn about null safety in Kotlin and how to avoid NPEs.
Read more >Handling Lifecycles with Lifecycle-Aware Components
A common pattern is to implement the actions of the dependent components in the lifecycle methods of activities and fragments.
Read more >Null Pointer Exception In Method (Android forum at Coderanch)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean org.json.JSONObject.getBoolean(java.lang.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@epool you might be correct, Flow is initialized in
onActivityCreated()method ofInternalLifecycleIntegration(retained fragment), so you might be better off if you move that logic toonPostCreate()+1 to handling this in the dispatcher.