java - Why is my "Notes" tab crashing? -
i've tried searching answer i'm relatively new area of programming , using android studio. firstly, error i'm receiving when emulator crashes. bear me theres lot of code , first time posting.
--------- beginning of crash 08-16 16:59:46.930 1861-1861/com.example.richard.stopandsearch e/androidruntime﹕ fatal exception: main process: com.example.richard.stopandsearch, pid: 1861 java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.textview.settext(java.lang.charsequence)' on null object reference @ com.example.richard.stopandsearch.mainactivity$noteslistadapter.getview(mainactivity.java:157) @ android.widget.abslistview.obtainview(abslistview.java:2347) @ android.widget.listview.measureheightofchildren(listview.java:1270) @ android.widget.listview.onmeasure(listview.java:1182) @ android.view.view.measure(view.java:17547) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5535) @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1436) @ android.widget.linearlayout.measurevertical(linearlayout.java:722) @ android.widget.linearlayout.onmeasure(linearlayout.java:613) @ android.view.view.measure(view.java:17547) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5535) @ android.widget.framelayout.onmeasure(framelayout.java:436) @ android.view.view.measure(view.java:17547) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5535) @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1436) @ android.widget.linearlayout.measurevertical(linearlayout.java:722) @ android.widget.linearlayout.onmeasure(linearlayout.java:613) @ android.view.view.measure(view.java:17547) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5535) @ android.widget.framelayout.onmeasure(framelayout.java:436) @ android.view.view.measure(view.java:17547) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5535) @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1436) @ android.widget.linearlayout.measurevertical(linearlayout.java:722) @ android.widget.linearlayout.onmeasure(linearlayout.java:613) @ android.view.view.measure(view.java:17547) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5535) @ android.widget.framelayout.onmeasure(framelayout.java:436) @ android.support.v7.internal.widget.contentframelayout.onmeasure(contentframelayout.java:124) @ android.view.view.measure(view.java:17547) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5535) @ android.support.v7.internal.widget.actionbaroverlaylayout.onmeasure(actionbaroverlaylayout.java:444) @ android.view.view.measure(view.java:17547) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5535) @ android.widget.framelayout.onmeasure(framelayout.java:436) @ android.view.view.measure(view.java:17547) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5535) @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1436) @ android.widget.linearlayout.measurevertical(linearlayout.java:722) @ android.widget.linearlayout.onmeasure(linearlayout.java:613) @ android.view.view.measure(view.java:17547) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5535) @ android.widget.framelayout.onmeasure(framelayout.java:436) @ com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2615) @ android.view.view.measure(view.java:17547) @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:2015) @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1173) @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1379) @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1061) @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:5885) @ android.view.choreographer$callbackrecord.run(choreographer.java:767) @ android.view.choreographer.docallbacks(choreographer.java:580) @ android.view.choreographer.doframe(choreographer.java:550) @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:753) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:135) @ android.app.activ
mainactivity.java
package com.example.richard.stopandsearch; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.text.editable; import android.text.textwatcher; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.button; import android.widget.edittext; import android.widget.listview; import android.widget.tabhost; import android.widget.textview; import android.widget.toast; import java.util.arraylist; import java.util.list; public class mainactivity extends appcompatactivity { edittext locationtxt, timetxt, datetxt, officertxt, detailstxt; list<notes> notes = new arraylist<notes>(); listview noteslistview; databasehandler dbhandler; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); locationtxt = (edittext) findviewbyid(r.id.txtlocation); timetxt = (edittext) findviewbyid(r.id.txttime); datetxt = (edittext) findviewbyid(r.id.txtdate); officertxt = (edittext) findviewbyid(r.id.txtofficer); detailstxt = (edittext) findviewbyid(r.id.txtdetails); noteslistview = (listview) findviewbyid(r.id.listview); dbhandler = new databasehandler(getapplicationcontext()); tabhost tabhost = (tabhost) findviewbyid(r.id.tabhost); tabhost.setup(); tabhost.tabspec tabspec = tabhost.newtabspec("addnote"); tabspec.setcontent(r.id.tabaddnote); tabspec.setindicator("addnote"); tabhost.addtab(tabspec); tabspec = tabhost.newtabspec("notes"); tabspec.setcontent(r.id.tabnotes); tabspec.setindicator("notes"); tabhost.addtab(tabspec); tabspec = tabhost.newtabspec("info"); tabspec.setcontent(r.id.tabinfo); tabspec.setindicator("info"); tabhost.addtab(tabspec); final button addbtn = (button) findviewbyid(r.id.btnadd); addbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { notes notes = new notes(dbhandler.getnotescount(), string.valueof(locationtxt.gettext()), string.valueof(timetxt.gettext()), string.valueof(datetxt.gettext()), string.valueof(officertxt.gettext()), string.valueof(detailstxt.gettext())); if (!noteexists(notes)) { // notes.add (new notes(0, locationtxt.gettext().tostring(), timetxt.gettext().tostring(), datetxt.gettext().tostring(), officertxt.gettext().tostring(), detailstxt.gettext().tostring())); dbhandler.createnote(notes); notes.add(notes); // populatelist(); toast.maketext(getapplicationcontext(), "your note has been added.", toast.length_short).show(); return; } toast.maketext(getapplicationcontext(), string.valueof(locationtxt.gettext()) + " exists. please enter different location.", toast.length_short).show(); } }); detailstxt.addtextchangedlistener(new textwatcher() { @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void ontextchanged(charsequence s, int start, int before, int count) { addbtn.setenabled(string.valueof(detailstxt.gettext()).trim().length() >0); // @override // public void aftertextchanged(editable s) { } @override public void aftertextchanged(editable s) { } }); // list<notes> addablenotes = dbhandler.getallnotes(); // int notecount = dbhandler.getnotescount(); // (int i=0; < notecount; i++) { // notes.add(addablenotes.get(i)); // } if (dbhandler.getnotescount()!=0) notes.addall(dbhandler.getallnotes()); // if (!addablenotes.isempty()) populatelist(); } private boolean noteexists(notes notes) { string location = notes.getlocation(); int notescount = notes.size(); (int i=0; < notescount; i++) { if(location.comparetoignorecase(notes.get(i).getlocation()) ==0) return true; } return false; } private void populatelist() { arrayadapter<notes> adapter = new noteslistadapter(); noteslistview.setadapter(adapter); } // private void addnotes(string location, string time, string date, string officer, string details) { //notes.add(new notes(location, time, date, officer, details)); // } public class noteslistadapter extends arrayadapter<notes> { public noteslistadapter() { super(mainactivity.this, r.layout.listviewitem, notes); } @override public view getview(int position, view view, viewgroup parent) { if (view == null) view = getlayoutinflater().inflate(r.layout.listviewitem, parent, false); notes currentnote = notes.get(position); textview location = (textview) view.findviewbyid(r.id.txtlocationview); location.settext(currentnote.getlocation()); textview time = (textview) view.findviewbyid(r.id.txttimeview); time.settext (currentnote.getlocation()); textview date = (textview) view.findviewbyid(r.id.txtdateview); date.settext (currentnote.getlocation()); textview officer = (textview) view.findviewbyid(r.id.txtofficer); officer.settext(currentnote.getlocation()); textview details = (textview) view.findviewbyid(r.id.txtdetailsview); details.settext(currentnote.getlocation()); return view; } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
databasehandler.java
package com.example.richard.stopandsearch; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import java.util.arraylist; import java.util.list; public class databasehandler extends sqliteopenhelper { private static final int database_version = 1; private static final string database_name = "notes", table_notes = "notes", key_id = "id", key_location = "location", key_time = "time", key_date = "date", key_officer = "officer", key_details = "details"; public databasehandler(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { db.execsql("create table " + table_notes + "(" + key_id + " integer primary key autoincrement, " + key_location + " text, " + key_time + " text, " + key_date + " text, " + key_officer + " text, " + key_details + " text)"); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " + table_notes); oncreate(db); } public void createnote(notes notes) { sqlitedatabase db = getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_location, notes.getlocation()); values.put(key_time, notes.gettime()); values.put(key_date, notes.getdate()); values.put(key_officer, notes.getofficer()); values.put(key_details, notes.getdetails()); db.insert(table_notes, null, values); db.close(); } public notes getnotes(int id) { sqlitedatabase db = getreadabledatabase(); cursor cursor = db.query(table_notes, new string[] { key_id, key_location, key_time, key_date, key_officer, key_details }, key_id + "=?", new string [] {string.valueof(id) }, null, null, null, null); if (cursor != null) cursor.movetofirst(); assert cursor != null; notes notes = new notes(integer.parseint(cursor.getstring(0)),cursor.getstring(1),cursor.getstring(2),cursor.getstring(3), cursor.getstring(4), cursor.getstring(5)); db.close(); cursor.close(); return notes; } public void deletenote(notes notes) { sqlitedatabase db = getwritabledatabase(); db.delete(table_notes, key_id + "=?", new string[] {string.valueof(notes.getid())}); db.close(); } public int getnotescount () { sqlitedatabase db = getreadabledatabase(); cursor cursor = db.rawquery("select * " + table_notes, null); int count = cursor.getcount(); cursor.close(); db.close(); return count; } public int updatenotes(notes notes) { sqlitedatabase db = getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_location, notes.getlocation()); values.put(key_time, notes.gettime()); values.put(key_date, notes.getdate()); values.put(key_officer, notes.getofficer()); values.put(key_details, notes.getdetails()); int rowsaffected = db.update(table_notes, values, key_id + "=?", new string[]{string.valueof(notes.getid())}); db.close(); return rowsaffected; } public list<notes> getallnotes() { list<notes> notes = new arraylist<notes>(); sqlitedatabase db = getwritabledatabase(); cursor cursor = db.rawquery("select * " + table_notes, null); if (cursor.movetofirst()) { { notes.add(new notes(integer.parseint(cursor.getstring(0)),cursor.getstring(1),cursor.getstring(2),cursor.getstring(3), cursor.getstring(4), cursor.getstring(5))); } while (cursor.movetonext()); } cursor.close(); db.close(); return notes; } }
notes.java
package com.example.richard.stopandsearch; public class notes { private string _location, _time, _date, _officer, _details; private int _id; public notes (int id, string location, string time, string date, string officer, string details) { _id = id; _location = location; _time = time; _date = date; _officer = officer; _details = details; } public int getid() {return _id; } public string getlocation() { return _location; } public string gettime() { return _time; } public string getdate() { return _date; } public string getofficer() { return _officer; } public string getdetails() { return _details; } }
activity_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity" android:orientation="vertical" android:background="#f9f9f9"> <tabhost android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/tabhost"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <tabwidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content"></tabwidget> <framelayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent"> <linearlayout android:id="@+id/tabinfo" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <imageview android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/imglogo3" android:layout_gravity="center_horizontal" android:src="@drawable/policelogo" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="information" android:id="@+id/txtinfo" android:textcolor="#1b0f3e" android:layout_gravity="center_horizontal" android:layout_margintop="16dp" /> </linearlayout> <linearlayout android:id="@+id/tabnotes" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imglogo" android:layout_gravity="center_horizontal" android:src="@drawable/policelogo" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="notes" android:id="@+id/txtnotes" android:layout_margintop="16dp" android:layout_gravity="center_horizontal" android:singleline="false" android:textcolor="#1b0f3e" /> <listview android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/listview" android:layout_gravity="center_horizontal" /> </linearlayout> <linearlayout android:id="@+id/tabaddnote" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <imageview android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/imglogo2" android:layout_gravity="center_horizontal" android:src="@drawable/policelogo" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="add note" android:id="@+id/lblnotestitle" android:layout_gravity="center_horizontal" android:layout_margintop="10dp" android:textcolor="#1b0f3e" /> <edittext android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputtype="textpersonname" android:ems="10" android:id="@+id/txtlocation" android:layout_margintop="16dp" android:hint="location" android:textcolor="#1b0f3e" /> <edittext android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputtype="time" android:ems="10" android:id="@+id/txttime" android:hint="time" android:layout_margintop="16dp" android:textcolor="#1b0f3e" /> <edittext android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputtype="date" android:ems="10" android:id="@+id/txtdate" android:hint="date" android:layout_margintop="16dp" android:textcolor="#1b0f3e" /> <edittext android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/txtofficer" android:hint="officer name/number" android:layout_margintop="16dp" android:textcolor="#1b0f3e" /> <edittext android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/txtdetails" android:hint="event details" android:layout_margintop="16dp" android:lines="4" android:textcolor="#1b0f3e" /> <button android:layout_width="200dp" android:layout_height="wrap_content" android:text="add note" android:id="@+id/btnadd" android:layout_gravity="center_horizontal" android:layout_margintop="16dp" android:enabled="false" android:textcolor="#1b0f3e" /> </linearlayout> </framelayout> </linearlayout> </tabhost> </linearlayout>
again apologise huge chunks of code, i'm hoping there's may obvious experienced eluding me. in advance.
going off of error log, looks if inflating listviewitem.xml
in getview()
method, widgets (textview
s) referencing in activity_main.xml
.
edit: well, @ least 1 is, id
txtofficer
in terms of resolution, ensure of id
attributes assign in xml
layouts unique, , ensure use findviewbyid()
locate widget contained inside calling view
:
view = getlayoutinflater().inflate(r.layout.listviewitem, parent, false); view.findviewbyid(*id of widget inside listviewitem.xml*)
Comments
Post a Comment