here is the error
Process: com.example.sonu.travelfreak, PID: 15374 java.lang.NullPointerException at com.example.sonu.travelfreak.PlaceView$1.onClick(PlaceView.java:42) at android.view.View.performClick(View.java:4438) at android.widget.CompoundButton.performClick(CompoundButton.java:100) at android.view.View$PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method)
class which add favorite
public class PlaceView extends AppCompatActivity{
TextView textView1;
TextView textView2;
ToggleButton toggleButton;
SharedPreference sharedPreference;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.place_view);
textView1= (TextView) findViewById(R.id.textViewActivity);
textView2= (TextView) findViewById(R.id.detail);
Intent intent=getIntent();
final Bundle bundle=intent.getExtras();
textView1.setText(bundle.getString("Activity"));
textView2.setText(bundle.getString("Detail"));
toggleButton= (ToggleButton) findViewById(R.id.toggleButton1);
toggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(toggleButton.isChecked()){
Log.d("check",bundle.getString("position name"));
Toast.makeText(getApplicationContext(),
"Added as favorites",
Toast.LENGTH_SHORT).show();
sharedPreference.addFavorite(getApplicationContext(),bundle.getString("position name"));
}
else{
}
}
});
}
}
class which set value for each row of recyclerview
public class fav_info {
String string;
public String getname(){
return string;
}
public void setname(String name){
this.string=name;
}}
sharedpreference class which handle add,remove,save favorites
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SharedPreference {
public static final String PREFS_NAME = "PRODUCT_APP";
public static final String FAVORITES = "Product_Favorite";
public SharedPreference() {
super();
}
public void saveFavorites(Context context, List<fav_info> favorites) {
SharedPreferences settings;
Editor editor;
settings = context.getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE);
editor = settings.edit();
Gson gson = new Gson();
String jsonFavorites = gson.toJson(favorites);
editor.putString(FAVORITES, jsonFavorites);
editor.commit();
}
public void addFavorite(Context context, String string) {
fav_info fi=new fav_info();
fi.setname(string);
List<fav_info> favorites = getFavorites(context);
if (favorites == null)
favorites = new ArrayList<fav_info>();
favorites.add(fi);
saveFavorites(context, favorites);
}
public void removeFavorite(Context context, fav_info fav_info) {
ArrayList<fav_info> favorites = getFavorites(context);
if (favorites != null) {
favorites.remove(fav_info);
saveFavorites(context, favorites);
}
}
public ArrayList<fav_info> getFavorites(Context context) {
SharedPreferences settings;
List<fav_info> favorites;
settings = context.getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE);
if (settings.contains(FAVORITES)) {
String jsonFavorites = settings.getString(FAVORITES, null);
Gson gson = new Gson();
fav_info[] favoriteItems = gson.fromJson(jsonFavorites,
fav_info[].class);
favorites = Arrays.asList(favoriteItems);
favorites = new ArrayList<fav_info>(favorites);
} else
return null;
return (ArrayList<fav_info>) favorites;
}}