2016-07-01 28 views
-1

以下是錯誤能不能使用sharedpreference

過程中添加最喜愛的清單:com.example.sonu.travelfreak,PID:在com.example.sonu 15374 顯示java.lang.NullPointerException 。在android.view.android.widget.CompoundButton.performClick(CompoundButton.java:100) android.view.View.performClick(View.java:4438) 。查看$ 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) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 在dalvik.system.NativeStart.main(本機方法)

類,添加自己喜歡的

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{ 

       } 

      } 


    }); 

} 

}

類的recyclerview的每一行設定值

public class fav_info { 
String string; 
public String getname(){ 
    return string; 
} 
public void setname(String name){ 
    this.string=name; 
}} 

sharedpreference類處理添加,刪除,保存收藏

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; 
}} 
+0

你我已經錯過了nyway thnx –

回答

0

您可以選擇使用它的對象

訪問sharedPreference對象之前,所以加這行之前,實例化SharedPreference類,

SharedPreference sharedPreference = new SharedPreference(); 

我想你忘了添加此行..

注意:您代碼已宣佈SharedPreference作爲一個類的屬性,所以下面的線就可以了,

sharedPreference = new SharedPreference(); 

感謝