2012-04-18 79 views
0

我正在開發一個應用程序,我在其中使用啓動畫面。但在飛濺消失後,然後
在切換到我的主要活動之前,下一個屏幕變黑,我不想黑屏。任何人都可以請建議我如何做到這一點? 這是我的代碼。在Android中的啓動畫面後出現的黑色屏幕

發射活動

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); 
} 
} 

我嘗試了異步任務,但它沒有奏效。這裏是我修改後的代碼:

ProjectStart

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() 

我沒有得到什麼問題。

+0

您的代碼在哪裏? – 2012-04-18 05:10:08

+0

嗨,你能告訴我你在啓動畫面中做了什麼,主要活動 – 2012-04-18 05:10:28

+0

試圖在MainActivity的onCreate()中儘可能多地減少代碼。 – Richa 2012-04-18 05:17:05

回答

1

我認爲你最好的解決辦法是asynctask

由於是從開發者網站的例子。 Asynctask主要有四種方法。

onPreExecute() 

doInBackground(Params...) 

onProgressUpdate(Progress...) 

onPostExecute(Result) 

而這是其他Example

+0

對不起,我錯誤地修改了你的帖子。根據ypur的建議,我嘗試了異步任務,但沒有奏效。 – Nitish 2012-04-19 05:33:57

+0

使用異步任務我得到這個異常:E/AndroidRuntime(341):引起:java.lang.RuntimeException:不能創建處理程序內部線程沒有調用Looper.prepare() – Nitish 2012-04-20 05:08:24

+0

@nitish:你可以只有一個看看這個博客。 http://androidpartaker.wordpress.com/2010/08/01/android-async-task/ – Bhavin 2012-04-20 05:12:27

2

但飛濺消失後,畫面切換 我的主要活動

看來,你可能會做的是花時間做在MainActivity的UI一些後臺進程之前變成黑色後臺進程。如果是這樣的使用

  • 的AsyncTask
  • 服務
  • IntentService

爲後臺進程。如果沒有顯示主要活動它有什麼代碼。

0

給您的活動意圖添加一個標誌.. setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)

相關問題