I'm trying to insert values to the database on SQLite by SQLiteOpenHelper bur the app closes.
I'm working with SQLiteOpenHelper on Android studio, and using an activity to insert values in the database, when I first enter to the activity I can insert all the values I want, but when I go out, re-enter the activity and press the button "Registar" that insert the values on the table, the app closes without showing the Toast message.
public void Registrar (View view){
Toast.makeText(this,"Entro al registrar",Toast.LENGTH_SHORT).show();
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this, "administracion", null, 1);
SQLiteDatabase bd = admin.getWritableDatabase(); <- in this line close
'''''
'''''
}
THE LOG MODULE
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.appmediocurso, PID: 2740
java.lang.IllegalStateException: Could not execute method for android:onClick
at $DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: android.database.sqlite.SQLiteException: Can't downgrade database from version 5 to 1
at android.database.sqlite.SQLiteOpenHelper.onDowngrade(SQLiteOpenHelper.java:360)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:254)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.example.appmediocurso.NuevoDeudor.Registrar(NuevoDeudor.java:57)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
E/Watchdog: !#Sync 4269 [26_abr_16_41_35.985]
E/wifi: failed to get channel list : -95
I only expect to insert the values even if it's the second ot third time I enter to the actitity.
The cause is that before the failed run the database had had it's version number set at 5 but you are now trying to use version 1 without overriding the SQliteOpenHelper's onDowngrade method; which without being overridden results in the encountered exception, as is intended, as reducing the version number is generally not done.
That is AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this, "administracion", null, 5); was used but now AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this, "administracion", null, 1); has been used (4th parameter was 5 but is now 1) .
You could fix this by again using 5 as the version number.
Another fix could be to uninstall the App or delete the App's data and then rerun the App without changing the version. Noting that this would result in all data being lost.
Yet another fix could be to override the onUpgrade method (perhaps doing nothing), the version should then be set to 1, the data would be kept.
There are other ways of fixing this issue but they would be a little more complicated and offer no advantage over the mentioned fixes.
Related
When I run my app on the emulator with version 27 & 28 APIs, everything works for me. However, as soon as I try to go to a lower level API, I get error messages regarding the setContentView() method in my custom Activity.java class.
Here is the stack trace:
2018-12-06 15:37:59.166 9426-9426/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mplexvr.ribbonracker, PID: 9426
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mplexvr.ribbonracker/com.mplexvr.ribbonracker.RibbonRackerActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f0b0000
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f0b0000
at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:190)
at android.content.res.Resources.getDrawable(Resources.java:766)
at android.content.Context.getDrawable(Context.java:525)
at com.android.internal.widget.ToolbarWidgetWrapper.setIcon(ToolbarWidgetWrapper.java:322)
at com.android.internal.widget.ActionBarOverlayLayout.setIcon(ActionBarOverlayLayout.java:737)
at com.android.internal.policy.PhoneWindow.setDefaultIcon(PhoneWindow.java:1737)
at android.app.Activity.initWindowDecorActionBar(Activity.java:2400)
at android.app.Activity.setContentView(Activity.java:2415)
at com.mplexvr.ribbonracker.RibbonRackerActivity.onCreate(RibbonRackerActivity.java:46)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
I tried to run this app on both a phone and emulator with a version 24 api, and nothing works. My activity_main.xml that the onCreate() method tries to call just contains two fragments that are modified in the onCreate() method. Here is a snippet of code from that:
/* This code runs perfectly fine on the version 28 and 27 api on the emulator*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
images = new ArrayList<>();
imageNames = new ArrayList<>();
setContentView(R.layout.activity_main);
Log.v(TAG, "I got in onCreate");
//Create the fragment for the ranking and add it to the current view
// Get references to the TitleFragment and to the QuotesFragment containers
mRibbonFrameLayout = findViewById(R.id.ribbon_fragment_container);
mChecklistFrameLayout = findViewById(R.id.checkbox_fragment_container);
...
}
I tried looking this problem up, but nothing I found has solved this problem - at least publicly.
UPDATE Solved within 5 minutes when I figured out that a folder had a version of 26, which would limit on using that version.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I'm developing an Android application with Android Studio and since today I can no longer run the app because when Android Studio installs the apk on my device, the package installer crashes with the following message "Unfortunately, package installer has stopped". The strange thing is that when I run the application I can see for a second the main activity and its components but then everything crashes.
This is the exception:
10-15 22:16:48.185 1793-1793/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.packageinstaller, PID: 1793
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.packageinstaller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity}: java.lang.NullPointerException: Attempt to get length of null array
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.computePermissionGrantState(GrantPermissionsActivity.java:312)
at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.updateDefaultResults(GrantPermissionsActivity.java:362)
at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.onCreate(GrantPermissionsActivity.java:105)
at android.app.Activity.performCreate(Activity.java:6323)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Thank you very much in advance
You have an issue with "granted permissions on runtime", basically you must grant permissions to use specific functionalities.
Have a look here: https://developer.android.com/training/permis
Have a look at you manifest and check which of the "user-permissions" are required to check in runtime, if you have not user-permissions added, then now is the time https://developer.android.com/guide/topics/manifest/uses-permission-element.html.
Then use the code provided in the first link after to check runtime permissions.
Remember: ask permissions on runtime is required for Android 6.0 (API level 23) and above. On lower SDK (<=22), is enough adding the "user-permissions" into the manifest.
I have got the errors like this when run the above code. Please help me..
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kalya.farmers1, PID: 32178
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kalya.farmers1/com.example.kalya.farmers1.Harvestitems}: android.database.sqlite.SQLiteException: Can't downgrade database from version 2 to 1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.access$800(ActivityThread.java:183)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5651)
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)
Caused by: android.database.sqlite.SQLiteException: Can't downgrade database from version 2 to 1
at android.database.sqlite.SQLiteOpenHelper.onDowngrade(SQLiteOpenHelper.java:360)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:254)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.example.kalya.farmers1.DataBaseHandler.getAllContacts(DataBaseHandler.java:97)
at com.example.kalya.farmers1.Harvestitems.onCreate(Harvestitems.java:43)
at android.app.Activity.performCreate(Activity.java:6119)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2491)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.access$800(ActivityThread.java:183)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5651)
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)
I/Process: Sending signal. PID: 32178 SIG: 9
Application terminated.
android.database.sqlite.SQLiteException: Can't downgrade database from version 2 to 1 seems that the error is that you are trying to change the database version from 2 to 1. Try to reset the database version to 2 or maybe if you had made some modifications try to set the version to 3.
Maybe will be helpful if you can post some code too.
I am using Realm for android and the error log does not show any error trace inside my project.
My logcat output looks like this:
Process: com.toyanathpatro.gurkha, PID: 7600
io.realm.exceptions.RealmError: Unrecoverable error. Wrong transactional state (no active transaction, wrong type of transaction, or transaction already in progress) in io_realm_internal_SharedGroup.cpp line 157
at io.realm.internal.SharedGroup.nativeAdvanceRead(Native Method)
at io.realm.internal.SharedGroup.advanceRead(SharedGroup.java:83)
at io.realm.internal.ImplicitTransaction.advanceRead(ImplicitTransaction.java:35)
at io.realm.internal.SharedGroupManager.advanceRead(SharedGroupManager.java:76)
at io.realm.HandlerController.realmChanged(HandlerController.java:384)
at io.realm.HandlerController.handleMessage(HandlerController.java:116)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I use a realm database in my android project.
I've added a new column to my realm db which I generate out of a CSV and did the same in my entity file.
However, I now get a RealmMigrationNeededException if I start my app although I have uninstalled it before. So it seems like if my generated database and the entity file don't match.
I would like to know if it's possible to find out what exactly causes the exception. The stacktrace I get in Android Studio doesn't contain any clues which might help me to resolve the issue
FATAL EXCEPTION: main
Process: de.myCompany.myApp, PID: 5270
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x4000010 (has extras) } in de.myCompany.myApp.helper.WifiReceiver#2d683f50
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:872)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
Caused by: io.realm.exceptions.RealmMigrationNeededException: RealmMigration must be provided
at io.realm.BaseRealm.migrateRealm(BaseRealm.java:589)
at io.realm.Realm.migrateRealm(Realm.java:1224)
at io.realm.Realm.migrateRealm(Realm.java:1213)
at io.realm.Realm.createInstance(Realm.java:237)
at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:114)
at io.realm.Realm.getInstance(Realm.java:197)
at de.myCompany.myApp.ImageDownloadController.fillDownloadStack(ImageDownloadController.java:94)
at de.myCompany.myApp.ImageDownloadController.<init>(ImageDownloadController.java:37)
at de.myCompany.myApp.ImageDownloadController.getInstance(ImageDownloadController.java:42)
at de.myCompany.myApp.helper.WifiReceiver.onReceive(WifiReceiver.java:30)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:862)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
Is there any proper way to debug a RealmMigrationNeededException except for trial and error?
If a RealmMigrationExceptionNeeded error happens, Realm automatically tries to trigger any configured migration to fix it. If no such RealmMigration exists, you instead get another RealmMigrationNeededException that overrides the underlying cause.
The fix is to just provide a dummy RealmMigration to see the real error.
I can see why it can be a bit confusing, and we should probably consider throwing a better exception message that also include the underlying cause.