android: java.lang.IllegalStateException: The specified child already has a parent.

I had quite a problem with fragments and the FragmentManager recently. The problem was the following stacktrace, which came when I tried to launch the application:

08-29 18:10:19.761: E/AndroidRuntime(17159): java.lang.RuntimeException: Unable to start activity ComponentInfo{packagename.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.app.ActivityThread.access$700(ActivityThread.java:152)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.os.Handler.dispatchMessage(Handler.java:99)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.os.Looper.loop(Looper.java:137)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.app.ActivityThread.main(ActivityThread.java:5328)

08-29 18:10:19.761: E/AndroidRuntime(17159): at java.lang.reflect.Method.invokeNative(Native Method)

08-29 18:10:19.761: E/AndroidRuntime(17159): at java.lang.reflect.Method.invoke(Method.java:511)

08-29 18:10:19.761: E/AndroidRuntime(17159): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)

08-29 18:10:19.761: E/AndroidRuntime(17159): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)

08-29 18:10:19.761: E/AndroidRuntime(17159): at dalvik.system.NativeStart.main(Native Method)

08-29 18:10:19.761: E/AndroidRuntime(17159): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.view.ViewGroup.addViewInner(ViewGroup.java:3435)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.view.ViewGroup.addView(ViewGroup.java:3306)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.view.ViewGroup.addView(ViewGroup.java:3251)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.view.ViewGroup.addView(ViewGroup.java:3227)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:914)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.app.BackStackRecord.run(BackStackRecord.java:682)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.app.Activity.performStart(Activity.java:5259)

08-29 18:10:19.761: E/AndroidRuntime(17159): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2220)

08-29 18:10:19.761: E/AndroidRuntime(17159): ... 11 more

The problem was the Fragment itself. You actually need to use a different constructor for the inflate method. I forgot to set attachToRoot to “false”, this should fix the problem:

View view = inflater.inflate(R.layout.fragment_graph, container, false);

various android ui things

20130817_120219

I just finished a small android project this morning. Yesterday evening I came across the code of someone on GitHub who had built a small duel game about Harry Potter. This was java application and you can play this in the console. So I then forked the repo and created the android application for it. You can find my project here: https://github.com/mrwonderman/Harry-Potter-Duel

The application is completely playable but I won’t publish the application to the play store as the game code isn’t from myself. But feel free to check the code out and try it yourself.

Now I learned some nice small things while writing the application. For example, you can append text to a TextView with the following command. So you don’t need to get and set the text manually.

textView.append("yay !");

Another thing is, how to make a TextView scrollable. I added the following lines to the xml layout of the TextView:

android:scrollbars="vertical"

android:singleLine="false"

and in the java code you need to use the following line:

textView.setMovementMethod(ScrollingMovementMethod.getInstance());

To display a Progressbar from right to left is very easy. Just use the following code:

progressBar.setRotation(180);

This then can look like that:

progressbarRTL

android-square-progressbar v.1.2.0

IMG_20130811_112751

After the summer holidays I ‘m publishing the next major version of the android-square-progressbar library. This isn’t actually a really big major version to the library itself, but the example application has changed very much. I’m now using the Navigation Drawer (https://developer.android.com/design/patterns/navigation-drawer.html) and a new Holo-Theme.

Navigation Drawer

Screenshot_2013-08-11-11-35-33With the navigation drawer I was able to move all the customisation things to another layer which is still easy to access. In the Navigation Drawer you can now choose the colour, activate the new opacity style, choose another image or visit the github page right from the app.

The Navigation Drawer is really easy to use and I created a small repository on Github where I’m hosting a basic version of an application which already contains a full configured Navigation Drawer. It still needs some fine tuning, so maybe check it out here later this month: BaseNavigationDrawer (GitHub). I will use this in future projects with Navigation Drawer’s.

The only thing I miss is the possibility to set a Style (like android.R.style.Theme_Holo_Light) to it. I haven’t found a possibility to do this. But I think this is supposed to be so, as you shouldn’t but any form widgets in there like CheckBoxes or Buttons.

This is now the second application where I am using the Navigation Drawer as the main navigation possibility and I can only recommend it. The other one was the “Current and Voltage” application here.

Opacity

Back to the new things the library can do now. As you see in the next image you can now activate the opacity. This will let the image disappear if the progress is 0, but I will appear more and more when the progress gets higher. When it reaches 100% then the images is shown normally. This can be a nice way to display the progress even more.

Screenshot_2013-08-11-11-35-42Screenshot_2013-08-11-11-35-55

 

You can set the opacity to the squareProgressBar with the following command:

squareProgressBar.setOpacity(true);

The road ahead

This is now the 4th update of the library and I want to bring a next one to you in september. I want to focus on these things:

  • OpenGL
  • Handling of bigger images
  • Some progressbar features

If you have any ideas of how I can improve the library, just comment or message me.

You can find the application and the source here:

Play Store : https://play.google.com/store/apps/details?id=net.yscs.android.square_progressbar_example

Github : https://github.com/mrwonderman/android-square-progressbar/