Android Studio中SQLite数据库出现异常行为并影响其他项目运行
Hey there! Let's tackle the issues you're facing with your SQLite code and those pesky errors popping up in all your new Android projects. First, let's break down what's going on:
1. First, Fix the Cursor Loop in Your Code
Looking at your SQLite implementation, there's a critical bug in how you're iterating over the Cursor—this is likely causing actual runtime crashes, even if some of the log messages seem unrelated.
Your original loop:
while (c != null) { Log.i("Result - event", c.getString(eventsIndex)); Log.i("Result - year", Integer.toString(c.getInt(yearIndex))); c.moveToNext(); }
The problem here is that c never becomes null—after the last row, moveToNext() returns false, but you'll still try to access column values one more time, throwing an exception.
Replace it with this safer pattern, and don't forget to close your resources to avoid memory leaks:
// Check if there are any rows first if (c.moveToFirst()) { do { Log.i("Result - event", c.getString(eventsIndex)); Log.i("Result - year", String.valueOf(c.getInt(yearIndex))); } while (c.moveToNext()); // Loop until there are no more rows } // Always close Cursor and Database when done if (!c.isClosed()) { c.close(); } if (database.isOpen()) { database.close(); }
2. Addressing the Persistent Project-Wide Warnings
The log messages you shared are mostly warnings from Android Studio tools or system-level permissions, not issues with your SQLite logic. Since these are affecting all new projects, here's how to fix the environment:
Invalidate Android Studio Caches:
Go toFile > Invalidate Caches...and selectInvalidate and Restart. This clears corrupted cached data that often causes weird, project-wide issues.Update SDK & Studio Tools:
- Open
Tools > SDK Manager - Check the
SDK Toolstab and update Android SDK Build-Tools, Android Emulator, and any other outdated tools - Update Android Studio itself via
Help > Check for Updates
- Open
Reset Your Emulator/Device:
- If using an emulator: Create a new AVD (Android Virtual Device) with the latest system image—old emulators can accumulate bugs.
- If using a physical device: Restart it, and make sure it's running the latest available OS updates.
Disable Advanced Profiling (Temporarily):
Theperfa.jarwarning comes from Android Studio's Profiler. To turn it off:- Open your Run/Debug Configuration
- Uncheck the box labeled
Enable advanced profiling - Apply changes and re-run your app
3. What About Those System-Level Warnings?
Just to put your mind at ease:
Access denied finding property "ro.serialno"is a harmless system permission warning on Android 10+—it doesn't affect your app's functionality.- The
sun.misc.Unsafemethod warnings are from internal libraries used by Android Studio tools, not your code. You can safely ignore these unless they're causing actual crashes.
After making these changes, your SQLite app should run as expected, and the project-wide warnings should disappear.
内容的提问来源于stack exchange,提问作者Amitesh Anand

