Friday, November 29, 2019

IllegalStateException: The specified child already has a parent


If your app suddenly starts crashing with the following exception, and you have ruled out the usual suspects, its probably due to an appcompat issue.

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first

I updated our andridx.appcompat version from 1.0.0 to 1.1.0 and was greeted with this crash. Unfortunately for me, that commit included multiple changes like enabling leak canary, updating the versions of various dependency libraries, and enabling multidex. So it took me some time to narrow down the offending change. The crash went away when I rolled back appcompat version to 1.0.0


But I want to use the latest version of appcompat!

In my case, the root cause was that we were reusing fragment's layout view object. In the first call to onCreateView, I'd inflate the layout xml and store the resulting view in a field. In the If the subsequent calls to this method, we would just return it instead of inflating again. Something like the following snippet. 

public View onCreateView(LayoutInflater inflater,
                         ViewGroup container, Bundle savedState) {
    if (mFragmentView != null) {
        return mFragmentView;
    }
    mFragmentView = inflater.inflate(R.layout.fragment_main, container, false);
    return mFragmentView;
}

The crash happens when `mFragmentView` is not null. This happens whenever the user navigates back to this fragment using the back button. I removed that line so that we always inflated the layout, and it fixed the crash. Looks like reusing is not encouraged in this context. I was relying on this logic to preserve the fragment's state (text etc) in some cases, so I think it is time for a 'minor' refactoring.

Removing the root view from its parent in onDestroyView also seems to work, but that sounds like cheating. I am going to keep that change on a branch until I figure out why appcompat team decided that we should not be reusing fragment layout objects like this. I couldn't find anything on StackOverflow that explains this behaviour. If you think reusing layouts is not a good idea, or know why appcompat changed its behaviour, please leave a comment.

Friday, September 27, 2013

MP4 Viewer

MP4Viewer is an open-source ISO base media file format viewer. It parses the isobmf file and displays the metadata on the console or in a separate window. It is written in python and uses pygtk for displaying the box information in a window. MP4Viewer is hosted in github

Screenshots


Usage: cd into src folder and run
$ ./showboxes.py [-h] [-o {stdout,gui}] [-e] [-c {on,off}] iso-base-media-file

Positional arguments:
  iso-base-media-file   Path to iso media file

Optional arguments:
  -o {stdout,gui}       Select output format (console or windows). Default is console.
                        TODO: Add XML output
  -c {on,off}           Turn on/off colors in stdout; on by default.
  -e                    Long arrays such as sample sizes are truncated by default.
                        This flag expands them.
  -h, --help            Help!

You need to have pygtk2.0 installed for viewing the results in a window.

The definitions of structures used in the code can be found in the publicly available standard ISO/IEC 14496-12 - ISO base media file format

Thursday, August 23, 2012

Search+

When I am not writing code, I find myself going through log files trying to make some sense out of it... er, unless I am browsing Reddit or Facebook, but I digress. The editor of choice is notepad++ as it is powerful and lightweight. However, the search functionality in notepad++ sometimes fails to meet my needs as I end up searching for a word, bookmark it and search for another word from that point, and then a third one from there and so on until I give up and copy the file to a Linux machine and grep for it in a loop. Things would be easier if one could search for multiple keywords at the same time and filter out the matching lines in npp itself. Maybe it is my poor googfu, but I couldn't find anything that would let me do that; but what I did find was that notepad++ provides a nice plugin interface with a decent documentation. So I decided to get my hands dirty and write a plugin that does this. I have been using it for sometime and found it helpful so I thought I'd share it.

As described above, Search+ is a notepad++ plugin that lets you search for multiple keywords in a single shot. You can specify a list of patterns and filter out all lines from the current document that match any of the keywords in that list.
  • Keywords are interpreted as regex
  • Search is case insensitive
  • Matching lines are listed in a separate ListBox
  • Option to highlight matched strings in the original document
Here is a screenshot of the plugin in action(click the image for full size).


  • You can download Search+ from Google code
  • For more information regarding installation and usage, read this wiki page.
  • Search+ is written in C++ and this is kinda my first work in that language. Feel free to browse the source code and point out any issues there :-)
In case of any issues or suggestions, please leave a comment here. And if you find this useful, do spread the word.

Update (December 2015): The project was initially hosted in google code; back then, you could just download the dll and put it in the npp plugins folder to install this. After moving the hosting to github when google discontinued their code hosting service, I haven't had a chance to add compiled dll to github. As of now, you'd have to download the source code and compile it yourself. I don't have access to windows compiler as I am working in Linux nowadays.