0

我在一个文本文件中有一些数据,我想在启动画面对用户可见时填​​充数据库。我用 AsyncTask 进行了尝试,但出现此错误 Can't create handler inside thread that has not called Looper.prepare()我知道当我们尝试在 AsyncTask 线程中更新 UI 时会引发此错误。但我不会做那样的事情。请帮忙。我是安卓新手。这是我的代码:AsyncTask:

       `public class LoadingTask extends AsyncTask<String, Integer, Integer>'

doInBackground 方法

        protected Integer doInBackground(String... params) {
    // TODO Auto-generated method stub
    MainActivity main = new MainActivity();
    return 1234;
    }   

从 MainActivity 构造函数中,我调用填充数据库的 DbHelper 类。

        `public  MainActivity(){
    Words word = new Words(getApplicationContext());
    try {
        word.open();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }`

我的 DbHelper onCreate 执行此操作。

        public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        mDatabase = db;
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + 
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_WORD + " TEXT NOT NULL, " + 
                KEY_MEANING + " TEXT NOT NULL);"
            );
        try {
            loadWords();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

loadWords 函数是:

 private void loadWords() throws IOException {
          Log.d(TAG, "Loading words...");
           final Resources resources = mHelperContext.getResources();
           InputStream inputStream = resources.openRawResource(R.raw.definitions);
           BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
         Words uff = new Words(ApplicationContextProvider.getContext());
         //  uff.close();
          // String line
               String line;
               try {
                    //if(!uff.ourDatabase.isOpen())
                        uff.open();

              // ourHelper.getWritableDatabase();
               while ((line = reader.readLine()) != null) {
                   String[] strings = TextUtils.split(line, "-");
                   if (strings.length < 2) continue;
                   Log.d(TAG, "going to insert");
                   long id = createEntry(strings[0].trim(), strings[1].trim());
                   Log.d(TAG, "After insert " + id);
                   if (id < 0) {
                       Log.e(TAG, "unable to add word: " + strings[0].trim());
                   }
               }
               Log.d(TAG, "after whille loop");
               }

               catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                       Log.d(TAG, "open fail");

     } finally {
            uff.close();
               reader.close();
           }
           Log.d(TAG, "DONE loading words.");

}

这就是我的 logcat 所说的:

    07-06 18:41:20.604: W/dalvikvm(2364): threadid=11: thread exiting with uncaught exception         (group=0x41fac2a0)
    07-06 18:41:20.624: E/AndroidRuntime(2364): FATAL EXCEPTION: AsyncTask #1
    07-06 18:41:20.624: E/AndroidRuntime(2364): java.lang.RuntimeException: An error occured while executing  doInBackground()
    07-06 18:41:20.624: E/AndroidRuntime(2364):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
    07-06 18:41:20.624: E/AndroidRuntime(2364):     at  java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
    07-06 18:41:20.624: E/AndroidRuntime(2364):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
    07-06 18:41:20.624: E/AndroidRuntime(2364):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
    07-06 18:41:20.624: E/AndroidRuntime(2364):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    07-06 18:41:20.624: E/AndroidRuntime(2364):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    07-06 18:41:20.624: E/AndroidRuntime(2364):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
    07-06 18:41:20.624: E/AndroidRuntime(2364):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
    07-06 18:41:20.624: E/AndroidRuntime(2364):     at java.lang.Thread.run(Thread.java:856)
    07-06 18:41:20.624: E/AndroidRuntime(2364): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
    07-06 18:41:20.624: E/AndroidRuntime(2364):     at android.os.Handler.<init>(Handler.java:121)
    07-06 18:41:20.624: E/AndroidRuntime(2364):     at android.app.Activity.<init>(Activity.java:772)
    07-06 18:41:20.624: E/AndroidRuntime(2364):     at com.sourabh.words.MainActivity.<init>(MainActivity.java:32)
    07-06 18:41:20.624: E/AndroidRuntime(2364):     at com.sourabh.words.LoadingTask.doInBackground(LoadingTask.java:69)
    07-06 18:41:20.624: E/AndroidRuntime(2364):     at com.sourabh.words.LoadingTask.doInBackground(LoadingTask.java:1)
    07-06 18:41:20.624: E/AndroidRuntime(2364):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
    07-06 18:41:20.624: E/AndroidRuntime(2364):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    07-06 18:41:20.624: E/AndroidRuntime(2364):     ... 5 more
    07-06 18:41:21.009: D/libEGL(2364): loaded /system/lib/egl/libEGL_mali.so
4

1 回答 1

1

正如 Luksprog 所说,您不能调用 Activity 类的构造函数——这是 Android 框架的工作(由于 Activity 本质上是一个 UI 概念,因此超类构造函数涉及 UI 操作也就不足为奇了)

如果您需要从现有的 Activity 实例中引用某些内容,请获取/保留/传递对现有类实例的引用 - 您创建的实例与现有实例不同new

如果您打算创建一个 Activity 的新实例,您可以使用 Intent 要求 Android 执行此操作 - 您不需要自己执行此操作。

于 2013-07-06T14:30:48.573 回答