Reputation: 142
When I click a Button I starting a new activity, called Profilo.java. My code is:
package com.example.prenotazione_esame;
import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.*; import android.view.Menu; import android.view.MenuItem; import android.widget.Button; import android.widget.EditText; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast;
public class Profilo extends Activity { private LoginDataBase dbLogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); int id = getIntent().getExtras().getInt("id"); setContentView(R.layout.profilo); SQLiteDatabase db = dbLogin.getWritableDatabase(); }
}
this LoginDataBase.java
public class LoginDataBase extends SQLiteOpenHelper {
private static final String DB_NAME="Login_DB";
private static final int DB_VERSION=1;
public LoginDataBase(Context context){
super(context,DB_NAME,null,DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql="";
sql+= "CREATE TABLE T_LOGIN (";
sql+= " _id_LOGIN INTEGER PRIMARY KEY AUTOINCREMENT,";
sql+= " USERNAME TEXT NOT NULL,";
sql+= " PASSWORD TEXT NOT NULL";
sql+=")";
db.execSQL(sql);
String sql2="";
sql2+= "CREATE TABLE T_PROFILO (";
sql2+= " _id_PROFILO INTEGER PRIMARY KEY AUTOINCREMENT,";
sql2+= " NOME TEXT NOT NULL,";
sql2+= " COGNOME TEXT NOT NULL,";
sql2+= " ETA TEXT NOT NULL,";
sql2+= " SESSO TEXT(1) NOT NULL,";
sql2+= " CODICE_FISCALE TEXT NOT NULL,";
sql2+= " CITTA TEXT NOT NULL,";
sql2+= " INDIRIZZO TEXT NOT NULL,";
sql2+= " TELEFONO TEXT NOT NULL,";
sql2+= " _id_L INTEGER NOT NULL,";
sql2+= " FOREIGN KEY(_id_L) REFERENCES T_LOGIN(_id_LOGIN)";
sql2+= ")";
db.execSQL(sql2);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Aggiornamento delle tabelle
}
}
the problem is when I try to take my database db. My app crash. Where is the problem?
Upvotes: 0
Views: 52
Reputation: 117
You need to inicializate dbLogin.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int id = getIntent().getExtras().getInt("id");
setContentView(R.layout.profilo);
///NEW
LoginDataBase dbLogin = new LoginDataBase (this, "NAMEOFDATABASE", null, 1);
///END OF NEW
SQLiteDatabase db = dbLogin.getWritableDatabase();
}
}
You class LoginDataBase must have a constructor similar than this:
public class LoginDataBase extends SQLiteOpenHelper {
private static final String DB_NAME="Login_DB";
private static final int DB_VERSION=1;
public LoginDataBase (Context contexto, String name,
CursorFactory factory, int version) {
super(contexto, name, factory, version);
}
Or you can also change this and continue with your constructor:
///NEW
LoginDataBase dbLogin = new LoginDataBase (this);
///END OF NEW
Upvotes: 1