我想在viewpager中保存字符串,在另一个活动中保存SharedPreference
。但是,一旦我点击ViewPager
中的按钮,程序就会返回错误java.lang.NullPointerException。为什么下面的代码行返回Null Pointer Exception?是因为上下文指向空值吗?
sharedPreference.save(context, test);
以下是ViewPager
活动
public class FullScreenImageActivity extends AppCompatActivity {
ViewPager viewPager;
ImageView imageView;
private SharedPreference sharedPreference;
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager_image); //TBS
if (Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
Intent i = getIntent();
int position = i.getIntExtra("position", 0);
String[] arr=i.getStringArrayExtra("array");
String url = i.getStringExtra("url");
viewPager = (ViewPager) findViewById(R.id.slider);
viewPager.setAdapter(new GalleryViewPagerAdapter(this, arr,position,url));
viewPager.setCurrentItem(position);
}
class GalleryViewPagerAdapter extends PagerAdapter {
private Context context;
LayoutInflater inflater;
private String[] imageArrayList;
private String mUrl;
private int mPosition;
private ProgressDialog mProgress;
public GalleryViewPagerAdapter(Context _context, String[] imageArrayList, int position, String url) {
context = _context;
this.imageArrayList = imageArrayList; //null;
this.mPosition=position;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mUrl=url;
}
@Override
public int getCount() {
return imageArrayList.length;
}
@Override
public Object instantiateItem(final ViewGroup container, final int position) {
showProgress();
imageView = (ImageView) findViewById(R.id.btn_favourite);
imageView.setColorFilter(Color.argb(255, 192, 192, 192));
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//imageView.setColorFilter(Color.argb(255, 249, 0, 0));
String test="testing";
sharedPreference.save(context, test);
Toast.makeText(context,
"Added to Favourite!",
Toast.LENGTH_SHORT).show();
}
});
View photoRow = inflater.inflate(R.layout.item_image, container,
false);
ImageView image = (ImageView) photoRow.findViewById(R.id.img_flickr);
// added imageloader for better performance
StaggeredDemoApplication.getImageLoader().get(imageArrayList[position],
ImageLoader.getImageListener(image, R.drawable.bg_no_image, android.R.drawable.ic_dialog_alert), container.getWidth(), 0);
((ViewPager) container).addView(photoRow);
stopProgress();
return photoRow;
}
private void stopProgress() {
mProgress.cancel();
}
private void showProgress() {
mProgress = ProgressDialog.show(FullScreenImageActivity.this, "", "Loading...");
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
}
}
}
以下是SharedPreference
public class SharedPreference {
public static final String PREFS_NAME = "AOP_PREFS";
public static final String PREFS_KEY = "AOP_PREFS_String";
public SharedPreference() {
super();
}
public void save(Context context, String text) {
SharedPreferences settings;
Editor editor;
//settings = PreferenceManager.getDefaultSharedPreferences(context);
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); //1
editor = settings.edit(); //2
editor.putString(PREFS_KEY, text); //3
editor.commit(); //4
}
public String getValue(Context context) {
SharedPreferences settings;
String text;
//settings = PreferenceManager.getDefaultSharedPreferences(context);
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
text = settings.getString(PREFS_KEY, null);
return text;
}
public void clearSharedPreference(Context context) {
SharedPreferences settings;
Editor editor;
//settings = PreferenceManager.getDefaultSharedPreferences(context);
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
editor = settings.edit();
editor.clear();
editor.commit();
}
public void removeValue(Context context) {
SharedPreferences settings;
Editor editor;
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
editor = settings.edit();
editor.remove(PREFS_KEY);
editor.commit();
}
}