以下源代码简化了我的源代码。
public class exfragment extends Fragment implements FragmentLifeCycle{
Context mContext;
DB db;
public exfragment(){
}
public exfragment(Context context){
mContext = context;
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState)
db = new DB(mContext, "name.db", null, 1);
}
public void selectName(){
db = DB.getReadableDatabase();
}
}
此源代码产生错误因为没有没有参数的构造函数 所以,添加一个没有参数的构造函数。
执行包含我的源代码的应用程序后,单击主页按钮以在后台执行。
然后我执行了一些应用程序,直到它们导致内存不足。当我使用源代码重新启动应用程序时,应用程序会生成FATAL EXCEPTION
。
db = DB.getReadableDatabase() method.
我认为这是由上下文问题引起的。因为我插入没有参数的构造函数,所以上下文变为Null。
请参阅以下logcat消息
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: Caused by: java.lang.NullPointerException
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: at sslab.com.sms_service.SmsReceiveListFragment.selectSmsList(SmsReceiveListFragment.java:494)
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: at sslab.com.sms_service.SmsReceiveListFragment.callSmsList(SmsReceiveListFragment.java:189)
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: at sslab.com.sms_service.SmsReceiveListFragment.onCreateView(SmsReceiveListFragment.java:103)
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2042)
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:165)
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:543)
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: at android.app.Activity.performStart(Activity.java:5018)
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4745)
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
04-15 18:47:23.969 24132-24132/sslab.com.sms_service E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
答案 0 :(得分:2)
错误是您已将Context
传递给SQLiteOpenHelper
构造函数。
要根据您的情况修复此问题,请在片段中getActivity()
使用mContext
。