First, I've been looking for the answer all over, but didn't find anything that can explain how to resolve my case.
I want to declare a few final fields in my Android FragmentActivity and initialize them later (I need onCreate to be executed first to get the needed data) I know that I can initialize final fields only in the constructor, however, it is called before onCreate when nothing else is yet initialized, therefore the application crashes with java.lang.NullPointerException.
My code:
public class MyActivity extends FragmentActivity {
private final File mPhotosDir;
private final ConstraintLayout mConstraintLayout;
...
// This is the constructor of MyActivity activity
public MyActivity() {
this.mPhotosDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
this.mConstraintLayout = findViewById(R.id.myActivityConstraintLayout);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
Is it even possible to declare final fields in an activity and initialize them later? Shall I give up the final declaration?