Android 应用程序在启动时挂起
我有一个需要很长时间才能加载的游戏应用程序。我过去在陈述我的比赛时没有落后,但现在是这样,我怀疑有什么事情悬而未决。是否可能是因为我加载图像的方式而挂起?以下是我所拥有的: 在onCreate中,我有:Android 应用程序在启动时挂起,android,performance,bitmap,load,android-asynctask,Android,Performance,Bitmap,Load,Android Asynctask,我有一个需要很长时间才能加载的游戏应用程序。我过去在陈述我的比赛时没有落后,但现在是这样,我怀疑有什么事情悬而未决。是否可能是因为我加载图像的方式而挂起?以下是我所拥有的: 在onCreate中,我有: v = new SView(this); // Creates my new SurfaceView a = new Accelerator(); // Adds the accelerometer new load
v = new SView(this); // Creates my new SurfaceView
a = new Accelerator(); // Adds the accelerometer
new loadPhotos().execute(""); // Initiates the AsyncTask posted below
new handleStuff().execute(""); // starts the generation of the game map
startTime = System.currentTimeMillis(); // Used to generate time lasted
setContentView(v); // Sets contentView to the SurfaceView
public class loadPhotos extends AsyncTask<String, Integer, String>
{
@Override
protected String doInBackground(String... arg0) {
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.d(null, "!!!!!!!!!!!!!----------Starting Load-----------!!!!!!!!!1");
player = BitmapFactory.decodeResource(getResources(), R.drawable.player);
playerOpaque = BitmapFactory.decodeResource(getResources(), R.drawable.player_opaque);
livesBM = BitmapFactory.decodeResource(getResources(), R.drawable.lives);
playerMissile = BitmapFactory.decodeResource(getResources(), R.drawable.player_missle);
missileButton = BitmapFactory.decodeResource(getResources(), R.drawable.missile_button);
playerBullet = BitmapFactory.decodeResource(getResources(), R.drawable.bullet);
enemyBullet = BitmapFactory.decodeResource(getResources(), R.drawable.enemy_bullet);
enemyMissile = BitmapFactory.decodeResource(getResources(), R.drawable.enemy_missle);
enemy = BitmapFactory.decodeResource(getResources(), R.drawable.enemy);
explosionBig = BitmapFactory.decodeResource(getResources(), R.drawable.explosion);
lastESpawn = System.currentTimeMillis();
background = BitmapFactory.decodeResource(getResources(), R.drawable.background_clearsky);
cloudBM1 = BitmapFactory.decodeResource(getResources(), R.drawable.cloud1);
cloudBM2 = BitmapFactory.decodeResource(getResources(), R.drawable.cloud2);
Log.d(null, "!!!!!!!!!!!!!----------Ending Load-----------!!!!!!!!!1");
}
}
这是我的日志
03-22 10:03:50.546: D/(1220): !!!!!!!!!!!!!----------Starting Load-----------!!!!!!!!!1
03-22 10:03:50.596: D/(1220): !!!!!!!!!!!!!----------Ending Load-----------!!!!!!!!!1
03-22 10:03:50.616: I/Adreno200-EGLSUB(1220): <ConfigWindowMatch:2087>: Format RGBA_8888.
03-22 10:03:50.646: W/SurfaceView(1220): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true realSizeChanged=false redrawNeeded=true left=false top=false
03-22 10:03:50.656: D/(1220): !!!!!!!!!!!!!----------Starting Load-----------!!!!!!!!!1
03-22 10:03:50.706: D/dalvikvm(1220): GC_CONCURRENT freed 2361K, 14% free 22198K/25543K, paused 11ms+2ms, total 28ms
03-22 10:03:50.726: D/(1220): !!!!!!!!!!!!!----------Ending Load-----------!!!!!!!!!1
03-22 10:03:50.746: I/Adreno200-EGLSUB(1220): <ConfigWindowMatch:2087>: Format RGBA_8888.
03-22 10:04:18.096: D/dalvikvm(1220): GC_FOR_ALLOC freed 4756K, 28% free 20236K/27975K, paused 16ms, total 18ms
03-22 10:04:18.096: I/dalvikvm-heap(1220): Grow heap (frag case) to 32.131MB for 3686416-byte allocation
您正试图通过doInBackground访问UI资源,但这是您无法做到的。这需要在onPostExecute或其他异步方法中实现,但我甚至不确定您是否希望/需要一个异步任务。您可以使用设置图像并将其加载到活动的方法中尝试按如下方式加载图像 在图像视图中 iv.setImageResourceR.drawable.imagename
这比通过decoderesource加载要快得多。您的每个图像加载都有这样一种方法。您可以发布日志吗?我怀疑@codeMagic是对的,你需要在onPreExecute方法中获得你的资源,然后在DoinBackground中对其进行操作使用此链接有效地使用AsyncTask我添加了Logcat并更新了AsyncTask ClassI建议您更仔细地阅读AsyncTask文档,因为您不了解它的作用: