当我尝试在Thread中将数据插入数据库时​​出现NullPointerException

时间:2014-01-23 22:27:59

标签: android multithreading sqlite

我正在尝试在线程中将数据插入数据库,但是我有一个错误:

01-23 17:17:58.127: E/AndroidRuntime(1400): FATAL EXCEPTION: AsyncTask #1
01-23 17:17:58.127: E/AndroidRuntime(1400): java.lang.RuntimeException: An error occured while executing doInBackground()
01-23 17:17:58.127: E/AndroidRuntime(1400):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-23 17:17:58.127: E/AndroidRuntime(1400):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
01-23 17:17:58.127: E/AndroidRuntime(1400):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
01-23 17:17:58.127: E/AndroidRuntime(1400):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
01-23 17:17:58.127: E/AndroidRuntime(1400):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-23 17:17:58.127: E/AndroidRuntime(1400):     at java.lang.Thread.run(Thread.java:841)
01-23 17:17:58.127: E/AndroidRuntime(1400): Caused by: java.lang.NullPointerException
01-23 17:17:58.127: E/AndroidRuntime(1400):     at pl.pawelfrydrych.flyingball.MyTask.insertData(MyTask.java:59)
01-23 17:17:58.127: E/AndroidRuntime(1400):     at pl.pawelfrydrych.flyingball.MyTask.doInBackground(MyTask.java:22)
01-23 17:17:58.127: E/AndroidRuntime(1400):     at pl.pawelfrydrych.flyingball.MyTask.doInBackground(MyTask.java:1)
01-23 17:17:58.127: E/AndroidRuntime(1400):     at android.os.AsyncTask$2.call(AsyncTask.java:287)

我想每1秒从MainActivity类(来自变量)添加加速位置,所以我将这些数据插入到ContentValues中,然后从contentvalues到数据库。

import java.util.Date;

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

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


        MainActivity main = new MainActivity();


        private int abc;
        float xPositionGlobal, yPositionGlobal;

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

        public void onSensorChanged(SensorEvent event){
            float xPosition = event.values[0];
            float yPosition = event.values[1];

            xPositionGlobal = xPosition;
            yPositionGlobal = yPosition;


        }


    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, xPositionGlobal);
            values.put(Database.RIGHT_POSITION, yPositionGlobal);
        //  values.put(Database.GPS, main.GPSposition);
            values.put(Database.GPS, "3");
            values.put(Database.TIME, date.toString());


            if(main.myDBAdapter.db != null){

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

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

        }

        }

MainActivity的OnClick方法:

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;

        }

    }


}

当我使用Database myDBAdapter = new Database(); ,logcat说db是null

1 个答案:

答案 0 :(得分:0)

MyTask创建一个“MainActivity”的新实例 - 其中“myDBAdapter”未初始化。您需要重新初始化或移动它。

您应该移动它,因为您可能不希望引用在后台线程上实例化的活动。