android - Application on Device Close on open -
every time open app on phone check it, close on second opens , says: ""name of app" stopped"
its autoclose without go main layout
p.s tnx guys!
debug says:
target device: 54d1969c installing apk: c:\users\erel\androidstudioprojects\accountsaver\app\build\outputs\apk\app-debug.apk uploading file to: /data/local/tmp/com.erelbiran.accountsaver com.android.ddmlib.adbcommandrejectedexception: device unauthorized. adb server's $adb_vendor_keys not set try 'adb kill-server' if seems wrong. otherwise check confirmation dialog on device.
mainactivity
package com.erelbiran.accountsaver; import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.toast; public class mainactivity extends activity { db mydb; button btnadd; edittext user = (edittext)findviewbyid(r.id.enteruser), pass = (edittext)findviewbyid(r.id.enterpass), acc = (edittext)findviewbyid(r.id.enteracc); @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); opendb(); btnadd.setonclicklistener( new view.onclicklistener() { public void onclick(view view) { mydb.insertrow(user.gettext().tostring(), pass.gettext().tostring(), acc.gettext().tostring()); toast.maketext(mainactivity.this, "account added!", toast.length_short).show(); }} ); } private void opendb(){ mydb = new db(this); mydb.open(); } private void closedb(){ mydb.close(); } }
menifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.erelbiran.accountsaver"> <application android:debuggable="true" android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/apptheme"> <activity android:name="mainactivity" android:label="@string/app_name" android:theme="@style/apptheme.noactionbar"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
database code:
// ------------------------------------ dbadapter.java --------------------------------------------- package com.erelbiran.accountsaver; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log; public class db { ///////////////////////////////////////////////////////////////////// // constants & data ///////////////////////////////////////////////////////////////////// // logging: private static final string tag = "dbadapter"; // db fields public static final string key_rowid = "_id"; public static final int key_accid = 0; public static final string key_user = "username"; public static final string key_pass = "password"; public static final string key_acc = "accounts"; // // setup fields public static final int col_user = 1; public static final int col_pass = 2; public static final int col_acc = 3; public static final string[] all_keys = new string[] {key_rowid, key_user, key_pass, key_acc}; // db info: it's name, , table using (just one). public static final string database_name = "mydb"; public static final string database_table = "maintable"; public static final int database_version = 1; private static final string database_create_sql = "create table " + database_table + " (" + key_accid + " integer primary key autoincrement, " + key_user + " string not null, " + key_pass + " string not null, " + key_acc + " string not null" // rest of creation: + ");"; // context of application uses us. private final context context; private databasehelper mydbhelper; private sqlitedatabase db; ///////////////////////////////////////////////////////////////////// // public methods: ///////////////////////////////////////////////////////////////////// public db(context ctx) { this.context = ctx; mydbhelper = new databasehelper(context); } // open database connection. public db open() { db = mydbhelper.getwritabledatabase(); return this; } // close database connection. public void close() { mydbhelper.close(); } // add new set of values database. public long insertrow(string username , string password , string account) { contentvalues initialvalues = new contentvalues(); initialvalues.put(key_user, username); initialvalues.put(key_pass, password); initialvalues.put(key_acc, account); return db.insert(database_table, null, initialvalues); } // delete row database, rowid (primary key) public boolean deleteacc(long accid) { string = key_accid + "=" + accid; return db.delete(database_table, where, null) != 0; } public void deleteall() { cursor c = getallrows(); long rowid = c.getcolumnindexorthrow(key_rowid); if (c.movetofirst()) { { deleteacc(c.getlong((int) rowid)); } while (c.movetonext()); } c.close(); } // return data in database. public cursor getallrows() { string = null; cursor c = db.query(true, database_table, all_keys, where, null, null, null, null, null); if (c != null) { c.movetofirst(); } return c; } // specific row (by rowid) public cursor getrow(long rowid) { string = key_rowid + "=" + rowid; cursor c = db.query(true, database_table, all_keys, where, null, null, null, null, null); if (c != null) { c.movetofirst(); } return c; } ///////////////////////////////////////////////////////////////////// // private helper classes: ///////////////////////////////////////////////////////////////////// /** * private class handles database creation , upgrading. * used handle low-level database access. */ private static class databasehelper extends sqliteopenhelper { databasehelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase _db) { _db.execsql(database_create_sql); } @override public void onupgrade(sqlitedatabase _db, int oldversion, int newversion) { log.w(tag, "upgrading application's database version " + oldversion + " " + newversion + ", destroy old data!"); // destroy old database: _db.execsql("drop table if exists " + database_table); // recreate new database: oncreate(_db); } } }
oncreate(bundle)
initialize activity.
most importantly, here call setcontentview(int)
layout resource defining ui, , using findviewbyid(int)
retrieve widgets in ui need interact programmatically. try ui in oncreate(bundle )
try
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); user = (edittext)findviewbyid(r.id.enteruser); pass = (edittext)findviewbyid(r.id.enterpass); acc = (edittext)findviewbyid(r.id.enteracc);
Comments
Post a Comment