Jasper
Jasper

Reputation: 2409

set new content view after running thread

I'm creating a startup activity for an application that downloads and parses some data. While it is loading i want to view my startup_loading.xml file, which contains a progressbar and a textview which is set to "loading". Everything i need to do in the startup is put in a thread, the startUp thread. After running this thread the data is loaded, and then i want to show a new view: startup_loaded.xml which contains two buttons which send you to different activities in the application.

But i have troubles using two views and displaying them at the time i want to. I thought that if i put the

setContentView(R.layout.startup_loaded);

at the end of the runnable of my thread it would work, but i get an error that that is impossible

CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

I thought i could fix it like this:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    startUp.start();
    String language =  Locale.getDefault().getISO3Language();
    if (startUp.isAlive()) {
        setContentView(R.layout.startup_loading);
        tv1=(TextView)findViewById(R.id.tvloading);
        if(language.equals("nld")) {
            tv1.setText("bijwerken");
        } else {
            tv1.setText("loading");
        }
        pbar=(ProgressBar)findViewById(R.id.progressBar1);
        pbar.setVisibility(1);
    } else {
        setContentView(R.layout.startup_loaded); 
    }
}
Thread startUp= new Thread() {
    public void run() {
        // all the loading
    }
};

But this didn't work either: my startup_loaded never appeared. I tried finishing my thread (finish(); at the end of the runnable), but it finished by total app. Anyone knows what to do?

Upvotes: 0

Views: 263

Answers (1)

Denis Babak
Denis Babak

Reputation: 1874

Use Handler object. Or, use AsyncTask and set new contrent in onPostExecute() method.

Upvotes: 1

Related Questions