views:

235

answers:

1

None of my apps work in the emulator. I am running Ubuntu 9.10 and everytime I try to access my UI, the app crashes. All I get is an "Sorry! The application ... has stopped unexpectedly". For EVERY app this happens.

package com.mohit.helloandroid;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;

public class HelloAndroid extends TabActivity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Resources res = getResources();     //Resource object to get drawables
        TabHost tabHost = getTabHost();     //The activity tabhost
        TabHost.TabSpec spec;           //Reusable tab spec 
        Intent intent;

        intent = new Intent().setClass(this, HelloAndroid.class);

        spec = tabHost.newTabSpec("artists").setIndicator("Artists", res
            .getDrawable(R.drawable.tab_artists))
                .setContent(intent);
        tabHost.addTab(spec);
    }
}

I don't know how this code could possibly throw a message like that.

+1  A: 

You've got at least a couple whacky things going on, any of them could cause the bomb.

First you've called:

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

But you've subclassed TabActivity which sets its own content view when you call super.OnCreate. Given the code you have i'm guessing that your R.layout.main is probably empty. Get rid of the setContentView call.

Next you call:

intent = new Intent().setClass(this, HelloAndroid.class);

Your trying to create an intent to display in a tab by passing in this class which itself is a tabHost and will try to do this all again when its onCreate is called. If android let this happen you'd have just created an infinitely recursive call and it would crash the phone when it ran out of memory. More likely I imagine Android will just bomb with some exception or the OS will just nuke the process.

Start simpler. Create a couple basic views in your resources to display in your tabs.

Mark