I have a listview which opens a new single itemview activity when clucked on each list item . Inside the singleitemview activity i have a favorites button which adds the list item that opened the singleitemview activity to my favorites activity using sharedpreferences.. To pass the listitem clicked I use jackson library to convert respective listitem to json string and use putextra via intent into singleitemview activity . Then I convert back the json string to listitem object in singleitemview and use it to add to favorites
But now when i click the favorite button in singleitemview the app crashes and after reopening the app the listutem is added to my favorites activity
Here is the code
onitemclicklistener of my 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 this 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) {
for (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 at com.mycompany.myapp.SingleItemView$100000000.onClick(SingleItemView.java:62)
01-27 17:29:19.777 14852 14852 E AndroidRuntime at android.view.View.performClick(View.java:4452)
01-27 17:29:19.777 14852 14852 E AndroidRuntime at android.widget.Button.performClick(Button.java:148)
01-27 17:29:19.777 14852 14852 E AndroidRuntime at android.view.View$PerformClick.run(View.java:18428)
01-27 17:29:19.777 14852 14852 E AndroidRuntime at android.os.Handler.handleCallback(Handler.java:725)
01-27 17:29:19.777 14852 14852 E AndroidRuntime at android.os.Handler.dispatchMessage(Handler.java:92)
01-27 17:29:19.777 14852 14852 E AndroidRuntime at android.os.Looper.loop(Looper.java:176)
01-27 17:29:19.777 14852 14852 E AndroidRuntime at android.app.ActivityThread.main(ActivityThread.java:5365)
01-27 17:29:19.777 14852 14852 E AndroidRuntime at java.lang.reflect.Method.invokeNative(Native Method)
01-27 17:29:19.777 14852 14852 E AndroidRuntime at java.lang.reflect.Method.invoke(Method.java:511)
01-27 17:29:19.777 14852 14852 E AndroidRuntime at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
01-27 17:29:19.777 14852 14852 E AndroidRuntime at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
01-27 17:29:19.777 14852 14852 E AndroidRuntime at dalvik.system.NativeStart.main(Native Method)
01-27 17:29:20.316 17044 17044 D AndroidRuntime Calling main entry com.android.commands.am.Am