0

ここで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

my から呼び出された場合、これは正常に機能しますSplashScreenActivity

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

ImportArticlesAsyncしかし、 (内MainActivity)から呼び出そうとすると、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): com.mysite.datasources.ArticlesDataSource.open (ArticlesDataSource.java:88) 07-20 21:33:26.721: W/System.err (8260): com.mysite.datasources で.ArticlesDataSource.importArticles (ArticlesDataSource.java:101) 07-20 21:33:26.721: W/System.err (8260): com.mysite.news.MainActivity$ImportArticlesAsync.doInBackground (MainActivity.java:194) で 07- 20 21:33:26.721: W/System.err (8260): 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): Java で。 util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 07-20 21:33:26.721: W/System.err(8260): java.util.concurrent.FutureTask.run(FutureTask.java:138) ) 07-20 21:33:26.721: W/System.err(8260): java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 07-20 21:33:26.721: W/System.err (8260): java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 07-20 21:33:26.721: W/System.err(8260): java.lang.Thread.run( Thread.java:1019)721: W/System.err(8260): java.util.concurrent.FutureTask.run(FutureTask.java:138) 07-20 21:33:26.721: W/System.err(8260): java.util で.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 07-20 21:33:26.721: W/System.err(8260): java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) で07-20 21:33:26.721: W/System.err(8260): java.lang.Thread.run (Thread.java:1019) で721: W/System.err(8260): java.util.concurrent.FutureTask.run(FutureTask.java:138) 07-20 21:33:26.721: W/System.err(8260): java.util で.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 07-20 21:33:26.721: W/System.err(8260): java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) で07-20 21:33:26.721: W/System.err(8260): 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       
    }

}
4

3 に答える 3

1

Context渡す参照AsyncTasknull. MainActivityメソッドを呼び出すためだけにnew をインスタンス化すると、問題が発生しますsectionSelected()。自分でアクティビティをインスタンス化しないでください。

最も簡単な解決策は、匿名の内部クラスを のリスナーとして単純に作成することMainActivityです。

sectionsSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
       sectionSelected();     
    }

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

});
于 2012-07-21T14:54:35.950 に答える
0

この行を のメソッドarticlesDataSource = new ArticlesDataSource(mContext);から のメソッドdoInBackground(..)に移動してみてください。このようなもの:AsyncTaskonPreExecute()AsyncTask

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 void onPreExecute()
{
   articlesDataSource = new ArticlesDataSource(mContext);
}


@Override
protected String doInBackground(String... params)
{        
    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);
}
}

これで問題が解決すると思います。

于 2012-07-20T20:47:01.140 に答える
0

move dbHelper variable from your ArticlesDataSource class to your Activity, ensure it gets initialized before you create the AsyncTasks.

于 2012-07-20T20:59:48.880 に答える