1

我想创建一个线程来更改我的活动布局...我有 2 个布局:welcomepage 和 activity_main ...线程的目标:当我启动我的应用程序时,welcomepage 布局将在 5 秒内可见并且之后布局再次将是 activity_main ...

我写的代码如下:

package com.example.tripolimazad;

import android.os.Bundle;  
import android.app.Activity; 
import android.view.Menu; 
import android.widget.TextView;

public class MainActivity extends Activity {

    public TextView counter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcomepage);
        counter = (TextView) findViewById(R.id.Counter);

        Thread th=new Thread(){ 
            @Override
            public void run(){
                try
                {  
                            Thread.sleep(10000);  
                            setContentView(R.layout.activity_main);   
                }catch (InterruptedException e) {
                } 
            }

        };
        th.start();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

但它不起作用!有没有人有任何解决方案PLZ!

4

4 回答 4

2

您不能在非 UI 线程上更改 UI,但是在 中Activity,您可以使用以下runOnUiThread方法:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        setContentView(R.layout.activity_main);
    }
});

不过,这似乎很奇怪,在你的onCreate.

于 2013-08-22T20:15:57.363 回答
1

您也可以尝试使用CountDownTimer,例如:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    
    setContentView(R.layout.welcomepage);
    //display the logo during 5 secondes,
    new CountDownTimer(5000,1000){
        @Override
        public void onTick(long millisUntilFinished){} 

        @Override
        public void onFinish(){
               //set the new Content of your activity
               MainActivity.this.setContentView(R.layout.main);
        }
   }.start();
   //...
}

有关更多信息,请参阅在应用程序启动时显示徽标几秒钟

于 2013-08-22T20:22:11.987 回答
0

UI 相关的操作必须只在 UI 线程上完成,不能像你正在做的那样在非 UI 线程中完成。但是您可以从非 UI 线程更新 UI,如下所示:

activityContext.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        setContentView(R.layout.activity_main);
    }
});

但是,对于您提到的情况,更好的方法是使用 Android 提供的异步任务。您可以尝试以下操作:

/*
 * Activity/Thread to display the **welcomepage** 
 * when the app is started.
 */
public class SplashActivity extends Activity {

    // how long until we go to the next activity
    protected int _splashTime = 5000; // 5 seconds

    // thread to display welcome page
    private Thread splashTread;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

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

        // thread for displaying the WelcomeScreen
        splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    synchronized (this) {
                        // wait 5 sec
                        wait(_splashTime);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    // finish the current splashactivity 
                    finish();                   
                    // start MainActivity as next activity
                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                }
            }
        };

        // start the thread.
        splashTread.start();
    }


}
于 2013-08-22T20:23:32.927 回答
0

或者,您可以使用处理程序来避免创建新线程,如下所示:

    getWindow().getDecorView().getHandler().postDelayed(new Runnable() {
        @Override public void run() {
            setContentView(R.layout.activity_main);
        }
    }, 10000);
于 2013-08-22T20:23:43.360 回答