Android:onCreate时的白屏

时间:2016-03-31 20:13:13

标签: android

我有一个应用程序,在启动时在onCreate方法中复制资产文件夹中的数据。它在三个循环中完成,每个循环都有活动指示器,问题是当前两个循环运行白屏显示时,只有当第三个循环开始时,我才能看到带有指示器的活动屏幕。

代码如下

Realm realm;
ListView list;
int[] imageidsm = {R.drawable.fon_sovety350, R.drawable.fon_german350, R.drawable.fon_usa350, R.drawable.fon_uk350, R.drawable.fon_fr_it200, R.drawable.fon_japan_china200, R.drawable.fon_history200};
String[] itemname =  {"СССР", "ГЕРМАНИЯ", "США", "ВЕЛИКОБРИТАНИЯ", "ФРАНЦИЯ И ИТАЛИЯ", "ЯПОНИЯ И КИТАЙ", "ИСТОРИЯ"};
Boolean firstLaunch = false;
SharedPreferences preferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    int height = metrics.heightPixels;
    int width = metrics.widthPixels;

    MainAdapter adapter = new MainAdapter(this, itemname, imageidsm, height, width);

    list = (ListView) findViewById(R.id.mainListView);
    list.setAdapter(adapter);

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (position == 2) {
                Intent toSssr = new Intent(MainActivity.this, TankListActivity.class);
                toSssr.putExtra("category", "СССР");
                startActivity(toSssr);
            } else if (position == 3) {
                Intent listActivity = new Intent(MainActivity.this, ArticleListActivity.class);
                startActivity(listActivity);
            }
        }
    });

    RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
            .name("db.realm")
            .build();
    realm.setDefaultConfiguration(realmConfiguration);

    realm = Realm.getDefaultInstance();

    preferences = getApplicationContext().getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
    firstLaunch = preferences.getBoolean("firstLaunch", false);

    if (firstLaunch == false) {
        firstLaunch();
    }

}

public void firstLaunch() {

    String[] arrayOfCatLists = {"00f.json", "01f.json", "02f.json", "10f.json"};
    String[] arrayOfArticles = {"32.json", "34.json", "44.json", "51.json", "33.json", "40.json", "41.json", "42.json", "52.json", "45.json", "37.json", "46.json", "36.json", "54.json", "35.json",  "43.json", "47.json", "50.json", "49.json", "48.json", "56.json", "58.json", "53.json", "59.json" , "55.json", "60.json", "61.json"};
    String[] arrayOfUsssr = {"62.json", "74.json", "75.json", "76.json", "63.json", "78.json", "79.json", "77.json", "81.json", "80.json"};

    for (int i = 0; i < arrayOfCatLists.length; i++) {
        new GetArticlesListFromDisk(arrayOfCatLists[i], i).execute();
    }

    for (int i = 0; i < arrayOfArticles.length; i++) {
        new GetArticleFromDisk(arrayOfArticles[i]).execute();
    }

    for (int i = 0; i < arrayOfUsssr.length; i++) {
        new GetTanksFromDisk(arrayOfUsssr[i]).execute();
    }

    firstLaunch = true;

    SharedPreferences.Editor editor = preferences.edit();
    editor.putBoolean("firstLaunch", firstLaunch);
    editor.apply();
}

private class GetArticlesListFromDisk extends AsyncTask<String, Void, String> {

    private String id;
    private int index;

    String[] arrayOfCatLists = {"00f.json", "01f.json", "02f.json"};

    private GetArticlesListFromDisk(String id, int index) {
        this.id = id;
        this.index = index;
    }

    ProgressDialog pd = new ProgressDialog(MainActivity.this);

    @Override
    protected String doInBackground(String... params) {

        String json = null;

        try {
            InputStream input = getApplicationContext().getAssets().open(id);
            int size = input.available();
            byte[] buffer = new byte[size];
            input.read(buffer);
            input.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pd.setCancelable(false);
        pd.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
        pd.setMessage("Минуточку, загружаемся");
        pd.show();
    }

    @Override
    protected void onPostExecute(String strJson) {
        super.onPostExecute(strJson);

        pd.dismiss();

        JSONObject dataJsonObj = null;
        String category = "";

        try {
            dataJsonObj = new JSONObject(strJson);
            JSONArray listing = dataJsonObj.getJSONArray("listing");

            for (int i = 0; i < listing.length(); i++) {
                JSONObject object = listing.getJSONObject(i);

                String id = object.getString("id");
                String title = object.getString("title");
                String subtitle = object.getString("subtitle");
                String image = object.getString("image");

                InputStream inputStream =null;
                Bitmap bitmap = null;

                try {
                    inputStream = getAssets().open(image);
                    bitmap = BitmapFactory.decodeStream(inputStream);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                Log.d("getArticleFromDisk", "Saved article " + title);

                ImageStorage.saveToSdCard(bitmap, image, getApplicationContext());

                if (index == 0) {
                    category = "Танковые сражения";
                } else if (index == 1) {
                    category = "Справочник танкиста";
                } else if (index == 2) {
                    category = "Танковые асы";
                } else if (index == 3) {
                    category = "СССР";
                } else if (index == 4) {
                    category = "Германия";
                } else if (index == 5) {
                    category = "США";
                } else if (index == 6) {
                    category = "Великобритания";
                }
                realm.beginTransaction();
                ArticleList articleList = realm.createObject(ArticleList.class);
                articleList.setId(id);
                articleList.setTitle(title);
                articleList.setSubtitle(subtitle);
                articleList.setImage(image);
                articleList.setCategory(category);
                realm.commitTransaction();

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}


private class GetArticleFromDisk extends AsyncTask<String, Void, String> {

    private String id;
    private int categoryIndex;

    private GetArticleFromDisk(String id) {
        this.id = id;
    }

    public String LOG_TAG = "GetArticleFromDisk";

    HttpURLConnection urlConnection = null;
    BufferedReader reader = null;
    String resultJson = "";

    ProgressDialog pd = new ProgressDialog(MainActivity.this);

    @Override
    protected String doInBackground(String... params) {

        String json = null;

        try {
            InputStream input = getApplicationContext().getAssets().open(id);
            int size = input.available();
            byte[] buffer = new byte[size];
            input.read(buffer);
            input.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pd.setCancelable(false);
        pd.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
        pd.setMessage("Минуточку, загружаемся");
        pd.show();
    }

    @Override
    protected void onPostExecute(String strJson) {
        super.onPostExecute(strJson);

        pd.dismiss();

        JSONObject dataJsonObj = null;
        String category = "";

        try {
            dataJsonObj = new JSONObject(strJson);
            JSONArray listing = dataJsonObj.getJSONArray("article");

            for (int i = 0; i < listing.length(); i++) {
                JSONObject object = listing.getJSONObject(i);

                String id = object.getString("id");
                String title = object.getString("title");
                String subtitle = object.getString("subtitle");
                String body = object.getString("body");
                String hash = object.getString("content_version");

                Log.d(LOG_TAG, "Saved article with id " + id);

                realm.beginTransaction();
                Article article = realm.createObject(Article.class);
                article.setId(id);
                article.setTitle(title);
                article.setSubtitle(subtitle);
                article.setBody(body);
                article.setHash(hash);
                realm.commitTransaction();


            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

 private class GetTanksFromDisk extends AsyncTask<String, Void, Tank> {

    private String id;
    private int categoryIndex;

    private GetTanksFromDisk(String id) {
        this.id = id;
    }

    public String LOG_TAG = "GetTankFromDisk";

    HttpURLConnection urlConnection = null;
    BufferedReader reader = null;
    String resultJson = "";

    ProgressDialog pd = new ProgressDialog(MainActivity.this);
    Tank tank = new Tank();

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        Log.d(LOG_TAG, "Entered preExecute");

        pd.setCancelable(false);
        pd.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
        pd.setMessage("Минуточку, загружаемся");
        pd.show();
    }

    @Override
    protected Tank doInBackground(String... params) {

        String json = null;

        try {
            InputStream input = getApplicationContext().getAssets().open(id);
            int size = input.available();
            byte[] buffer = new byte[size];
            input.read(buffer);
            input.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }

        JSONObject dataJsonObj = null;
        String category = "";

        try {
            dataJsonObj = new JSONObject(json);
            JSONArray listing = dataJsonObj.getJSONArray("article");

            for (int i = 0; i < listing.length(); i++) {
                JSONObject object = listing.getJSONObject(i);

                String id = object.getString("id");
                String title = object.getString("title");
                JSONArray signatures = object.getJSONArray("signatures");

                ArrayList<String> signatures_list = new ArrayList<String>();

                for (int j = 0; j < signatures.length(); j++) {
                    signatures_list.add(signatures.get(j).toString());
                }

                String signatures_string = Joiner.on(",").join(signatures_list);

                String body = object.getString("body");
                String construction = object.getString("construction");
                String modification = object.getString("modification");
                String ttx = object.getString("ttx");
                JSONObject images = object.getJSONObject("images");
                JSONArray tank_slider = images.getJSONArray("tank_slider");

                ArrayList<String> tank_slider_list = new ArrayList<String>();

                for (int k = 0; k < tank_slider.length(); k++) {

                    InputStream inputStream =null;
                    Bitmap bitmap = null;

                    try {
                        inputStream = getAssets().open(tank_slider.getString(k));
                        bitmap = BitmapFactory.decodeStream(inputStream);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }


                    ImageStorage.saveToSdCard(bitmap, tank_slider.getString(k), getApplicationContext());

                    tank_slider_list.add(tank_slider.getString(k));
                }

                String tank_slider_string = Joiner.on(",").join(tank_slider_list);

                String hash = object.getString("content_version");

                Log.d(LOG_TAG, "Imported from assets tank with id " + id);

                tank.setId(id);
                tank.setTitle(title);
                tank.setSignatures(signatures_string);
                tank.setBody(body);
                tank.setConstruction(construction);
                tank.setModification(modification);
                tank.setTtx(ttx);
                tank.setTank_slider(tank_slider_string);
                tank.setHash(hash);

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return tank;
    }

    @Override
    protected void onPostExecute(Tank tank) {
        super.onPostExecute(tank);

        pd.dismiss();

        realm.beginTransaction();
        Tank newTank = realm.createObject(Tank.class);
        newTank.setId(tank.getId());
        newTank.setTitle(tank.getTitle());
        newTank.setSignatures(tank.getSignatures());
        newTank.setBody(tank.getBody());
        newTank.setConstruction(tank.getConstruction());
        newTank.setModification(tank.getModification());
        newTank.setTtx(tank.getTtx());
        newTank.setTank_slider(tank.getTank_slider());
        newTank.setHash(tank.getHash());
        realm.commitTransaction();

    }
}

我做错了什么?

0 个答案:

没有答案