AsyncTask - 执行doInBackground()时发生错误

时间:2014-01-21 22:57:16

标签: java android android-asynctask

我在Android中遇到AsyncTask问题。当用户按下Start时,我尝试将数据从accelerometr插入到数据库中。通常,当用户按下按钮Start时,应用程序在5秒后挂起。所以我尝试将此函数放在AsyncTask中,但我遇到编译问题:

01-21 17:44:58.515: E/AndroidRuntime(1131): FATAL EXCEPTION: AsyncTask #1
01-21 17:44:58.515: E/AndroidRuntime(1131): java.lang.RuntimeException: An error occured while executing doInBackground()
01-21 17:44:58.515: E/AndroidRuntime(1131):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-21 17:44:58.515: E/AndroidRuntime(1131):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
01-21 17:44:58.515: E/AndroidRuntime(1131):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
01-21 17:44:58.515: E/AndroidRuntime(1131):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
01-21 17:44:58.515: E/AndroidRuntime(1131):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-21 17:44:58.515: E/AndroidRuntime(1131):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-21 17:44:58.515: E/AndroidRuntime(1131):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-21 17:44:58.515: E/AndroidRuntime(1131):     at java.lang.Thread.run(Thread.java:841)
01-21 17:44:58.515: E/AndroidRuntime(1131): Caused by: java.lang.NullPointerException
01-21 17:44:58.515: E/AndroidRuntime(1131):     at pl.pawelfrydrych.flyingball.MyTask.insertData(MyTask.java:37)
01-21 17:44:58.515: E/AndroidRuntime(1131):     at pl.pawelfrydrych.flyingball.MyTask.doInBackground(MyTask.java:18)
01-21 17:44:58.515: E/AndroidRuntime(1131):     at pl.pawelfrydrych.flyingball.MyTask.doInBackground(MyTask.java:1)
01-21 17:44:58.515: E/AndroidRuntime(1131):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-21 17:44:58.515: E/AndroidRuntime(1131):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-21 17:44:58.515: E/AndroidRuntime(1131):     ... 4 more

MyTask课程:

    import java.util.Date;

    import android.content.ContentValues;
    import android.os.AsyncTask;
    import android.util.Log;

    public class MyTask extends AsyncTask<Void, Void, Void> {

        private MainActivity main;
        public Database myDBAdapter;
        private int abc;

        @Override
        protected Void doInBackground(Void... arg0) {
            try {
   //18 line//          insertData();
            } catch (InterruptedException e) {
                Log.d("appname","błąd w doInBackground");
                e.printStackTrace();
            }
            return null;
        }

    public void insertData() throws InterruptedException{

            ContentValues values = new ContentValues();
            Date date = new Date();


        while(true){
            long millis = System.currentTimeMillis();
            abc++;

            values.put(Database.ID, 1+abc);
            values.put(Database.LEFT_POSITION, main.xPosition);
            values.put(Database.RIGHT_POSITION, main.yPosition);
            values.put(Database.GPS, main.GPSposition);
            values.put(Database.TIME, date.toString());


            if(myDBAdapter.db != null){

                myDBAdapter.db.insert("baza", null, values);
                Thread.sleep(1000 - millis % 1000);

            }else{
                Log.d(Database.DB_NAME,"db is null");
            }

        }

        }
    }

MainActivity类:

 public void onClick(View v) {

                switch(v.getId()){
                case R.id.bStart:


                myDBAdapter = new Database(this).open();
                        new MyTask().execute();

                case R.id.bStop:

                    myDBAdapter.close();
                    break;

                }

            }

2 个答案:

答案 0 :(得分:2)

看起来你在这里得到NPE

if(myDBAdapter.db != null){
<{1>}中的

因为您从未初始化insertData()。您在任务中声明它

myDBAdapter

但是在尝试使用它之前,你永远不会初始化它。

实际上看起来你有一个public Database myDBAdapter; 变量和一个Activity变量,所以当你在任务中声明它时,它是一个未初始化的局部变量。您可以重新初始化该变量,但如果它们相同则最好删除它并确保它是一个成员变量,如果该任务是AsyncTask的内部类。如果它是一个单独的文件,那么您将需要初始化它或传递对Activity类的构造函数的引用。

答案 1 :(得分:0)

看起来你没有用任何东西初始化private MainActivity main;。您尝试在初始化之前使用它。