Witam
Mój problem z bazą danych jest taki: gdy próbuje zapisać do bazy dane z edit texta który znajduje się w dialogu wyskakuje mi komunikat z crashem. mój kod:
Main:
public class MainActivity extends AppCompatActivity {
EditText et1;
final Context context=this;
DbAdapter dbAdapter;
@Override
public void onStart(){
super.onStart();
createNotification();
Toast.makeText(this,"Welcome!!!",Toast.LENGTH_SHORT).show();
}
public void author_intent(){
Intent intent=new Intent(this, Intecja_O_Autorze.class);
startActivity(intent);
}
/* public void insertAddress(){
et1=(EditText) (findViewById(R.id.ET));
String address=et1.getText().toString();
dbAdapter.open();
Long inserted=dbAdapter.insertAddress(address);
if(inserted>=0){
Toast.makeText(context, "added!!!", Toast.LENGTH_SHORT).show();
et1.setText("");
} else{
Toast.makeText(context, "not added :(", Toast.LENGTH_SHORT).show();
}
} */
public void createNotification(){
NotificationCompat.Builder mBuilder=(NotificationCompat.Builder) new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.notif);
mBuilder.setContentTitle("EZopener:");
mBuilder.setContentText("Click here to enter app");
NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,mBuilder.build());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
dbAdapter=new DbAdapter(getApplicationContext());
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//showDialog(view);
LayoutInflater li = LayoutInflater.from(context);
View dialoglayoutView = li.inflate(R.layout.dialoglayout, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setView(dialoglayoutView);
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setPositiveButton("Add",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//insertAddress();
et1=(EditText) (findViewById(R.id.ET));
String address=et1.getText().toString();
dbAdapter.open();
Long inserted=dbAdapter.insertAddress(address);
if(inserted>=0){
Toast.makeText(context, "added!!!", Toast.LENGTH_SHORT).show();
et1.setText("");
} else{
Toast.makeText(context, "not added :(", Toast.LENGTH_SHORT).show();
}
dbAdapter.close();
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
author_intent();
return super.onOptionsItemSelected(item);
}
}
Pomocnik:
public class EzOpenerPomocnikBazyDanych extends SQLiteOpenHelper {
private static final String DB_NAME="zapisaneStrony";
private static final int DB_VERSION=1;
public EzOpenerPomocnikBazyDanych(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
//SQLiteDatabase.execSQL(String sql);
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE STRONY(_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "ADRES TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
tutaj wzorowałem się na poradniku z jakiegoś zagranicznego bloga:
public class DbAdapter {
static SQLiteDatabase database;
EzOpenerPomocnikBazyDanych EzOHelper;
public DbAdapter(Context context){
EzOHelper=new EzOpenerPomocnikBazyDanych(context);
}
public void open(){
database=EzOHelper.getWritableDatabase();
}
public void close(){
database.close();
}
public static Long insertAddress(String address){
ContentValues webAddressValues=new ContentValues();
webAddressValues.put("ADRES", address );
database.insert("STRONY", null, webAddressValues);
return null;
}
public static void deleteAddress(SQLiteDatabase db, String address){
db.delete("STRONY",
"ADRES = ?",
new String[] {address});
}