【问题标题】:Black screen appearing after splash screen in android在android中启动屏幕后出现黑屏
【发布时间】:2012-04-18 05:08:10
【问题描述】:

我正在开发一个使用闪屏的应用程序。但是飞溅消失后,然后
在切换到我的主要活动之前,下一个屏幕变黑,我不想要黑屏。谁能建议我如何做到这一点? 这是我的代码。

启动器活动

public class MainActivity extends Activity{
public static final String TAG = "MainActivity";
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent in = new Intent(getApplicationContext(), SplashScreenActivity.class);
synchronized (this) {
try {
wait(50);
} catch (InterruptedException e) {
String str = e.toString();
Log.d(TAG, str);
}//catch
}//synchronized block
startActivity(in);
}//onCreate
}//class

SplashScreenAcitvity

public class SplashScreenActivity extends Activity {
private Thread splashThread;
public static final String TAG = "SplashScreenActivity";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final SplashScreenActivity splashScreen = this;

splashThread = new Thread(){
public void run(){
try {
synchronized (this) {
wait(3000);
}
} catch (Exception e) {
String str = e.toString();
Log.d(TAG, str);
}
finish();
Intent in = new Intent();
in.setClass(splashScreen, ProjectActivity.class);
startActivity(in);
stop();
}//run
};
splashThread.start();
}//onCreate

}//class

项目活动 这是主要操作将从中开始的类。

public class ProjectActivity extends TabActivity {
static int x=0;
static int color;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.tabxml);

if(x==1)
{
Dor.c=1;
color = getIntent().getExtras().getInt("color");
//Intent in=new Intent();
//in.putExtra("color",color);
}
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost();  // The activity TabHost
TabHost.TabSpec spec;  // Resusable TabSpec for each tab
Intent intent;  // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Dor.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("dor").setIndicator("Dor",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);

// Do the same for the other tabs
intent = new Intent().setClass(this, Album.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.ic_tab_album)
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Forhandler.class);
spec = tabHost.newTabSpec("forhandler").setIndicator("Forhandler",
res.getDrawable(R.drawable.ic_tab_forhandler))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Diplomat.class);
spec = tabHost.newTabSpec("diplomat").setIndicator("Diplomat",
res.getDrawable(R.drawable.ic_tab_diplomat))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}

我尝试了异步任务,但没有成功。这是我修改后的代码:

项目开始

public class ProjectStart extends AsyncTask<Context, Void, Void>{
public static final String TAG = "ProjectStart";
/*public ProjectStart() {
    super("ProjectStart");
    // TODO Auto-generated constructor stub
}*/

Context ctx;
public ProjectStart(Context context){
    this.ctx = context;
}//constructor

@Override
protected Void doInBackground(Context... params) {
    // TODO Auto-generated method stub
    Intent in = new Intent();
    in.setClass(ctx, ProjectActivity.class);
    Log.d(TAG, "doInBackground called");
    return null;
}//doInBackground

/*@Override
protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub
    Intent in = new Intent();
    in.setClass(getApplicationContext(), ProjectActivity.class);
    startActivity(in);
    Log.d("Project Start", "onHandleIntentCalled");
}*/

}//类

在 SplashScreen.java 中我做了以下修改

splashThread = new Thread() {
        public void run(){
            try {                   
                synchronized (this) {
                    wait(3000);
                }//synchronized
            }//try  
            catch(InterruptedException e) {} 
            finally {
                finish();
                new ProjectStart(sPlashScreen).execute();
                /*Intent in = new Intent(getApplicationContext(), ProjectStart.class);
                startService(in);*/
                stop();
            }//finally
            Log.d(TAG, "run method called");
        }//run
    };

还有,这里是堆栈跟踪:

  04-19 11:29:02.181: E/AndroidRuntime(341): FATAL EXCEPTION: Thread-8
04-19 11:29:02.181: E/AndroidRuntime(341): java.lang.ExceptionInInitializerError
04-19 11:29:02.181: E/AndroidRuntime(341):  at abc.com.camera.SplashScreen$1.run(SplashScreen.java:32)
04-19 11:29:02.181: E/AndroidRuntime(341): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
04-19 11:29:02.181: E/AndroidRuntime(341):  at android.os.Handler.<init>(Handler.java:121)
04-19 11:29:02.181: E/AndroidRuntime(341):  at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
04-19 11:29:02.181: E/AndroidRuntime(341):  at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
04-19 11:29:02.181: E/AndroidRuntime(341):  at android.os.AsyncTask.<clinit>(AsyncTask.java:152)
04-19 11:29:02.181: E/AndroidRuntime(341):  ... 1 more


    04-19 11:29:02.181: E/AndroidRuntime(341): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

我不明白是什么问题。

【问题讨论】:

  • 嗨,你能告诉我你在启动画面和主要活动中做了什么
  • 尽量减少 MainActivity 的 onCreate() 中的代码。
  • @PankajKumar 我已经发布了代码,请检查一次。

标签: android splash-screen


【解决方案1】:

但是闪屏消失后,切换前屏幕变黑 到我的主要活动

看来您可能正在 MainActivity 的 UI 上执行一些后台进程,这需要时间来执行后台进程。如果是这样使用

  • 异步任务
  • 服务
  • 意图服务

用于后台进程。如果没有显示主活动它有什么代码。

【讨论】:

    【解决方案2】:

    我认为最适合您的解决方案是asynctask

    Given 是来自开发者网站的示例。 Asynctask 主要有四种方法。

    onPreExecute()
    
    doInBackground(Params...)
    
    onProgressUpdate(Progress...)
    
    onPostExecute(Result)
    

    这是另一个Example

    【讨论】:

    • 对不起,我错误地修改了你的帖子。根据 ypur 的建议,我尝试了异步任务,但没有成功。
    • 使用异步任务我得到了这个异常:E/AndroidRuntime(341): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()跨度>
    • @nitish :你能看看这个博客吗? androidpartaker.wordpress.com/2010/08/01/android-async-task
    【解决方案3】:

    【讨论】:

    • 拒绝投票作为答案并没有试图解释问题的原因可能是什么。 OP 不是在问如何制作启动画面,这是他们面临的特定问题。简单地添加一些教程的链接就相当于建议它们只是谷歌。
    • @Ash 很酷,没问题,这是我的错 :(。我应该给出一个解决方案,无论如何我在第一次来 stackoverflow 时回答了。我在第一次学习时犯错误并不总是。:) 谢谢
    【解决方案4】:

    为您的活动意图添加一个标志.. setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)

    【讨论】:

      猜你喜欢
      • 2014-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多