Well, just do what the error message tells you.
Don't call setContentView()
before requestFeature()
.
Note:
As said in comments, for both ActionBarSherlock
and AppCompat
library, it's necessary to call requestFeature()
before super.onCreate()
I know it's over a year old, but calling requestFeature()
never solved my problem. In fact I don't call it at all.
It was an issue with inflating the view I suppose. Despite all my searching, I never found a suitable solution until I played around with the different methods of inflating a view.
AlertDialog.Builder is the easy solution but requires a lot of work if you use the onPrepareDialog()
to update that view.
Another alternative is to leverage AsyncTask for dialogs.
A final solution that I used is below:
public class CustomDialog extends AlertDialog {
private View content;
public CustomDialog(Context context) {
super(context);
LayoutInflater li = LayoutInflater.from(context);
content = li.inflate(R.layout.custom_view, null);
setUpAdditionalStuff(); // do more view cleanup
setView(content);
}
private void setUpAdditionalStuff() {
// ...
}
// Call ((CustomDialog) dialog).prepare() in the onPrepareDialog() method
public void prepare() {
setTitle(R.string.custom_title);
setIcon( getIcon() );
// ...
}
}
* Some Additional notes:
I was extending a DialogFragment and the above answer didnot work. I had to use getDialog() to achieve remove the title:
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
For SDK version 23 and above, the same RuntimeException is thrown if you are using AppCompatActivity to extend your activity. It will not happen if your activity derives directly from Activity.
This is a known issue on google as mentioned in https://code.google.com/p/android/issues/detail?id=186440
The work around provided for this is to use supportRequestWindowFeature() method instead of using requestFeature().
Please upvote if it solves your problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With