我試圖在我的第一個活動和駐紮在第二個活動上的片段之間使用共享偏好。該代碼顯示了我迄今爲止所做的工作,但我在獲取上下文時遇到了問題。我也認爲我在做什麼不會工作,所以只需要一些快速的幫助。感謝使用片段和活動之間的共享偏好
嘗試一個空對象上調用虛擬方法android.content.Context android.content.Context.getApplicationContext()' 參考
READ PREFS
sharedPreferences = getSharedPreferences("alexcz", MODE_PRIVATE);
String drawableString = sharedPreferences.getString("PARTICLE_TYPE", "null")
WRITE PREF
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_particle_type, container, false);
white_blur = (ImageButton)view.findViewById(R.id.white_blur);
green_blur = (ImageButton)view.findViewById(R.id.green_blur);
orange_blur = (ImageButton)view.findViewById(R.id.orange_blur);
pink_blur = (ImageButton)view.findViewById(R.id.pink_blur);
yellow_blur = (ImageButton)view.findViewById(R.id.yellow_blur);
blue_blur = (ImageButton)view.findViewById(R.id.blue_blur);
main main = new main();
Context context = main.getApplicationContext();
sharedPref = context.getSharedPreferences("alexcz", main.MODE_PRIVATE);
editor = sharedPref.edit();
editor.putString("PARTICLE_TYPE", "white_blur");
setOnClickListeners();
return view;
}
你實際上並不是在任何地方寫字。你不使用'編輯器'。 – Vucko