我有一个包含2个不同片段的ViewPager,当我在onResume
方法getActivity()
之间滑动时,总是返回null
。
public class ViewPagerAdapter extends FragmentPagerAdapter {
public static final int PAGE_RECORD = 0;
public static final int PAGE_CAPTURE = 1;
public static final int PAGE_PICK = 2;
private Context context;
public ViewPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
@Override
public Fragment getItem(int type) {
switch(type){
case 0:
return new RecordFragment();
case 1:
return new CaptureFragment();
}
return new Fragment();
}
@Override
public int getCount() {
return 2;
}
pager = (ViewPager) findViewById(R.id.pager);
pager_adapter = new ViewPagerAdapter(getSupportFragmentManager(), this);
pager.setAdapter(pager_adapter);
OnPageChangeListener pageChangeListener = new OnPageChangeListener() {
int currentPosition = 0;
@Override
public void onPageSelected(int newPosition) {
FragmentLifecycle fragmentToShow = (FragmentLifecycle)pager_adapter.getItem(newPosition);
fragmentToShow.onResumeFragment();
FragmentLifecycle fragmentToHide = (FragmentLifecycle)pager_adapter.getItem(currentPosition);
fragmentToHide.onPauseFragment();
currentPosition = newPosition;
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) { }
public void onPageScrollStateChanged(int arg0) { }
};
pager.setOnPageChangeListener(pageChangeListener);
和
public class CaptureFragment extends Fragment implements OnClickListener, FragmentLifecycle {
private static final String TAG = "CaptureFragment";
private ResizableCameraPreview mPreview;
private RelativeLayout mLayout;
private int camera_id = 0;
private ImageButton record_btn;
private ImageView flash_btn;
public ImageView done_btn;
private ImageView rotate_camera;
private ImageView delete_btn;
private TextView text;
public CaptureFragment() {
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
MainGifActivity.capture_fragment = this;
setRetainInstance(true);
Log.i(TAG, "onAttach");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(TAG, "onCreateView");
View view = inflater.inflate(R.layout.fragment_record, null);
mLayout = (RelativeLayout) view.findViewById(R.id.layout);
text = (TextView) view.findViewById(R.id.count_text);
text.setText(""
+ (TakenPictures.FRAMES_COUNT - TakenPictures.photos.size()));
delete_btn = (ImageView) view.findViewById(R.id.delete);
done_btn = (ImageView) view.findViewById(R.id.done_btn);
record_btn = (ImageButton) view.findViewById(R.id.record_btn);
rotate_camera = (ImageView) view.findViewById(R.id.rotate_btn);
flash_btn = (ImageView) view.findViewById(R.id.flash_btn);
delete_btn.setOnClickListener(this);
record_btn.setOnClickListener(this);
flash_btn.setOnClickListener(this);
done_btn.setOnClickListener(this);
rotate_camera.setOnClickListener(this);
return view;
}
@Override
public void onResume() {
super.onResume();
Log.i(TAG, "onResume " + getActivity()); //null
}
@Override
public void onPause() {
super.onPause();
Log.i(TAG, "onPause");
if(mPreview == null)
return;
mPreview.isPause = true;
mPreview.stop();
mLayout.removeView(mPreview);
mPreview = null;
}
public void forcePause(){
mPreview.isPause = true;
mPreview.stop();
mLayout.removeView(mPreview);
mPreview = null;
}
private void createCameraPreview() {
mPreview = new ResizableCameraPreview(MainGifActivity.capture_fragment.getActivity(), camera_id,
CameraPreview.LayoutMode.FitToParent, false, mLayout, 1, done_btn);
LayoutParams previewLayoutParams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
previewLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
mLayout.addView(mPreview, 0, previewLayoutParams);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.done_btn:
if (TakenPictures.photos.size() == 0)
Toast.makeText(getActivity(), "No frames recorded",
Toast.LENGTH_SHORT).show();
else {
startActivity(new Intent(getActivity(), EditGifActivity.class));
getActivity().finish();
}
mPreview.isRecording = false;
mPreview.isInterrupted = true;
break;
case R.id.record_btn:
synchronized (mPreview) {
if (mPreview == null)
return;
if (mPreview.isRecording) {
if (mPreview.isPause) {
if (mPreview.isOutOfMemory) {
Toast.makeText(getActivity(),
"Is out of memory, please click done",
Toast.LENGTH_SHORT).show();
break;
}
mPreview.isPause = false;
} else
mPreview.isPause = true;
} else {
mPreview.isRecording = true;
// TimeCounterTask time_counter = new TimeCounterTask();
// time_counter.execute(System.currentTimeMillis());
}
}
break;
case R.id.rotate_btn:
boolean did_r = false;
boolean did_p = false;
if (mPreview.isRecording)
did_r = true;
if (mPreview.isPause)
did_p = true;
synchronized (mPreview.mCamera) {
mPreview.isRotated = true;
camera_id = ((camera_id == 0) ? 1 : 0);
mPreview.stop();
mLayout.removeView(mPreview);
createCameraPreview();
mPreview.isRecording = did_r;
mPreview.isPause = did_p;
}
break;
case R.id.flash_btn:
mPreview.turnFlash();
break;
case R.id.delete:
synchronized (TakenPictures.photos) {
TakenPictures.photos.clear();
}
text.setText("Time: "
+ ((TakenPictures.FRAMES_COUNT - TakenPictures.photos
.size())));
}
}
@Override
public void onPauseFragment() {
Log.i(TAG, "onPauseFragment()");
//Toast.makeText(getActivity(), "onPauseFragment():" + TAG, Toast.LENGTH_SHORT).show();
}
@Override
public void onResumeFragment() {
onResume();
getActivity(); // always null
//Toast.makeText(getActivity(), "onResumeFragment():" + TAG, Toast.LENGTH_SHORT).show();
}
}
请帮我刷一下如何活动;
答案 0 :(得分:0)
您可以使用全局“上下文”变量,并可能将其转换为主要活动。您在类构造函数中设置了上下文,那么为什么不使用它而不是getActivitiy()?
答案 1 :(得分:0)
宣布这样的全球活动:
YourActivityName activity;
@Override
public void onCreate(Bundle savedInstanceState) {
activity = (YourActivityName) getActivity();
}
然后你可以在任何你想要的地方使用变量activity
,它不会为空。