我正在尝试添加选项以单击片段中的Gridview中的图像/视频,但它说我无法将我的主要活动转换为我创建的OnClickThumbListener接口... 我认为这是因为我使用片段而不是FragmentActivity。
主:
public class MyPostsScreen extends Fragment implements
LoaderManager.LoaderCallbacks<Cursor>, MediaStoreAdapter.OnClickThumbListener{
private OnFragmentInteractionListener mListener;
private final static int READ_EXTERNAL_STORAGE_PERMMISSION_RESULT = 0;
private final static int MEDIASTORE_LOADER_ID = 0;
private RecyclerView mThumbnailRecyclerView;
private MediaStoreAdapter mMediaStoreAdapter;
public MyPostsScreen() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_my_posts_screen, container, false);
mThumbnailRecyclerView = (RecyclerView) v.findViewById(R.id.thumbnailRv);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this.getContext(), 3);
mThumbnailRecyclerView.setLayoutManager(gridLayoutManager);
mMediaStoreAdapter = new MediaStoreAdapter(this.getActivity());
mThumbnailRecyclerView.setAdapter(mMediaStoreAdapter);
checkReadExternalStoragePermission();
// Inflate the layout for this fragment
return v;
}
}
MediaStoreAdapter:
public class MediaStoreAdapter extends RecyclerView.Adapter<MediaStoreAdapter.ViewHolder> {
private Cursor mMediaStoreCursor;
private final Activity mActivity;
private OnClickThumbListener mOnClickThumbListener;
public interface OnClickThumbListener {
void OnClickImage(Uri imageUri);
void OnClickVideo(Uri videoUri);
}
public MediaStoreAdapter(Activity activity) {
this.mActivity = activity;
//TODO: Figure out why this fails Java Class Exception, and FIX.
this.mOnClickThumbListener = (OnClickThumbListener)activity;
}
}
ERROR:
致命的例外:主要 处理:example.fadysi.fomoapptest2,PID:21631 java.lang.ClassCastException:example.fadysi.fomoapptest2.MainActivity 无法施展 example.fadysi.fomoapptest2.ThumbnailVideoPlay.MediaStoreAdapter $ OnClickThumbListener 在 example.fadysi.fomoapptest2.ThumbnailVideoPlay.MediaStoreAdapter。(MediaStoreAdapter.java:31) 在layout.MyPostsScreen.onCreateView(MyPostsScreen.java:65) 在 android.support.v4.app.Fragment.performCreateView(Fragment.java:2087) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295) 在 android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801) 在 android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1643) 在 android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679) 在 android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143) 在android.support.v4.view.ViewPager.populate(ViewPager.java:1272) 在android.support.v4.view.ViewPager.populate(ViewPager.java:1120) 在android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1646) 在android.view.View.measure(View.java:18794) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 在 android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:703) 在 android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90) 在 android.support.design.widget.AppBarLayout $ ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1367) 在 android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:768) 在android.view.View.measure(View.java:18794) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 在 android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139) 在android.view.View.measure(View.java:18794) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 在 android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 在android.view.View.measure(View.java:18794) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 在android.view.View.measure(View.java:18794) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 在 android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 在android.view.View.measure(View.java:18794) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 在 com.android.internal.policy.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2643) 在android.view.View.measure(View.java:18794) 在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100) 在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) 在 android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6013) 在 android.view.Choreographer $ CallbackRecord.run(Choreographer.java:858) 在android.view.Choreographer.doCallbacks(Choreographer.java:670) 在android.view.Choreographer.doFrame(Choreographer.java:606) 在 android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:844) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5417) 在java.lang.refle
答案 0 :(得分:3)
将其更改为
private final Context mActivity;
....
....
public MediaStoreAdapter(Context context, OnClickThumbListener onClickThumbListener) {
this.mActivity = context;
this.mOnClickThumbListener = onClickThumbListener;
}
在MyPostsScreen
片段中,调用适配器构造函数,如下所示,
mMediaStoreAdapter = new MediaStoreAdapter(this.getActivity(), this);
mThumbnailRecyclerView.setAdapter(mMediaStoreAdapter);
这是因为您的界面是由片段实现的,而不是MainActivity。