Hi是从Playstore执行更新时扩展Application Called的类。 我的问题是我在我的Application Class的onCreate函数上初始化了SQLiteDatabase。当我这样做并升级我的应用程序崩溃时出现以下错误
java.lang.NullPointerException:尝试调用虚方法' boolean android.database.sqlite.SQLiteDatabase.isOpen()
这意味着SQLiteDatabase未在UPDATE初始化。
任何想法???
下面我们更多
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.database.sqlite.SQLiteDatabase.isOpen()' on a null object reference
at com.cellulant.consumerapp.db.DBAdapter.getSqliteDatabase(DBAdapter.java:518)
at com.cellulant.consumerapp.fragments.PaymentsFragment.onCreateView(PaymentsFragment.java:113)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1632)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:637)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1235)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1083)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1609)
at android.view.View.measure(View.java:17923)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:735)
at android.view.View.measure(View.java:17923)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
at android.view.View.measure(View.java:17923)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1079)
at android.view.View.measure(View.java:17923)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:17923)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:760)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:17923)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
at android.view.View.measure(View.java:17923)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:760)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:17923)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2733)
at android.view.View.measure(View.java:17923)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2461)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1463)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1687)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1341)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6700)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:800)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:572)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:786)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
DB Apdater
public class DBAdapter扩展了SQLHelper {
private CustomerTable customerTable;
private ProfilesTable profilesTable;
/**
* @param context Application Context
* @param dbName Database name
* @param dbVersion database version
*/
public DBAdapter(Context context, final String dbName, final int dbVersion) {
super(context, dbName, null, dbVersion);
customerTable = new CustomerTable();
billsTable = new BillsTable();
}
/***
* Create individual table needed in database
*
* @param sqlDB Database
*/
private void createInitialTables(SQLiteDatabase sqlDB) {
try {
customerTable.createTable(sqlDB);
billsTable.createTable(sqlDB);
} catch (Exception e) {
LogUtils.showException(e);
}
}
@Override
public void onCreate(SQLiteDatabase sqlDB) {
createInitialTables(sqlDB);
super.onCreate(sqlDB);
}
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
}
/**
* This method is responsible for closing the database connection.
*
* @param db Database
*/
public void close(SQLiteDatabase db) {
super.close();
//Check if there is any open connection before closing.
if (db.isOpen()) {
db.close();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
super.onUpgrade(db, oldVersion, newVersion);
if (oldVersion < 21) {
boolean tableSMSTemplateColumnIsDefault = StringUtils.checkIfColumnExists(db, SMSTemplateTable.class.getSimpleName(), SMSTemplateMap.PARSE_TYPE);
if (!tableSMSTemplateColumnIsDefault) {
db.execSQL(ALTER_SMS_TEMPLATES_PARSE_TYPE);
}
boolean tableMerchantColumnIsDefault = StringUtils.checkIfColumnExists(db, MerchantPayersTable.class.getSimpleName(), MerchantPayersMap.IS_DEFAULT);
if (!tableMerchantColumnIsDefault) {
db.execSQL(ALTER_MERCHANT_PAYER_IS_DEFAULT);
}
}
}
@Override
public SQLiteDatabase getSqliteDatabase() {
if (!super.getSQLiteDatabase().isOpen()) {
open();
}
return super.getSQLiteDatabase();
}
/*
* Getter Methods
*/
public CustomerTable getCustomerTable() {
return customerTable;
}
public ProfilesTable getProfilesTable() {
return profilesTable;
}
}
答案 0 :(得分:2)
Android应用程序升级是否会调用扩展应用程序的类?
不直接。应用程序升级将终止您的进程。创建进程的新实例时,将创建Application
子类的新实例。完全当创建流程的新实例时会有所不同,具体取决于应用设置,用户正在做什么等等。
请记住,在创建流程时,您的任何活动都可能是用户的起点。用户频繁将从您的启动器活动开始,但情况并非总是如此。
例如,假设您的启动器活动是活动A,它会打开活动B,从而打开活动C.此时,用户获得可用更新的Notification
。因此,用户点击Notification
,转到Play商店(或任何地方),并请求更新应用。您的流程将在此过程中终止。升级完成后,用户将返回到您的应用程序(主屏幕启动器图标,概览/最近任务列表等)。此时,您将获得一个新进程...并且将创建活动C并将其带到前台。如果您的代码假定用户将始终通过活动A,则可能会遇到问题。
这不是应用升级所特有的。用户可能已经离开A-> B-&gt; C,按下HOME,进入另一个应用程序10分钟,然后返回到您的应用程序。当您的UI处于后台时,您的进程可能已被终止,只是为了释放系统RAM。再一次,当用户返回您的应用程序时,您将获得一个新进程,用户将看到活动C,而不是活动A.