DialogFragment mysterious Null Pointer Exception - java

I cannot seem to make head or tail from the stack trace eclipse is giving me. The code used to work. I am not sure what I changed. But now when I launch the dialog fragment, the app crashes with the following error log.
FATAL EXCEPTION: main
Process: com.company.appname, PID: 8962
java.lang.NullPointerException
at android.support.v4.app.DialogFragment.onActivityCreated(DialogFragment.java:366)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1508)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:958)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5579)
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:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
My code is massive so I am not sure which portion to show. So maybe someone has seen this mysterious kind of crash with apparently no root in my code (i.e. since eclipse is not naming one)
UPDATE
Ok. I find the culprit. But I still don’t know how to fix it. So I have CatDialog (the one that’s crashing now) and FacebookDialog. So I have had these two DialogFragments working independently. But now I am launching FacebookDialog from inside the onCreateView method of the CatDialog. That is the reason for the crash. The launching is conditional as the following code shows
protected void requestFaceBookPublishPermission() {
Log.i(TAG, "going into requestFaceBookPublishPermission");
Session session = Session.getActiveSession();
if (session == null || !session.isOpened() ||
!session.isPermissionGranted(FacebookDialog.PERMISSION)) {
FacebookDialog fb = new FacebookDialog();
fb.show(getChildFragmentManager(), "fb");
dismiss();
}
Log.i(TAG, "leaving requestFaceBookPublishPermission");
}

You are calling dismiss() from inside requestFaceBookPublishPermission and that's totally wack! You can't do that if you are calling it from inside onCreateView of another DialogFragment. So you seed to keep the two separated again. But great question though.
So understand what you are doing: you are dismissing before returning the view inside onCreateView. You are pre-empting.

I've encountered this issue before. Then I was dismissing the dialog inside oncreateView and I was getting NullpointerException. Then I moved my code to dismiss the dialog to onResume() and the issue was fixed!
Wrong
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
if(something){
} else{
dismiss(); //Dismissing dialog here gives NullpointerException
}
}
return view;
Correct
#Override
public void onResume() {
super.onResume();
if(something){
} else{
dismiss();
}
}

Related

Activity has leaked window that was originally added (problem in superclass show())

I have some dialog popups in app. If I kill app while dialog popup is raised I am getting below crash error.
2020-11-26 10:09:25.275 15274-15274/? E/WindowManager: android.view.WindowLeaked: Activity com.myproject.v1.HomeActivity has leaked window DecorView#d75132a[] that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:418)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:331)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.app.Dialog.show(Dialog.java:322)
at com.helper.DialogHelper.show(DialogHelper.java:71)
at com.myproject.v1.HomeActivity.pointDialog(HomeActivity.java:1592)
at com.myproject.v1.HomeActivity.onClick_LevelInfo(HomeActivity.java:1604)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4720)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
I have seen many similar problems in stack but they are not same to my problem bcs my problem is not in dialoghelper class it is in superclass show(). I have tried to solve in dialoghelper dismiss while activity destroying but didn't fix bcs showing problem is in dialoghelper but it is beforedialog helper.
The view is trying to show a Dialog after it has exited the Activity.
Make sure you close the dialog before leaving the activity.
Call dismiss() on the Dialog instance you created before exiting your Activity, e.g. in onPause() or onStop() or onDestroy()
Like
#Override public void onStop() { if (dialog != null) { dialog.dismiss(); }}`.
Also make sure you do a null check for the dialog before displaying it.
Like
if (dialog != null) { dialog.show(); }

attempt to remove OnFrameMetricsAvailableListener that was never added

The app crashes everytime I exit the main activity, but only if I go into another activity before that.
Meaning that, if I just go into the first screen and exit it befor entrying another activity, the app wont crash.
I'm getting the Crash on the "super.onStop();" line, in the main activity as a result of an error saying: "attempt to remove OnFrameMetricsAvailableListener that was never added".
The code worked fine last time, and the last thing I changed was the build.gradle and some classpath to: classpath 'com.android.tools.build:gradle:3.1.2' and classpath 'com.google.gms: google-services: 3.3.1'. nothing else..
(Also, the application crashes even if I'm not calling the onStop() method. and I wants to add that I'm not using FrameMetrics object in my project at all !!)
Thank very much to anyone who can help!
#Override
public void onStop() {
super.onStop();
StateManager mgr = new StateManager(this);
if (mMap != null) {
mgr.saveMapState(mMap);
}
}
crash
Process: com.example.nabeth.youandi, PID: 24034
java.lang.RuntimeException: Unable to stop activity {com.example.nabeth.youandi/com.example.nabeth.youandi.MapsActivity}: java.lang.IllegalArgumentException: attempt to remove OnFrameMetricsAvailableListener that was never added
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4209)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4265)
at android.app.ActivityThread.-wrap6(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1565)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6247)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
Caused by: java.lang.IllegalArgumentException: attempt to remove OnFrameMetricsAvailableListener that was never added**
at android.view.View.removeFrameMetricsListener(View.java:5573)
at android.view.Window.removeOnFrameMetricsAvailableListener(Window.java:865)
at android.support.v4.app.FrameMetricsAggregator$FrameMetricsApi24Impl.remove(FrameMetricsAggregator.java:432)
at android.support.v4.app.FrameMetricsAggregator.remove(FrameMetricsAggregator.java:245)
at com.google.android.gms.internal.firebase-perf.zzd.onActivityStopped(Unknown Source)
at android.app.Application.dispatchActivityStopped(Application.java:234)
at android.app.Activity.onStop(Activity.java:1815)
at android.support.v4.app.FragmentActivity.onStop(FragmentActivity.java:613)
at com.example.nabeth.youandi.MapsActivity.onStop(MapsActivity.java:2684)
at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1290)
at android.app.Activity.performStop(Activity.java:6932)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4204)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4265) 
at android.app.ActivityThread.-wrap6(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1565) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6247) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 
Removing android:hardwareAccelerated="false" from AndroidManifest.xml helped me
For me the issue is because of com.google.firebase:firebase-perf:15.2.0, so
I have resolved my issue by downgrading the version
implementation 'com.google.firebase:firebase-perf:15.+'
The same issue with firebase-perf:16.1.2. Following workaround helped to me.
In your activity (from the crash's message) do the following:
#Override
protected void onStop() {
try {
super.onStop();
} catch (Exception e) {
Log.w(TAG, "onStop()", e);
super.onStop();
}
}

WindowManager$BadTokenException when showing AlertDialog on Android 7

Google Play shows a lot of crash reports caused by WindowManager$BadTokenException.
Full log:
java.lang.RuntimeException:
at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:3344)
at android.app.ActivityThread.-wrap21 (ActivityThread.java)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1583)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:154)
at android.app.ActivityThread.main (ActivityThread.java:6121)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:779)
Caused by: android.view.WindowManager$BadTokenException:
at android.view.ViewRootImpl.setView (ViewRootImpl.java:679)
at android.view.WindowManagerGlobal.addView (WindowManagerGlobal.java:342)
at android.view.WindowManagerImpl.addView (WindowManagerImpl.java:93)
at android.app.Dialog.show (Dialog.java:329)
at android.app.AlertDialog$Builder.show (AlertDialog.java:1112)
at com.nutomic.syncthingandroid.activities.SyncthingActivity.showLoadingDialog (SyncthingActivity.java:142)
at com.nutomic.syncthingandroid.activities.SyncthingActivity.onApiChange (SyncthingActivity.java:88)
at com.nutomic.syncthingandroid.activities.SyncthingActivity.access$lambda$1 (SyncthingActivity.java)
at com.nutomic.syncthingandroid.activities.SyncthingActivity$$Lambda$2.onApiChange (Unknown Source)
at com.nutomic.syncthingandroid.service.SyncthingService.onApiChange (SyncthingService.java:530)
at com.nutomic.syncthingandroid.service.SyncthingService.updateState (SyncthingService.java:246)
at com.nutomic.syncthingandroid.service.SyncthingService.onStartCommand (SyncthingService.java:181)
at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:3327)
Relevant code:
if (isFinishing() || mLoadingDialog != null)
return;
LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.dialog_loading, null);
TextView loadingText = (TextView) dialogLayout.findViewById(R.id.loading_text);
loadingText.setText((getIntent().getBooleanExtra(EXTRA_FIRST_START, false))
? R.string.web_gui_creating_key
: R.string.api_loading);
mLoadingDialog = new AlertDialog.Builder(this)
.setCancelable(false)
.setView(dialogLayout)
.show();
What could be the cause of this crash? It would also help if I had a way to reproduce the crash. The full source of the app is available on Github.
Such a problem might occur if the app is closing, or is in the background somehow.
You should check if your activity is not being closed (for example, onDestroy was called), and that it is active in the foreground (and visible).
There are several options for how to check if it is in the background, here's one that should work:
RunningAppProcessInfo myProcess = new RunningAppProcessInfo();
ActivityManager.getMyMemoryState(myProcess);
isInBackground = myProcess.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
Okay I found the problem. In my code I was doing something like this (in a much more complicated way):
if (isFinishing())
return;
new AlertDialog.Builder(this).show();
Thread.sleep(1000);
The show() method call finished normally, but the dialog was only attached to the Activity after the sleep was finished. So if you have this problem, make sure you're not doing any long-running operation on the main thread.

NullPointerException: SetOnClickListener(this) - Android

When running my application in Android Studio I get a NullPointerException at line 30 which is this:
llTheory.setOnClickListener(this);
which has always worked for me.. I am implementing View.OnClickListener and have the override method here:
#Override
public void onClick(View v) {
....
}
My LinearLayout is declared before the setOnClickListener here:
LinearLayout llTheory = (LinearLayout) findViewById(R.id.llTheory);
so what is going wrong? Here is my LogCat output:
10-20 21:09:24.045 14625-14625/com.timmo.applauncher E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.timmo.applauncher, PID: 14625
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.timmo.applauncher/com.timmo.timmoapplauncher.Main}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2224)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5153)
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:796)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.timmo.timmoapplauncher.Main.Init(Main.java:30)
at com.timmo.timmoapplauncher.Main.onCreate(Main.java:78)
at android.app.Activity.performCreate(Activity.java:5312)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5153)
            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:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
            at dalvik.system.NativeStart.main(Native Method)
Fragments work a little bit differently.
You have to use the concept of rootview when dealing with them. Otherwise you will find yourself always coming across the dreaded "NULL POINTER" exception.
Here is how to go about it.
Define the rootview when you inflate each view for each fragment, then call the onClick functionality.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootview = inflater.inflate(R.layout.xml_you_want_to_inflate, container,false);
// Now call rootview and the onclick fuction, presumably for a list or button
rootview.findViewById(R.id.your_button/list_id_here).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do stuff.
}
});
Another alternative is using getView which always returns the rootview. Try this instead
LinearLayout llTheory = (LinearLayout) getView().findViewById(R.id.llTheory);
This link explains a similar problem.
The former way however is the correct way as you isolate your fragments from each other and understand the view hierarchy correctly. I would read this. Trust me the time investment reading that stuff is worthwhile then the hours you'll spend otherwise with half-wit hack job ideas to make stuff work.
findViewById returns a null value when the requested view cannot be found.
Check in your corresponding xml that a view is indeed declared with the id #id/llTheory
A standard Java best practice would be checking a casted item before using it.
Do you have
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_with_R_id_llTheory);
...
in your activity? Your activity (or fragment hosted by activity) must have layout with R.id.llTheory.

android.util.AndroidRuntimeException: requestFeature() must be called before adding content

I am getting this android.util.AndroidRuntimeException: requestFeature() must be called before adding content error. As you can see in the below code, the requestWindowFeature(Window.FEATURE_NO_TITLE); line comes before setContentView(R.layout.mainmenu); line of code. This onCreate() code is the same format in just about every one of my activities and I've never had trouble with it before until now. Ever since I updated to ADT 22 a lot of random errors have been popping up everywhere. I have weeded through a lot of those errors and this is my latest one.
What can I do to fix this error?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.mainmenu);
LogCat
05-31 04:20:43.121: E/AndroidRuntime(14559): FATAL EXCEPTION: main
05-31 04:20:43.121: E/AndroidRuntime(14559): java.lang.RuntimeException: Unable to start activity ComponentInfo{matt.lyons.bibletrivia.lite/matt.lyons.bibletrivia.lite.MainMenu}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
05-31 04:20:43.121: E/AndroidRuntime(14559): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-31 04:20:43.121: E/AndroidRuntime(14559): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-31 04:20:43.121: E/AndroidRuntime(14559): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-31 04:20:43.121: E/AndroidRuntime(14559): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-31 04:20:43.121: E/AndroidRuntime(14559): at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 04:20:43.121: E/AndroidRuntime(14559): at android.os.Looper.loop(Looper.java:137)
05-31 04:20:43.121: E/AndroidRuntime(14559): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-31 04:20:43.121: E/AndroidRuntime(14559): at java.lang.reflect.Method.invokeNative(Native Method)
05-31 04:20:43.121: E/AndroidRuntime(14559): at java.lang.reflect.Method.invoke(Method.java:511)
05-31 04:20:43.121: E/AndroidRuntime(14559): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-31 04:20:43.121: E/AndroidRuntime(14559): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-31 04:20:43.121: E/AndroidRuntime(14559): at dalvik.system.NativeStart.main(Native Method)
05-31 04:20:43.121: E/AndroidRuntime(14559): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
05-31 04:20:43.121: E/AndroidRuntime(14559): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:229)
05-31 04:20:43.121: E/AndroidRuntime(14559): at android.app.Activity.requestWindowFeature(Activity.java:3244)
05-31 04:20:43.121: E/AndroidRuntime(14559): at matt.lyons.bibletrivia.lite.MainMenu.onCreate(MainMenu.java:28)
05-31 04:20:43.121: E/AndroidRuntime(14559): at android.app.Activity.performCreate(Activity.java:5104)
05-31 04:20:43.121: E/AndroidRuntime(14559): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-31 04:20:43.121: E/AndroidRuntime(14559): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-31 04:20:43.121: E/AndroidRuntime(14559): ... 11 more
I also faced this problem but when i call window request before calling super.onCreate() then problem was solved, please try it also like..
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
}
Hope this will help you...:)
Edited: For other possible solutions for Android'd new versions
Hide the Status Bar on Android 4.0 and Lower
<application
...
android:theme="#android:style/Theme.Holo.NoActionBar.Fullscreen" >
...
</application>
The advantages of using an activity theme are as follows:
It's easier to maintain and less error-prone than setting a flag programmatically.
It results in smoother UI transitions, because the system has the information it needs to render your UI before instantiating your app's main activity.
Android version is lower than Jellybean
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// If the Android version is lower than Jellybean, use this call to hide
// the status bar.
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
setContentView(R.layout.activity_main);
}
Hide the Status Bar on Android 4.1 and Higher
View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
// Remember that you should never show the action bar if the
// status bar is hidden, so hide that too if necessary.
ActionBar actionBar = getActionBar();
actionBar.hide();
Note the following:
Once UI flags have been cleared (for example, by navigating away from the activity), your app needs to reset them if you want to hide the bars again. See Responding to UI Visibility Changes for a discussion of how to listen for UI visibility changes so that your app can respond accordingly.
Where you set the UI flags makes a difference. If you hide the system bars in your activity's onCreate() method and the user presses Home, the system bars will reappear. When the user reopens the activity, onCreate() won't get called, so the system bars will remain visible. If you want system UI changes to persist as the user navigates in and out of your activity, set UI flags in onResume() or onWindowFocusChanged().
The method setSystemUiVisibility() only has an effect if the view you call it from is visible.
Navigating away from the view causes flags set with setSystemUiVisibility() to be cleared.
I got that exception (android.util.AndroidRuntimeException: requestFeature() must be called before adding content) when using
requestWindowFeature(Window.FEATURE_NO_TITLE);
in an older device running Android 2.3.5 (Gingerbread). I am using the v7 support library.
The error was fixed when I changed it to use:
supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
(This comes after my super.onCreate call in the fix, too). See docs at https://developer.android.com/reference/android/support/v7/app/ActionBarActivity.html#supportRequestWindowFeature(int)
So it may be more a case of a misleading error message than anything else.
Please check your class is extend from Activity or ActionBarActivity. If you are using ActionBarActivity please use Activity.
If you're using your activity as a Dialog (with Theme.Dialog), then make sure you extend Activity instead of ActionBarActivity (the default given to you with the Android Studio wizard). Then you can use
requestWindowFeature(Window.FEATURE_NO_TITLE);
or
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
AFTER the super.onCreate()...
I also ran into this error from a different workflow. I created a custom DialogFragment class and I created two #Override functions - onCreateView and onCreateDialog. My onCreateView function grabbed a custom layout for the fragment, and my onCreateDialog function created an AlertDialog.Builder.
This appeared to not work because the onCreateDialog is called before onCreateView. After I deleted onCreateView [by moving my custom view inflation into onCreateDialog, i encountered the error:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I realized my difficulty came from trying to implement both overrides since I wanted to 1) use a layout for the main view of the dialog, and 2) use the Builder pre-defined Positive / Negative buttons. My solution was to create the positive / negative buttons in my custom dialog view, so i deleted my implementation of the Override onCreateDialog function.
Hope this helps someone in the future!
Here are some SO questions that helped me:
Problem inflating custom view for AlertDialog in DialogFragment
ViewPager in DialogFragment - IllegalStateException: Fragment does not have a view
Custom Layout for DialogFragment OnCreateView vs. OnCreateDialog
The specified child already has a parent. You must call removeView() on the child's parent first
I was extending a DialogFragment and the above answer didnot work. I had to use getDialog() to remove the title:
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowActionBar">false</item>
</style>
just only set style like this no any coding side changing is required.
Extend (Activity) instead of (ActionBarActivity)
example: public class Itemdetails extends Activity {....
then in the onCreate write:
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.Your_Activity);
i think the simplest way to do this is use this below code
getActionBar().setTitle(null);
Please try the following options, at least one of these should work for you:
If you are using a DialogFragment then try not overriding both methods onCreateDialog(..) and onCreateView(..) (This change worked for me)
try supportRequestWindowFeature(Window.FEATURE_NO_TITLE)/requestWindowFeature(Window.FEATURE_NO_TITLE) before setContentView(..) in the activity and after super.onCreate(..);
Try option 2 before super.onCreate(..);
Misplaced your line:
super.onCreate(savedInstanceState);
use this Manner:
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qrscanner);
I had this error with AlertDialog and the error was happening in API 19 only when dialog.show() was called.
figured out that the android.app.AlertDialog import is causing the problem so I changed it to androidx.appcompat.app.AlertDialog and it fixed it.
Note that this will work if you are using androidx. if you are not, you should use the old appcompat version of that import.

Categories