从AsyncTask触发时SQLiteOpenHelper.getWritableDatabase上的NullPointerException

时间:2012-07-20 20:22:24

标签: java android sqlite

我在这里收到java.lang.NullPointerException错误:

com.mysite.datasources.ArticlesDataSource.open(ArticlesDataSource.java:88)

代码是这样的:

public void open() throws SQLException
{
    database = dbHelper.getWritableDatabase(); //this is line 88
}

importArticles()内的ArticlesDataSource函数调用它。

从我的SplashScreenActivity调用时,此功能正常:

articlesDataSource = new ArticlesDataSource(context);
try {
    articlesDataSource.importArticles(Section.currentSection, false);
    ...

但是当我尝试从我的ImportArticlesAsyncMainActivity内)调用它时,我收到NullPointerException错误。

//This is within my MainActivity class

    //...
    ImportArticlesAsync importArticlesAsync = new ImportArticlesAsync(this);
    importArticlesAsync.execute("");
}

private class ImportArticlesAsync extends AsyncTask<String, Void, String>
{

    Context mContext;
    //ProgressDialog waitSpinner;
    private ArticlesDataSource articlesDataSource;
    //ConfigurationContainer configuration = ConfigurationContainer.getInstance();

    public ImportArticlesAsync(Context context) {
        mContext = context;
        //waitSpinner = new ProgressDialog(this.context);
    }

    @Override
    protected String doInBackground(String... params)
    {
        //IMPORTS THE ARTICLES FROM THE RSS FEED (adds or updates)
    //tried just mContext //notice
    //tried mContext.getApplicationContext() //ERROR / crash

        articlesDataSource = new ArticlesDataSource(mContext);
        try {
            articlesDataSource.importArticles(Section.currentSection, false);
        } catch (Exception e) {
            Toast toast = Toast.makeText(
                    mContext,
                    "NOTICE: Could not import/update " + Section.currentSection.toString() + " articles.",
                    Toast.LENGTH_LONG);
            toast.show();
        }
        return null;
    }


    @Override
    protected void onPostExecute(String result) {

        //loads article from database to the list
        loadArticlesIntoList(Section.currentSection);
}
}

从我所读到的,大多数人认为它必须是不正确的背景。但是 - 我还没有真正理解上下文,并且已经尝试了我能想到的各种方式(并且阅读了很多关于上下文的内容)。


我尝试对“代码”进行图表/清洁说明:

//YAY this works!
SplashScreenActivity
    -LoadApplication (AsyncTask)
        -doInBackground
             articlesDataSource = new ArticleDataSource(context);
             articlesDataSource.importArticles(Section.currentSection, false);

//BOO, this doesn't work!
MainActivity
    -ImportArticlesAsync
        -doInBackground (AsyncTask)
             articlesDataSource = new ArticlesDataSource(context);
             articlesDataSource.importArticles(Section.currentSection, false);

每个请求 - dbHelper在此处设置:

private SQLiteDatabase database;
private DbHelper dbHelper;
private PhotosDataSource photosdatasource;
private SectionsDataSource sectionsdatasource;

public ArticlesDataSource(Context context)
{
    mContext = context;
    dbHelper = new DbHelper(mContext);
    photosdatasource = new PhotosDataSource(context);
    sectionsdatasource = new SectionsDataSource(context);
}

public void open() throws SQLException
{
    database = dbHelper.getWritableDatabase();
}

代码:

com.mysite.news

com.mysite.models

com.mysite.datasources

com.mysite.utilites

AndroidManifest.xml - http://pastebin.com/hHF86f4d


错误:

07-20 21:33:26.721:W / System.err(8260):java.lang.NullPointerException 07-20 21:33:26.721:W / System.err(8260):在android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203) 07-20 21:33:26.721:W / System.err(8260):在android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118) 07-20 21:33:26.721:W / System.err(8260):at com.mysite.datasources.ArticlesDataSource.open(ArticlesDataSource.java:88) 07-20 21:33:26.721:W / System.err(8260):at com.mysite.datasources.ArticlesDataSource.importArticles(ArticlesDataSource.java:101) 07-20 21:33:26.721:W / System.err(8260):at com.mysite.news.MainActivity $ ImportArticlesAsync.doInBackground(MainActivity.java:194) 07-20 21:33:26.721:W / System.err(8260):at com.mysite.news.MainActivity $ ImportArticlesAsync.doInBackground(MainActivity.java:1) 07-20 21:33:26.721:W / System.err(8260):在android.os.AsyncTask $ 2.call(AsyncTask.java:185) 07-20 21:33:26.721:W / System.err(8260):at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:306) 07-20 21:33:26.721:W / System.err(8260):at java.util.concurrent.FutureTask.run(FutureTask.java:138) 07-20 21:33:26.721:W / System.err(8260):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 07-20 21:33:26.721:W / System.err(8260):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:581) 07-20 21:33:26.721:W / System.err(8260):at java.lang.Thread.run(Thread.java:1019)


SectionSelectedListener.java

package com.mysite.news;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;

public class SectionSelectedListener implements OnItemSelectedListener
{
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
    {
        MainActivity mainActivity = new MainActivity();
        mainActivity.sectionSelected();     
    }

    public void onNothingSelected(AdapterView<?> arg0) {
        // Do nothing       
    }

}

3 个答案:

答案 0 :(得分:1)

您传递给Context的{​​{1}}引用为AsyncTask。您只需调用null方法即可实例化新的MainActivity,从而出现此问题。你永远不应该自己实例化一个活动!

最简单的解决方案是简单地将匿名内部类作为sectionSelected()中的侦听器:

MainActivity

答案 1 :(得分:0)

尝试将此行articlesDataSource = new ArticlesDataSource(mContext);从您doInBackground(..)的{​​{1}}方法移至AsyncTask的{​​{1}}方法。像这样:

onPreExecute()

我认为这可以解决您的问题。

答案 2 :(得分:0)

将dbHelper变量从ArticlesDataSource类移动到Activity,确保在创建AsyncTasks之前初始化它。