java - Widget Char-Sequence Error in Android Studio -
iam dumb newbie android programmer, here have problem in case want run android programs make on android studio ide. when run program, error field said "process: com.example.ever_ncn.cashflow, pid: 6317 java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.textview.settext(java.lang.charsequence)' on null object reference"
i still dont understand explanation on question.
i confuse cuz of error, have googling still unsolved. please master, answer question simple , easy understanding explanation. thank you.
nb. main activity show activity contain spinner spinner value taken database. source code main_activity.java :
public class mainactivity extends actionbaractivity { private static button btninewtrans; private static button btniviewcash; private static button btniaddcateg; databasehelper dbhelper = new databasehelper(this); spinner selectcategory; arrayadapter<string> adaptercategory; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); onbuttonclickbuttonlistener(); select_spinner_category(); } public void select_spinner_category (){ arraylist<string> allcategorylisting= dbhelper.getallcategory(); selectcategory = (spinner) findviewbyid(r.id.spncategselect); adaptercategory = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, r.id.spncategselect, allcategorylisting); adaptercategory.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); selectcategory.setadapter(adaptercategory); selectcategory.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { toast.maketext(getbasecontext(), parent.getitematposition(position) + " selected", toast.length_long).show(); string currencyvalue = string.valueof(parent.getselecteditem()); } @override public void onnothingselected(adapterview<?> parent) { } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } public void onbuttonclickbuttonlistener(){ btninewtrans = (button)findviewbyid(r.id.btnnewtrans); btninewtrans.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { intent intentnewtrans = new intent ("com.example.ever_ncn.cashflow.newtransaction"); startactivity(intentnewtrans); } } ); btniviewcash = (button)findviewbyid(r.id.btnviewcashflow); btniviewcash.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { intent intentviewcash = new intent ("com.example.ever_ncn.cashflow.viewcashflow"); startactivity(intentviewcash); } } ); btniaddcateg = (button)findviewbyid(r.id.btnaddcateg); btniaddcateg.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { intent intentaddcateg = new intent ("com.example.ever_ncn.cashflow.addcategory"); startactivity(intentaddcateg); } } ); } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); }
}
and 1 database_helper.java :
public class databasehelper extends sqliteopenhelper { public static final string myvillagesoftware = "myvillagesoftware"; public static final string database_name = "cashflow.db"; public static final string table_categ_name = "category_table"; public static final string col1 = "categid"; public static final string col2 = "categname"; public static final string col3 = "note"; public static final string col4 = "currency"; public databasehelper(context context) { super(context, database_name, null, 2); } @override public void oncreate(sqlitedatabase db) { db.execsql("create table " + table_categ_name + " (categid integer primary key autoincrement, " + "categname text," + " note text," + " currency text)"); } public boolean insertcategdata(string categname, string note, string currency){ sqlitedatabase db = this.getwritabledatabase(); contentvalues contentvalues = new contentvalues(); contentvalues.put(col2, categname); contentvalues.put(col3, note); contentvalues.put(col4, currency); long result = db.insert(table_categ_name, null, contentvalues); if (result == -1) return true; else return false; } public arraylist<string>getallcategory(){ arraylist<string> allcategorylist = new arraylist<string>(); sqlitedatabase db = this.getreadabledatabase(); string selectcateg="select * " +table_categ_name; cursor cursor = db.rawquery(selectcateg, null); if(cursor.getcount()>0){ while (cursor.movetonext()){ string categname=cursor.getstring(cursor.getcolumnindex(col2)); allcategorylist.add(col2); }return allcategorylist; } return allcategorylist; } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " +table_categ_name); oncreate(db); }
}
basically the error explained in error message. learning program included learning read error messages.
you calling settext() method on textview object, except textview you're calling on doesn't exist (in memory, in it's not allocated). without know line reports error comes have no way of helping beyond that. suggest debugging , check objects turn being null when shouldn't be.
Comments
Post a Comment