1

I've spent the last few hours figuring out why my application is crashing without an error message on load.

Basically, when I run it either by USB or by emulator, the screen refreshes every half a second or so. Looking at the log, it seems to be running onCreate in a loop. I can't figure out why! Eventually, there are a bunch of 'Channel is unrecoverably broken and will be disposed' messages. This message also turns up a lot:

07-15 13:59:22.334: ERROR/AbstractCompatWrapper(381): Invalid input to AbstructCompatWrapper 07-15 13:59:22.334: ERROR/CompatUtils(381): Exception in invoke: NullPointerException

I can post a complete log if you like, but I couldn't find anything useful there.

Finally, I discovered that removing the call to 'fillActionBar', a method that instantiates the action bar, 'fixes' the problem. However, I would like to be able to use the action bar! What have I done wrong?

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    resultBox = (TextView) findViewById(R.id.readout_values);

    // commenting out the following line fixes the problem, 
    // but removes the action bar.
    this.actionBar = fillActionBar(); 

    try {
        specialHttpClient = new SpecialHttpClient(
                "username", "password");
    } catch (Exception e) {
        Dbg.loge(this.getClass().getName(), "Could not instantiate client", e);
    }
}

private ActionBar fillActionBar() {
    ActionBar bar = getActionBar();// getSupportActionBar();
    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(
            this,
            android.R.layout.simple_spinner_dropdown_item,
            //R.layout.sherlock_spinner_item,
            pages
    );
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    ActionBar.OnNavigationListener navListener = new ActionBar.OnNavigationListener() {
        @Override
        public boolean onNavigationItemSelected(int itemPosition, long itemId) {
            Intent i = null;
            switch(itemPosition) {
                case 0:
                    i = new Intent(context, RecorderActivity.class);
                    break;
                case 1:
                    i = new Intent(context, TrackerActivity.class);
                    break;
            }
            startActivity(i);
            return true;
        }
    };
    bar.setListNavigationCallbacks(spinnerAdapter, navListener);
    return bar;
}

** EDIT **

The problem seems to be that startActivity(i) is being called even when no button is pressed. Moving that call to the second menu item only seems to fix the problem - but why is this happening?

Alex
  • 18,332
  • 10
  • 49
  • 53
  • Why don't you step through it and see where it breaks? – 323go Jul 15 '13 at 04:17
  • You know, I didn't realise until your comment that it was actually possible to step through live Android code executing on a separate device. How awesome! – Alex Jul 15 '13 at 08:03

1 Answers1

0

As sometimes happens, the process of compressing a problem for posting on SO helps reveal an answer.

The problem is that for a reason I don't understand, the first item in the ActionBar menu is being called when it gets created - thus the Activity keeps calling startActivity() on itself.

Adjusting the switch statement to the following stops the error:

    @Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
            switch(itemPosition) {
                case 0:
                    break;
                case 1:
                    Intent i = new Intent(context, TrackerActivity.class);
                    startActivity(i);
                    break;
            }
            return true;
    }

I will award correct answer to anyone who can explain why!

Alex
  • 18,332
  • 10
  • 49
  • 53