android - App crash when onClick on 2nd item and so on in listview -


i beginner in programming. having issue code. whenever try click on 2nd item in list, app crashes.

this code:

public class mainactivity extends appcompatactivity {      listview lvcategories;     string[] categories = {         "school", "work", "family outings", "friends outings", "groceries" , "appointments", "indoor activities",             "outdoor activities", "games","overseas travelling"     };      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         lvcategories = (listview)findviewbyid(r.id.listviewcategories);          final arrayadapter<string> adapter = new arrayadapter(this, android.r.layout.simple_list_item_1, categories);         lvcategories.setadapter(adapter);          lvcategories.setonitemclicklistener(new adapterview.onitemclicklistener() {             @override             public void onitemclick(adapterview<?> parent, view view, int position, long id) {                 string category = (string) parent.getitematposition(position);                  if (category == "school") {                     intent school = new intent(view.getcontext(), school_activity.class);                     school.putextra("school_category", "");                     startactivity(school);                 }                 if (category == "work") {                     intent work = new intent(view.getcontext(), work_activity.class);                     work.putextra("work_category", "");                     startactivity(work);                 }             }         });     } } 

may know issue?

here error found logcat.

caused by: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.edittext.settext(java.lang.charsequence)' on null object reference

and here's work_activity

public class work_activity extends appcompatactivity {      button btnsave;     edittext etcontent;     textview tvlastupdated;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_work_activity);          btnsave = (button)findviewbyid(r.id.buttonsave);         etcontent = (edittext)findviewbyid(r.id.edittextcontent);         tvlastupdated = (textview)findviewbyid(r.id.textviewlastupdated);          intent intentreceived = getintent();         final string strcontent = intentreceived.getstringextra("work_category");         etcontent.settext(strcontent);         calendar = calendar.getinstance();         final string datetime = now.get(calendar.day_of_month) + "/" +                 (now.get(calendar.month) + 1) + "/" +                 now.get(calendar.year) + " " +                 now.get(calendar.hour_of_day) + ":" +                 now.get(calendar.minute);         tvlastupdated.settext(datetime);          btnsave.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 string strcontentsaved = etcontent.gettext().tostring();                 sharedpreferences preferences = getsharedpreferences("category_work_saved", mode_private);                 sharedpreferences.editor prefedit = preferences.edit();                 prefedit.putstring("contentsaved", strcontentsaved);                 prefedit.putstring("date", datetime);                 prefedit.commit();                 toast.maketext(work_activity.this, "content saved successfully", toast.length_short).show();                 finish();             }         });     }      @override     protected void onresume() {         super.onresume();         sharedpreferences preferences = getsharedpreferences("category_work_saved", mode_private);         string contentsaved = preferences.getstring("contentsaved", "");         etcontent.settext(contentsaved);         string datetime = preferences.getstring("date","");         tvlastupdated.settext(datetime);     } } 

you've critical problems in code. let me point out these issues here.

in mainactivity you're comparing string values wrongly. need use equals() method instead of == operator comparison compare string.

and yes, try sending work_activity or school_activity.

lvcategories.setonitemclicklistener(new adapterview.onitemclicklistener() {     @override     public void onitemclick(adapterview<?> parent, view view, int position, long id) {         string category = (string) parent.getitematposition(position);          if (category.equals("school")) {             intent school = new intent(view.getcontext(), school_activity.class);             school.putextra("school_category", "send here");             startactivity(school);         }         if (category.equals("work")) {             intent work = new intent(view.getcontext(), work_activity.class);             work.putextra("work_category", "send here");             startactivity(work);         }     } }); 

now in work_activity, need check null value received intent.

string strcontent = null; intent intentreceived = getintent();  // check if intent received null if (intentreceived != null)      strcontent = intentreceived.getstringextra("work_category");  // check if string content null  if(strcontent != null)      etcontent.settext(strcontent); 

another thing check if id of edittext in activity_work_activity layout edittextcontent. id needs match find edittext might cause crash here.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -