觸發我在這裏得到一個java.lang.NullPointerException
錯誤時:NullPointerException異常上SQLiteOpenHelper.getWritableDatabase從的AsyncTask
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);
...
但是,當我試圖從我的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
- SplashScreenActivity.java - http://pastebin.com/ZaunrrzY
- MainActivity.java - http://pastebin.com/k08sDLaJ
com.mysite.models
- Article.java - http://pastebin.com/SBEqrCuw
- Photo.java - http://pastebin.com/75DVzK6Z
- Section.java - http://pastebin.com/0YYkk5JQ
com.mysite.datasources
- ArticlesDataSource.java - http://pastebin.com/bFM4G0xT
- SectionsDataSource.java - http://pastebin.com/pKZQHt9U
- PhotosDataSource.java - http://pastebin.com/sZTZd4ZP
com.mysite。UTILITES
- DbHelper.java - http://pastebin.com/PMCFrVyG
的AndroidManifest.xml - http://pastebin.com/hHF86f4d
ERROR:
07-20 21:33:26.721:W /系統.err(8260):java.lang.NullPointerException 07-20 21:33:26.721:W/System.err (8260):at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203) 07-20 21:33:26.721:W/System.err(8260):at 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(AsyncT ask.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):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):位於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
}
}
它好像dbHelper變量爲空。你能告訴我們價值分配的地方嗎? – mfrankli 2012-07-20 20:25:32
假設DbHelper是一個覆蓋SQLiteDatabaseHelper的類是否安全? – mfrankli 2012-07-20 20:35:18
mfrankli - 「公共類DbHelper擴展SQLiteOpenHelper」 – Dave 2012-07-20 20:47:10