我们知道,当应用程序做一些漫长的过程,比如从互联网上下载一些信息时,它可能会在加载应用程序之前显示启动画面,当应用程序完全加载时,它将显示主页面 . 在启动画面活动中,我们必须在线程中加载长进程,以避免在加载应用程序之前显示黑屏 . 我做了所有这些 . 但在显示应用程序之前还会出现黑屏 . 这是启动画面活动的onCreate方法:
protected override void OnCreate (Bundle bundle)
{
try {
base.OnCreate (bundle);
//_dt = DateTime.Now.AddSeconds (_splashTime);
SetContentView (Resource.Layout.Splash );
FirstLoadPB= FindViewById <ProgressBar >(Resource .Id.FirstLoadPB );
FirstLoadingInfo= FindViewById <TextView >(Resource .Id.FirstLoadInfo );
LoadApplication ();
} catch (System.Exception ex) {
Common.HandleException (ex);
}
}
这是 LoadApplication
方法的代码:
public void LoadApplication()
{
new System.Threading.Thread (new ThreadStart (() =>
{
//Some Codes to load applications- Downloading from web and accessing the storage(Because was many codes - about 100 line- i was clear them.
}
)
).Start ();
}
我不明白为什么黑屏会出现,现在该如何避免 . 我有一些代码可以在我的应用程序类的oncreate中访问存储 . 也许问题的根本原因就在那里 . 因此我分享了它的代码:
public override void OnCreate ()
{
try {
base.OnCreate ();
_typeOfShow = new MapViewType ();
ListingTypes = new Dictionary<int,ListingTypeItem> ();
OfflineMode =false;
PropertyShowWasShown = false;
MeasutingUnitsChanged =false;
if(RplXmlSettings .Instance .getVal (AppConstants .XmlSettingShowOnCurrentLocationKey )== "True")
typeOfShow .ShowOnCurrentLocation =true ;
else
typeOfShow .ShowOnCurrentLocation =false;
//StorageClass .ctx = ApplicationContext ;
FillDashboardOnResume =false;
//initlize image loader
ImageLoader = Com.Nostra13.Universalimageloader.Core.ImageLoader.Instance;
Options = new DisplayImageOptions.Builder ()
.ShowImageForEmptyUri (Resource.Drawable.ic_tab_map)
.CacheOnDisc ()
.CacheInMemory ()
.ImageScaleType (ImageScaleType.InSampleInt)
.BitmapConfig (Bitmap.Config.Rgb565)
.Displayer (new FadeInBitmapDisplayer (300))
.Build ();
ImageLoaderConfiguration config;
ImageLoaderConfiguration .Builder builder =new ImageLoaderConfiguration
.Builder (ApplicationContext).ThreadPoolSize (3);
if(RplXmlSettings .Instance .getVal (AppConstants .XmlSettingMemoryCacheKey )== "True")
builder .ThreadPriority (4).MemoryCacheSize (1500000) ;// 1.5 Mb
builder .
DenyCacheImageMultipleSizesInMemory ().
DiscCacheFileNameGenerator (new Md5FileNameGenerator ()).
MemoryCache (new WeakMemoryCache()).
DiscCacheSize (15000000);
config = builder .Build ();
ImageLoader.Init (config);
} catch (Exception ex) {
Common .HandleException (ex);
}
}
OK.Long story short . 现在问题是这个 - 真的是这个黑屏的根本原因是什么 . 这是来自启动活动还是来自应用程序类 . 我们如何解决它并避免形式表现出来?
7 回答
将具有您正在使用的背景的主题添加到清单文件中的应用程序标记,以防止绘制黑屏 .
theme.xml
AndroidManifest.xml中
Read why there is a black screen here
您看到的初始屏幕称为“预览”屏幕 . 您可以通过在主题中声明这一点来完全禁用它:
有关如何处理此屏幕的说明,请点击此处:http://cyrilmottier.com/2013/01/23/android-app-launching-made-gorgeous/
将
AndroidManifest.xml
中的这一行添加到启动器活动中:android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen
您可以通过将图像转换为画笔(颜色)来解决此错误 .
在drawable文件夹中添加新文件xml(splash_bg.xml)文件,如下所示 .
现在添加一个新样式,并将splash_bg.xml应用为背景颜色 .
将此新样式应用于主启动器活动或启动画面 .
如果你在onCreate中调用一些“重代码”,屏幕将显示为黑色,直到完成加载 . 您可以考虑使用AsyncTask并使onCreate处理setContentView等,并使AsyncTask处理“重代码” .
避免这个问题的更好的解决方案是使用AsyncTask,这是我在我的ListActivity中使用的示例代码:
在onCreate Methode里面实现这个:
这是一些值得思考的东西;也许你的应用程序根本没有相当大的初始化延迟;事实上,你可能是 waiting for the instant run service .
根据我的经验,这样做的症状是你的应用程序在初始化时会显示一个很长的黑屏,但是经过调试你发现你的所有方法都没有在它可见时被调用 .