我正在尝试对我的数据库运行查询以从我的数据库中获取一些数据(从页面到页面的更改)。我在Double balance = db.getSum();
得到一个NullPointerException。下面是我的fragmentstatepageradapter的完整代码
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {
int listSize;
String[] tabNames;
String tabName;
protected Context mContext;
/** Constructor of the class */
public MyFragmentPagerAdapter(FragmentManager fm, Context c) {
super(fm);
mContext = c;
}
/** This method will be invoked when a page is requested to create */
@Override
public Fragment getItem(int arg0) {
MainFragment mainFragment = new MainFragment();
final DatabaseHelper db = new DatabaseHelper(mContext);
Double balance = db.getSum(); //NPE here*******************
tabName = "[" + getPageTitle(arg0) + "]";
Bundle data = new Bundle();
data.putInt("current_page", arg0 + 1);
data.putString("tabName", tabName);
mainFragment.setArguments(data);
return mainFragment;
}
/** Returns the number of pages */
@Override
public int getCount() {
return listSize;
}
@Override
public CharSequence getPageTitle(int position) {
return tabNames[position];
}
public void setCount(int count){
listSize = count;
}
public void setNames(String[] names) {
tabNames = names;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
这是我的数据库getSum()
// GET SUM
public Double getSum(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT SUM(credit), SUM(debit) FROM " + TABLE_DEFAULT_ACCOUNT, null);
if(c.moveToFirst()) {
sum = (c.getDouble(0) - c.getDouble(1));
}
return sum;
}
这是我得到的错误
07-09 15:26:52.861 14874-14874/com.rcd.simpleregister D/AndroidRuntime﹕ Shutting down VM
07-09 15:26:52.865 14874-14874/com.rcd.simpleregister E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.rcd.simpleregister, PID: 14874
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
at com.rcd.simpleregister.DatabaseHelper.getSum(DatabaseHelper.java:90)
at com.rcd.simpleregister.MyFragmentPagerAdapter.getItem(MyFragmentPagerAdapter.java:31)
at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:105)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
at android.support.v4.view.ViewPager.populate(ViewPager.java:982)
at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
at android.view.View.measure(View.java:16537)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:16537)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5135)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16537)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5135)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
at android.view.View.measure(View.java:16537)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5135)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16537)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1947)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1137)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1326)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1024)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5796)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
任何人都明白为什么我会这样做?
谢谢!
答案 0 :(得分:8)
您传递给Context
超级构造函数的SQLiteOpenHelper
为空。
您发布的代码并未准确显示mContext
的值来自哪里。在任何情况下,在片段中你应该只有一个no-arg构造函数,当片段附加到一个活动时,你可以使用getActivity()
作为Context
。