0

当我在 android 模拟器上运行我的应用程序时,log-cat 告诉我它跳过了 70 帧(当应用程序开始启动时)并且它可能在主线程上做的太多了。如何分散负载或提高应用程序的性能。我需要在启动时加载屏幕吗?

这是我的启动活动,

package com.the.maze;

import android.app.ListActivity;  
import android.content.Intent;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.ArrayAdapter;  
import android.widget.ListView;

    public class TheMazeActivity extends ListActivity{  
            String list[]={"New Game","Highscores","How to play","Settings","About"};
            Class classes[]={GameActivity.class,null,Instructions.class,null,null};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, list));

    }
        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        try{
        Intent intent= new Intent(TheMazeActivity.this,classes[position]);
        startActivity(intent);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
4

1 回答 1

0

我可以建议您的改进不多,而且我没有在您的代码中看到繁重的计算(如果有的话!)。无论如何,这些是我的建议:

public class TheMazeActivity extends ListActivity{
    //make them static final  
    private static final String[] list={"New Game","Highscores","How to play","Settings","About"};
    private stativc final Class[] classes={GameActivity.class,null,Instructions.class,null,null};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, list));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        //no need for try catch, if you can perfectly find out, when to trigger!
        if [classes[position] != null){
            Intent intent= new Intent(TheMazeActivity.this,classes[position]);
            startActivity(intent);
        }
   }
}

但请注意,这些更改只是所谓的微优化

于 2012-07-18T12:45:25.047 回答