Reputation: 21870
I'm launching a new activity (TVMPDFActivity
) from my root activity. I'm using Android Annotations, so this is how I'm launching the Activity:
TVMPDFActivity_.intent(this.getActivity()).start();
TVMPDFActivity
is a subclass of PDFPreviewActivity
which is a subclass of ActionBarActivity
.
A NullPointerException is being thrown from onCreate in PDFPreviewActivity
when I simply try to GET the ActionBar. On this line:
final ActionBar actionBar = getSupportActionBar();
Here is the stacktrace from my code to the NPE:
Caused by: java.lang.NullPointerException
at android.support.v7.internal.app.WindowDecorActionBar.getDecorToolbar(WindowDecorActionBar.java:248)
at android.support.v7.internal.app.WindowDecorActionBar.init(WindowDecorActionBar.java:201)
at android.support.v7.internal.app.WindowDecorActionBar.<init>(WindowDecorActionBar.java:176)
at android.support.v7.app.ActionBarActivityDelegateBase.createSupportActionBar(ActionBarActivityDelegateBase.java:156)
at android.support.v7.app.ActionBarActivityDelegate.getSupportActionBar(ActionBarActivityDelegate.java:123)
at android.support.v7.app.ActionBarActivity.getSupportActionBar(ActionBarActivity.java:73)
at com.my.app.PDFPreviewActivity.onCreate(PDFPreviewActivity.java:63)
at com.my.app.TVMPDFActivity.onCreate(TVMPDFActivity.java:24)
at com.my.app.TVMPDFActivity_.onCreate(TVMPDFActivity_.java:31)
Here are the onCreate methods of the various classes involved:
public class TVMPDFActivity extends PDFPreviewActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
this.setContentView(R.layout.tvm_pdf_activity);
super.onCreate(savedInstanceState);
...
}
}
public abstract class PDFPreviewActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ActionBar actionBar = getSupportActionBar(); // throws NullPointerException
...
}
}
Why am I getting a NullPointerException from deep within the support v7 code?
Upvotes: 2
Views: 4383
Reputation: 157457
you should call super.onCreate
before setContentView
in TVMPDFActivity
in order to give android the opportunity to initialize its internal state, before trying to access something that relies on that internal state. For onCreate
/onStart
/onResume
call the super
always as first statement
Upvotes: 1