我在 android 中使用滑动菜单,我在通过 Fragment 获取上下文时遇到了很多麻烦。主要活动:
slide = new SlidingMenu(this);
slide.setMode(SlidingMenu.LEFT);
slide.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
slide.setFadeDegree(0.35f);
slide.setBehindOffsetRes(R.dimen.slidingmenu_offset);
slide.setShadowWidthRes(R.dimen.slidingmenu_shadow_width);
slide.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
slide.setMenu(R.layout.left_panel);
getActionBar().setDisplayHomeAsUpEnabled(true);
getFragmentManager().beginTransaction().replace(R.id.leftPanel, new LeftPanel()).commit();
我尝试使用:
Context context;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.d("Spread",activity.toString());
context = activity;
}
和
this.getActivity()
它总是返回null。这是怎么回事?
编辑:使用上下文:
com.nostra13.universalimageloader.core.ImageLoader imageLoader = com.nostra13.universalimageloader.core.ImageLoader.getInstance(); \
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
DisplayImageOptions conf = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true).build();
if (((ParseFile)user.get("profilePicture")).getUrl() != null) imageLoader.displayImage(((ParseFile)user.get("profilePicture")).getUrl(), profPic, conf, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
@Override
public void onLoadingFailed(String imageUri, View view,FailReason failReason) {
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
}
});*/