android - Null pointer exception on pressing favorites button in single item view activity -
i have listview opens new single itemview activity when clucked on each list item . inside singleitemview activity have favorites button adds list item opened singleitemview activity favorites activity using sharedpreferences.. pass listitem clicked use jackson library convert respective listitem json string , use putextra via intent singleitemview activity . convert json string listitem object in singleitemview , use add favorites
but when click favorite button in singleitemview app crashes , after reopening app listutem added favorites activity
here code
onitemclicklistener of list activity
@override public void onitemclick(adapterview<?> parent, view view, int position, long id) { objectmapper mapper = new objectmapper(); product pro = productlistadapter.getitem(position); try { string jsoninstring = mapper.writevalueasstring(pro); intent intent = new intent(activity.getapplicationcontext(), singleitemview.class); intent.putextra("selected item", jsoninstring); startactivity(intent); } catch (jsonprocessingexception e) {//something went wrong } }
singleitemview.java
public class singleitemview extends activity { productlistadapter padaptr; sharedpreference sharedpreference; list<product> products = null; @override protected void oncreate(bundle savedinstancestate) { // todo: implement method super.oncreate(savedinstancestate); setcontentview(r.layout.singleitem); sharedpreference = new sharedpreference(); padaptr = new productlistadapter(singleitemview.this, products); button btn = (button) findviewbyid(r.id.singleitembutton1); btn.setonclicklistener(new onclicklistener(){ @override public void onclick(view v){ bundle extras = getintent().getextras(); string jsonobj = extras.getstring("selected item"); objectmapper mapper = new objectmapper(); try { product pro = mapper.readvalue(jsonobj, product.class); //the fav image present on evry list item imageview button = (imageview) findviewbyid(r.id.imgbtn_favorite); if (checkfavoriteitem(pro)) { sharedpreference.removefavorite(singleitemview.this, pro); button.settag("no"); button.setimageresource(r.drawable.heart_grey); toast.maketext(singleitemview.this, singleitemview.this.getresources().getstring(r.string.remove_favr), toast.length_short).show(); } else { sharedpreference.addfavorite(singleitemview.this, pro); toast.maketext(singleitemview.this, singleitemview.this.getresources().getstring(r.string.add_favr), toast.length_short).show(); button.settag("yes"); button.setimageresource(r.drawable.heart_red); } } catch (ioexception e) {}; } private boolean checkfavoriteitem(product checkproduct) { boolean check = false; list<product> favorites = sharedpreference.getfavorites(getapplicationcontext()); if (favorites != null) { (product product : favorites) { if (product.equals(checkproduct)) { check = true; break; } } } return check; } }); } }
the line pointing nullpointer eception log cat is
button.settag("yes");
singleitem.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#a25550" android:gravity="center"> <button android:layout_height="wrap_content" android:text="addcto fav" android:layout_width="wrap_content" android:id="@+id/singleitembutton1"/> <textview android:layout_height="wrap_content" android:text="large text" android:textappearance="?android:attr/textappearancelarge" android:layout_width="wrap_content" android:id="@+id/singleitemtextview1"/> </linearlayout>
list item xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/product_list_item_bg" android:descendantfocusability="blocksdescendants" > <relativelayout android:id="@+id/pdt_layout_item" android:layout_width="fill_parent" android:layout_height="wrap_content" > <textview android:id="@+id/txt_pdt_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="6dp" /> <textview android:id="@+id/txt_pdt_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/txt_pdt_name" android:padding="6dp" /> <textview android:id="@+id/txt_pdt_desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/txt_pdt_price" android:padding="6dp" /> <imageview android:id="@+id/imgbtn_favorite" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/txt_pdt_desc" android:layout_alignparentright="true" android:layout_marginright="3dp" android:background="@null" android:contentdescription="@string/favorites" /> </relativelayout> <view android:layout_width="match_parent" android:layout_height="1dp" android:layout_below="@+id/pdt_layout_item" android:background="@color/view_divider_color" /> </relativelayout>
my logcat
01-27 17:29:19.777 14852 14852 e androidruntime fatal exception: main 01-27 17:29:19.777 14852 14852 e androidruntime java.lang.nullpointerexception 01-27 17:29:19.777 14852 14852 e androidruntime @ com.mycompany.myapp.singleitemview$100000000.onclick(singleitemview.java:62) 01-27 17:29:19.777 14852 14852 e androidruntime @ android.view.view.performclick(view.java:4452) 01-27 17:29:19.777 14852 14852 e androidruntime @ android.widget.button.performclick(button.java:148) 01-27 17:29:19.777 14852 14852 e androidruntime @ android.view.view$performclick.run(view.java:18428) 01-27 17:29:19.777 14852 14852 e androidruntime @ android.os.handler.handlecallback(handler.java:725) 01-27 17:29:19.777 14852 14852 e androidruntime @ android.os.handler.dispatchmessage(handler.java:92) 01-27 17:29:19.777 14852 14852 e androidruntime @ android.os.looper.loop(looper.java:176) 01-27 17:29:19.777 14852 14852 e androidruntime @ android.app.activitythread.main(activitythread.java:5365) 01-27 17:29:19.777 14852 14852 e androidruntime @ java.lang.reflect.method.invokenative(native method) 01-27 17:29:19.777 14852 14852 e androidruntime @ java.lang.reflect.method.invoke(method.java:511) 01-27 17:29:19.777 14852 14852 e androidruntime @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1102) 01-27 17:29:19.777 14852 14852 e androidruntime @ com.android.internal.os.zygoteinit.main(zygoteinit.java:869) 01-27 17:29:19.777 14852 14852 e androidruntime @ dalvik.system.nativestart.main(native method) 01-27 17:29:20.316 17044 17044 d androidruntime calling main entry com.android.commands.am.am
reason: reason getting nullpointerexception
trying access id imgbtn_favorite
in singlelistitem.xml
not exist. you can access variable in xml associated activity class.
solution: noticed using variable set tag
, change imageresource
. can't activity. can either
store global value in
sharedpreferences
, show changed value whenlistview
opens again.or, if do not need open new activity, can access view in listview activity itself.
edit:
- you can use
startactivityforresult
send values listview activity and then make changes in variable. can refer this link example.
Comments
Post a Comment