Ejecutar un método en una clase que no extiende una actividad en Android

I am currently developing an application on android.

I would like a class which creates the database and tables in a separated from the Activity screen.

public void createDatabase()
    {
        try
        {
            SQLiteDatabase myDB;
            myDB = this.openOrCreateDatabase("PasswordManager", Context.MODE_PRIVATE, null);

            myDB.execSQL("CREATE TABLE IF NOT EXISTS `users` (`use_userID` int(11) NOT NULL, `use_username` varchar(25) DEFAULT NULL," +
                    "`use_usePassword` char(1) DEFAULT NULL, `use_password` varchar(150) DEFAULT NULL, PRIMARY KEY (`use_userID`))");

            myDB.execSQL("CREATE TABLE IF NOT EXISTS `password` (`pas_loginID` int(11) NOT NULL, `pas_userID` int(11) DEFAULT NULL," +
                    "`pas_company` varchar(50) NOT NULL, `pas_companyURL` varchar(250) DEFAULT NULL, `pas_username` " +
                    "varchar(150) NOT NULL, `pas_password` varchar(150) NOT NULL, `pas_type` varchar(50) NOT NULL, " +
                    "PRIMARY KEY (`pas_loginID`))");
        }
        catch (SQLiteException sqlEx)
        {
            Log.d("Database Error", sqlEx.toString());
        }
        catch (Exception ex)
        {
            Log.d("General Error", ex.toString());
        }
    }

The class isn't activity i.e. it is not showing a screen, it is just going to do certain methods which will need to happen regularly so I haven't got to repeat code. How can I do this. It keeps on coming up with a Null Pointer Exception

preguntado el 08 de enero de 11 a las 23:01

1 Respuestas

Presume you have a class (which is not visible from the code snippet). If you have a class, just create an instance in the Activity class and call the method.

snippet as follows.

public class MyDBHelper extends SQLiteOpenHelper {
   public void createDatabase() { ......}
.....
}

public class MyActivity extends Activity {
    private MyDBHelper mDBHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
        ........
        mDBHelper.createDatabase();
     }
............
}

Respondido el 09 de enero de 11 a las 04:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.