主要活动未开放

时间:2013-04-13 18:21:01

标签: android

我的应用程序似乎启动正常,启动画面和内容。但是当它睡了6秒并且它应该进入主要活动时应用程序会崩溃任何帮助吗?

这是我的代码(android.intent.action1.MAINACTIVIVTY,“action”故意改为“action1”)

package com.hellhogone.multitools;

import com.hellhogone.multitools.R;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Window;
import android.view.WindowManager;

public class Splash extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.splash);

    MediaPlayer yo = MediaPlayer.create(Splash.this, R.raw.smusic); 
    yo.start(); 

    Thread timer = new Thread(){
        public void run(){
            try{
                sleep(6000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }finally{
                Intent h1 = new Intent("android.intent.action1.MAINACTIVITY");
                startActivity(h1); 
            }
        }
    };

    timer.start(); 
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish(); 
}



}

1 个答案:

答案 0 :(得分:1)

您无法从UI线程以外的其他线程启动活动。要避免此问题,您可以使用runOnUiThread()

    }finally{
        runOnUiThread(new Runnable() {
            public void run() {
                Intent h1 = new Intent("android.intent.action1.MAINACTIVITY");
                startActivity(h1);
            }
        });
    }