Windows 8.1 update fails repeatedly with error 800F0922

Took me a while to figure this one out.

The reason for the error was insufficient space in the Windows System Reserved partition. I had to shrink my C drive and expand the System Reserved partition. (I used GParted from Linux, you can use a boot CD like PartedMagic or whatever method works for you).

Windows boot might break because of this. You’ll get error 0xc0000225.

You’ll have to boot from a Windows 8(.1) installation media, get to the command line and then:

  1. Run diskpart
  2. Type select disk 0 (0 is the number of the drive)
  3. Type list partition to see make sure this is the right drive and to find the system reserved partition number.
  4. Type select partition 1 (change 1 to the reserved partition number)
  5. Type active
  6. Type exit to close the tool
  7. Run bcdboot C:\Windows (change C to the partition where Windows in installed).

Windows should boot now. You can use EasyBCD to remove the old unbootable BCD entry.

Hope it helps.

Android – how to make UI values survive rotation

When programming for Android, you have to take into account that your application will be killed, paused and recreated without your consent. This is because of the nature of phones which have less memory and move from one task to another without the ability to support everything at the same time.

The example I’m going to take is screen rotation. When the screen rotates, the visible activity gets recreated. How do I know that? I have two Spinners on it, one with an adapter that changes according to the other’s selection. With each rotation I lost the current selection of the second Spinner. I added some Toasts to the code and found that onCreate() is being called upon orientation change. The first thing that comes to mind is to save the selection somewhere and restore it after the recreation.

There is a facility for that. Have you seen the parameter that your onCreate() gets?
protected void onCreate(Bundle savedInstanceState)
This savedInstanceState is a ‘bundle’ which means a space that Android gives your application to save some info between recreations of your activities. There are also two methods you can use to put the save and restore code in:

public void onSaveInstanceState(Bundle savedInstanceState)
    savedInstanceState.putInt("numberOfThings", num);
    savedInstanceState.putInt("anotherNumber", numnum);

public void onRestoreInstanceState(Bundle savedInstanceState)
    num = savedInstanceState.getInt("numberOfThings");
    numnum = savedInstanceState.getInt("anotherNumber");

This didn’t help me completely because it was only then that I realized that onCreate is called for every rotation and in onCreate I initialized my UI elements to their default values. I tried putting num = savedInstanceState.getInt("numberOfThings") in onCreate to load the previous values but got a null reference exception on the first run because savedInstanceState is not initialized if this is the first run. The complete solution was to ditch onRestoreInstanceState and use only onCreate(). I set the default values in variables, and then did:

if (savedInstanceState != null)
    // Load variables here and overrite the default values

The rest of the initialization stayed as it was.

So the complete solution is:

  • Save the values you need in class members using onSaveInstanceState
  • In onCreate() restore the values if savedInstanceState != null and use them for initialization

That’s all folks.

C++ – How to convert anything to string?

Well, at least anything that can be printed. It’s quite easy.

  1. Create a string stream
  2. “Print” the value to the stream
  3. Get a string from the string stream

I like to wrap it in a template function like that:

#include <string>
#include <sstream>

template <typename T>
string to_string(T x)
    stringstream ss;
    return ss.str();

You can make the function inline, and get the argument as a reference to const if you want.

Have you got a simpler generic solution?