views:

45

answers:

2

I have an application that must create a database and if that fails, then no sense moving forward. I've built an AlertDialog and show() it but it never displays. The logic falls through and then barfs because of the missing database.

What is the right/best way to throw up a message and halt the activity? The code below executes fine (meaning the show() occurs while debugging and it falls to the next line) but the UI never displays this alert. BTW - I realize the throw might not be the most graceful but I'm not even getting that far so... B^)

 try {

myDBHelp.createDataBase(); } catch (IOException ioe) {

new AlertDialog.Builder(this).setCancelable(false) .setMessage(ioe.getMessage()) .setTitle("Database Create Failed") .setPositiveButton("Quit", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { throw new Error("Unable to create database - please try uninstall/reinstall");

         }
         })
.show();
A: 

hi,

I do not know what flow u use.But have an suggestion U can do it like this.

U can start database operation like this..

Intent i = new Intent(this,Databaseoperation.class); startactivity(i); ............................... this will make controls to move to databaseoperation class it performs various operations like open.close,insert delete..etc.

U can extend databasehelper in built class

and now when any problem in opening database or any stuff , finish() intent and go back to main activity...

U can make it like this..

thanks rakesh

Rakesh Gondaliya
Rakesh- the issue isn't with catching the error. It's letting the user know something went wrong and preventing useless code from occurring and blowing up. I wonder if using Intent would just make this worse - kicking of an asychronous database thread when in fact I want to block/wait for that to finish - no database, no application.
teachableMe
A: 

I ususally do something like this:

void myFunction() {

    try {
        somecode..
    } catch (IOException e){
        e.printStackTrace();
        doToast("Unknown Error");  //Display Toast to user
        return;           //Leave myFunction
    }

    somecode...  //If no error continue here

    return;
}

protected void doToast(final String str) {
    this.runOnUiThread(new Runnable() {
        public void run() {
            Toast.makeText(myClass.this, str, Toast.LENGTH_SHORT).show();
        }
    });

}
droidgren
Using a toast is certainly valid but I wonder if I would have had the same problem. The problem is the UI was not showing my Dialog before it puked. Turns out, the overridden onResume() was executing BEFORE the dialog showed, and was failing there, as code was expecting a database. I had to put in a boolean and test/execute only that dependent code if no prior errors. Once onResume() finished, the dialog shows. I'm pretty sure Toast will work once I've protected the onResume() code. Thanks.
teachableMe