3

我正在为 android 3.2 使用严格模式,并且StrictModeDiskReadViolationonCreate我的Activity.

我试图将执行 SQL 查询的代码移至:

  • 一个新的Thread.
  • 一个新的AsyncTaskLoader.
  • 一个新的AsynTask.

问题只是AsyncTask让违规消失了,我想知道为什么其他两种方法不起作用?

这是我的代码:

AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                try {
                    Dao<Listino, Integer> dao = DatabaseHelper.getHelper(ListinoActivity.this).getListinoDao();
                    if (dao.countOf() == 1)
                    {
                        long id = dao.queryForAll().get(0).getId();//long non int
                        final Intent intent = new Intent(ListinoActivity.this, ListinoProdottiActivity.class);
                        intent.putExtra("listino_id", id);
                        intent.setAction(Intent.ACTION_MAIN);
                        ListinoActivity.this.finish();
                        ListinoActivity.this.startActivity(intent);
                    }
                } catch (SQLException e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            MyToast.makeText(ListinoActivity.this, "Errore ottenere i listini", MyToast.LENGTH_SHORT).show();
                        }
                    });

                    e.printStackTrace();
                }
                return null;
            }

        };
        asyncTask.execute();

        AsyncTaskLoader async = new AsyncTaskLoader(this) {


            @Override
            public Object loadInBackground() {
                //do stuff, violation still here
                return null;
            }
        };

        async.loadInBackground();

        Thread t = new Thread() {
            @Override
            public void run() {
                super.run();
                //do stuff, violation still here

            }
        };
        t.run();
4

1 回答 1

3

你没有分叉一个Thread. 要 fork 一个Thread,你调用start(). 您调用run()了 ,它只是run()在当前线程上运行您的方法。

而且您甚至没有接近Loader正确使用该框架。您那里的代码不仅存在与您ThreadLoader.

于 2012-06-19T11:55:12.590 回答