gpt4 book ai didi

java - 从 AsyncTask 触发时 SQLiteOpenHelper.getWritableDatabase 上出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 15:16:07 24 4
gpt4 key购买 nike

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

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

代码是这样的:

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

它是从 ArticlesDataSource 中的 importArticles() 函数调用的。

从我的 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.NullPointerException07-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)

<小时/>

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
}

}

最佳答案

传递给 AsyncTaskContext 引用为 null。当您实例化一个新的 MainActivity 只是为了调用 sectionSelected() 方法时,就会出现问题。你永远不应该自己实例化一个 Activity!

最简单的解决方案是简单地创建一个匿名内部类作为MainActivity中的监听器:

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

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

});

关于java - 从 AsyncTask 触发时 SQLiteOpenHelper.getWritableDatabase 上出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11586458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com