Android error copying database to devices - android

I'm using this famous database helper and want to copy a database to a device.
I have already filled and pushed the database to emulator through adb, and it works fine on emulator, but the app has difficulty copying the db to devices.
I have also created an assets folder and put the database there at ~/Shiny/app/src/main/assets/database/shiny.db.
Here is my main activity:
Here is my app's specific variables in the DataBaseHelper helper class:
public class DataBaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "shiny.db";
private static String DB_PATH = "/data/data/com.shinyapp.shiny/databases/";
private SQLiteDatabase myDataBase;
private Context myContext;
And here is the onCreate method that calls the DataBaseHelper:
but I get this error:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Get a support ActionBar corresponding to this toolbar
ActionBar ab = getSupportActionBar();
// Enable the Up button
ab.setDisplayHomeAsUpEnabled(true);
myDB = new DataBaseHelper(this);
try {
myDB.createDataBase();
} catch (IOException e) {
throw new Error("Unable to create database");
}
try {
myDB.openDataBase();
}catch(SQLException sqle){
throw new Error("Unable to open database");
}
But I get this error:
FATAL EXCEPTION: main
java.lang.Error: Error copying database
at com.shinyapp.shiny.DataBaseHelper.createDataBase(DataBaseHelper.java:61)
at com.shinyapp.shiny.MainActivity.onCreate(MainActivity.java:52)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
at android.app.ActivityThread.access$600(ActivityThread.java:128)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4514)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
I'm wondering what could be wrong here and how can I fix it?
UPDATE: here the error log that I get when I modified error handling to log the error instead of just throwing errors:
05-27 16:13:08.196 23913-23913/com.shinyshop.shiny E/SqliteDatabaseCpp: sqlite3_open_v2("/data/data/com.shinyshop.shiny/databases/shiny.db", &handle, 1, NULL) failed
05-27 16:13:08.204 23913-23913/com.shinyshop.shiny E/SQLiteDatabase: Failed to open the database. closing it.
android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file
app at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
app at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1124)
app at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1075)
app at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1051)
app at com.shinyshop.shiny.DataBaseHelper.checkDataBase(DataBaseHelper.java:78)
app at com.shinyshop.shiny.DataBaseHelper.createDataBase(DataBaseHelper.java:45)
app at com.shinyshop.shiny.MainActivity.onCreate(MainActivity.java:49)
app at android.app.Activity.performCreate(Activity.java:4465)
app at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
app at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
app at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
app at android.app.ActivityThread.access$600(ActivityThread.java:128)
app at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
app at android.os.Handler.dispatchMessage(Handler.java:99)
app at android.os.Looper.loop(Looper.java:137)
app at android.app.ActivityThread.main(ActivityThread.java:4514)
app at java.lang.reflect.Method.invokeNative(Native Method)
app at java.lang.reflect.Method.invoke(Method.java:511)
app at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
app at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
app at dalvik.system.NativeStart.main(Native Method)
05-27 16:13:08.360 23913-23913/com.shinyshop.shiny E/Creating DB ERROR: exception
app java.io.FileNotFoundException: shiny.db
app at android.content.res.AssetManager.openAsset(Native Method)
app at android.content.res.AssetManager.open(AssetManager.java:315)
app at android.content.res.AssetManager.open(AssetManager.java:289)
app at com.shinyshop.shiny.DataBaseHelper.copyDataBase(DataBaseHelper.java:103)
app at com.shinyshop.shiny.DataBaseHelper.createDataBase(DataBaseHelper.java:57)
app at com.shinyshop.shiny.MainActivity.onCreate(MainActivity.java:49)
app at android.app.Activity.performCreate(Activity.java:4465)
app at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
app at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
app at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
app at android.app.ActivityThread.access$600(ActivityThread.java:128)
app at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
app at android.os.Handler.dispatchMessage(Handler.java:99)
app at android.os.Looper.loop(Looper.java:137)
app at android.app.ActivityThread.main(ActivityThread.java:4514)
app at java.lang.reflect.Method.invokeNative(Native Method)
app at java.lang.reflect.Method.invoke(Method.java:511)
app at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
app at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
app at dalvik.system.NativeStart.main(Native Method)

The sample code that you are using is old and awful.
That being said, your problem is that the sample code is expecting your database to be in ~/Shiny/app/src/main/assets/shiny.db, not ~/Shiny/app/src/main/assets/database/shiny.db. If you want the asset in ~/Shiny/app/src/main/assets/database/shiny.db, you would need to modify the AssetManager code to add the database/ part to the value that you pass to open().

Related

Osmand build crash on debug

I imported the latest master folder (master branch 26-12-2014) to Android-Studio,Gradle build the project successfully after little fix (asset missing,inner fragment static error) then i launched the debugger on my device (samsung galaxy 3 min,Android 4.1.2),I expect to have Osmand running on my device but the application crashes and the log indicate that it happen on getting routing.xml file.
12-26 10:47:10.069 10508-10508/net.osmand.plus E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to create application net.osmand.plus.OsmandApplication: java.lang.IllegalStateException: java.lang.IllegalArgumentException
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4200)
at android.app.ActivityThread.access$1400(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: java.lang.IllegalArgumentException
at net.osmand.router.RoutingConfiguration.getDefault(RoutingConfiguration.java:171)
at net.osmand.plus.helpers.AvoidSpecificRoads.getBuilder(AvoidSpecificRoads.java:44)
at net.osmand.plus.helpers.AvoidSpecificRoads.<init>(AvoidSpecificRoads.java:38)
at net.osmand.plus.OsmandApplication.onCreate(OsmandApplication.java:168)
Thanks.
Make sure that there is a folder named "router" inside your "OsmAnd/scr/net/osmand/" folder which contains the routing.xml file.
If not you can find the resources needed here

actionBar.setDisplayHomeAsUpEnabled(true); Crashes Android Application When Using Android Studio

I Recently Upgraded From Eclipse To Android Studio...
I Imported My App Into Android Studio And When I Try To Run My App Through ADB, My App Crashes..
I Removed actionBar.setDisplayHomeAsUpEnabled(true); From An Activity And That Activity Didn't Crash..
I Wasn't Facing This Error When I Was Using Eclipse...
Here Is The Logcat Log :
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.test/com.test.test.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.access$600(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.test.test.MainActivity.onCreate(MainActivity.java:33)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
At The 33rd Line In Main Activity I'm Setting The setDisplayHomeAsUpEnabled To True..
And The Theme Of MainActivity Is Not Set To Theme.NoTitleBar...
Edit : I Tried Importing More Apps But I Got The Same Error...All Of The Apps Worked With Eclipse..
Edit : Code :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true); // Line 33

Simple android app always crashes

Im newbie at developing Android apps but even the simpliest app always crashes when I try to add click listener I tried to google my problem but with no success. I also tried to change API to different versions at new project screen. Im able to run Hello World app but when I try to add listener to my app then Im no longer able to run it. Here is my error log.
03-02 21:13:49.153 19700-19700/com.example.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
at android.app.ActivityThread.access$600(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:4977)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.app.MainActivity.onCreate(MainActivity.java:32)
at android.app.Activity.performCreate(Activity.java:4538)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
 at android.app.ActivityThread.access$600(ActivityThread.java:139)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:4977)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
03-02 21:13:49.163 1598-1922/? E/EmbeddedLogger﹕ App crashed! Process: com.example.app
03-02 21:13:49.163 1598-1922/? E/EmbeddedLogger﹕ App crashed! Package: com.example.app v1 (1.0)
03-02 21:13:49.163 1598-1922/? E/EmbeddedLogger﹕ Application Label: My Application 7
03-02 21:13:51.566 19729-19729/? E/ActivityThread﹕ Failed to find provider info for com.google.android.gallery3d.GooglePhotoProvider
03-02 21:13:56.871 19959-19959/? E/ActivityThread﹕ Failed to find provider info for com.google.android.gallery3d.GooglePhotoProvider
And here only piece of code where I changed something (I added button in design)
public class MainActivity extends ActionBarActivity {
Button btn = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btn.setText("Hi");
}
});
}
According to your comment on the last answer, you do not import any R file.
You need to import it this way:
import com.<package_name>.<app_name>.R;
That will import the ids generated file available into your Java code and the Button id button will be visible.
Look at the line 32 of the file MainActivity.java. Sounds like you're trying to call a method on a null object.
For example, are you sure your layout activity_main.xml contains a Button with an id R.id.button?
The problem is either that R.id.button doesn't exist in your activity_main.xml layout, or that there is something null in this code:
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
Try them both out separately and figure out which one isn't working. Go from there.
By the way, the NullPointerException occurs on line 32.

Android SQLite database is locked

I have a bound service running multiple threads that read and write to an SQLite database. I create an instance of my database helper and get a connection to the database in the onCreate of my service, which should execute on the main thread. As it's a bound service there should only ever be one instance of the service in memory. However i still get
exceptions occasionally when the service tries to open the database or when one of the threads tries to execute a statement.
Service code:
#Override
public void onCreate() {
super.onCreate();
MyDatabaseHelper helper = new MyDatabaseHelper(this);
database = helper.getWritableDatabase();
}
Exceptions:
java.lang.RuntimeException: Unable to create service uk.co.example.service.MyService:
android.database.sqlite.SQLiteException: unable to open database file
at android.app.ActivityThread.handleCreateService(ActivityThread.java:1959)
at android.app.ActivityThread.access$2500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:989)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3691)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: unable to open database file
at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1960)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:887)
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:965)
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:958)
at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:585)
at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
at uk.co.example.service.MyService.onCreate(MyService.java:69)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:1949)
... 10 more
android.database.sqlite.SQLiteException: error code 5: database is locked
at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:61)
at uk.co.example.a.a.a.a(MyTable.java:147)
at uk.co.example.service.b.a.m.run(MySaveTask.java:84)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)
Any ideas what could be causing this?
Thanks
David
Sqlite now supports a new journal mode called Writing-Ahead Logging, which is suitable for concurrent access to sqlite. you may want to take a look at http://www.sqlite.org/wal.html.

Android Problem on Widget ImageButton click

I'm new in Android development, and my English is awfull.
I have an application with a homescreen widget.
When I start application, in main activity, I can select options for widget (these options are saved to a DB).
Then, I add a Widget, that it have an ImageButton on it, to the homescreen. The click on ImageButton works fine.
If I kill the application activity with some Task Manager, delete the widget, and add it again. The click on ImageButton crash.
Is this normally?
Here you have the error:
java.lang.RuntimeException: Unable to start service cat.aat.quoteswidget.Widget$UpdateService#45d1b188 with Intent { cmp=cat.aat.quoteswidget/.Widget$UpdateService }: java.lang.NullPointerException
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3282)
at android.app.ActivityThread.access$3600(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2211)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:144)
at android.app.ActivityThread.main(ActivityThread.java:4937)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at cat.aat.quoteswidget.Widget$UpdateService.buildUpdate(Widget.java:130)
at cat.aat.quoteswidget.Widget$UpdateService.onStart(Widget.java:90)
at android.app.Service.onStartCommand(Service.java:420)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3267)
... 10 more
Thanks!
You have a NullPointerException on line 130 of your Widget.java file, in the buildUpdate() method of your UpdateService.

Categories

Resources