android,asynctask,在doInBackground()中写入/读取数据库失败

时间:2014-01-02 15:39:00

标签: java android android-asynctask android-sqlite

美好的一天,

我使用Load()方法下载并写入MainActivity中的数据库。 Load()方法是在doInBackground()中的asynctask类中调用的(asynctask类在MainActivity中)并且它运行良好。

但是,我不知道为什么它不适用于另一堂课。我在Load()中创建了另一个asynctask类和调用方法。 Load()

中的行db.updateContactdb.addContact出错

Load()方法

 public List<Contact> Load()
{
    final String URL = "http://192.168.1.110/index.xml";
    // XML node keys
    final String KEY_SONG = "data"; // parent node
    final String KEY_KEY = "id";
    final String KEY_NAME = "temperature";
    final String KEY_TEMPERATURE = "humidity";
    final String KEY_LATITUDE = "latitude";
    final String KEY_STATUS = "status";
    Databaze db = new Databaze(this);
    List<Contact> contacts;

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML from URL
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_SONG);
    // looping through all mista nodes <mista>
    for (int j = 0; j < nl.getLength(); j++)
    {
        Element e = (Element) nl.item(j);

        String name = parser.getValue(e, KEY_NAME);
        String temperature = parser.getValue(e, KEY_TEMPERATURE);
        String latitude = parser.getValue(e, KEY_LATITUDE);
        String status = parser.getValue(e, KEY_STATUS);
        String key = parser.getValue(e, KEY_KEY);
        int keyINT = Integer.valueOf(key);

            if(keyINT == 1){
            db.updateContact(new Contact(keyINT, name, temperature, latitude, status));

            }
            else{
    db.addContact(new Contact(keyINT, name, temperature, latitude, status));
            }
    }
    contacts = db.getAllContacts();

    return contacts;
}

的AsyncTask

public class LoadDataTask extends AsyncTask<Void, Void, List<Contact>> {
private MainActivity mMainActivity;

@Override
protected void onPreExecute()
{
    mMainActivity = new MainActivity();
}

@Override
protected List<Contact> doInBackground(Void... voids)
{
    return mMainActivity.Load();
}

@Override
protected void onPostExecute(List<Contact> cont)
{
    // do something
}

}

错误代码

    FATAL EXCEPTION: AsyncTask #2
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
at     android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
at com.example.hello.Databaze.updateContact(Databaze.java:171)
at com.example.hello.MainActivity.Load(MainActivity.java:245)
at com.example.hello.MyWidgetIntentReceiver$LoadDataTask.doInBackground(MyWidgetIntentReceiver.java:156)
at com.example.hello.MyWidgetIntentReceiver$LoadDataTask.doInBackground(MyWidgetIntentReceiver.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
... 4 more

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

如果MainActivity是Activity类,则不应创建活动类的实例。仅在清单文件中声明它。它有生命周期。

 mMainActivity = new MainActivity();

这是您获得NPE mMainActivity.Load();

的地方

请检查以下链接

Can i Create the object of a activity in other class?

您可以在doInbackground本身写入数据库。