gpt4 book ai didi

java.lang.ClassCastException : cast to created interface issue? 异常

转载 作者:行者123 更新时间:2023-11-29 04:34:09 24 4
gpt4 key购买 nike

我正在尝试添加选项以在片段中单击我的 Gridview 中的图像/视频,但它说我无法将我的主要 Activity 转换到我创建的 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;
}
}

媒体商店适配器:

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;
}
}

错误:

FATAL EXCEPTION: main Process: example.fadysi.fomoapptest2, PID: 21631 java.lang.ClassCastException: example.fadysi.fomoapptest2.MainActivity cannot be cast to example.fadysi.fomoapptest2.ThumbnailVideoPlay.MediaStoreAdapter$OnClickThumbListener at example.fadysi.fomoapptest2.ThumbnailVideoPlay.MediaStoreAdapter.(MediaStoreAdapter.java:31) at layout.MyPostsScreen.onCreateView(MyPostsScreen.java:65) at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801) at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1643) at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679) at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143) at android.support.v4.view.ViewPager.populate(ViewPager.java:1272) at android.support.v4.view.ViewPager.populate(ViewPager.java:1120) at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1646) at android.view.View.measure(View.java:18794) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:703) at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90) at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1367) at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:768) at android.view.View.measure(View.java:18794) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139) at android.view.View.measure(View.java:18794) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) at android.widget.LinearLayout.measureVertical(LinearLayout.java:748) at android.widget.LinearLayout.onMeasure(LinearLayout.java:630) at android.view.View.measure(View.java:18794) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) at android.view.View.measure(View.java:18794) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) at android.widget.LinearLayout.measureVertical(LinearLayout.java:748) at android.widget.LinearLayout.onMeasure(LinearLayout.java:630) at android.view.View.measure(View.java:18794) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643) at android.view.View.measure(View.java:18794) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) at android.view.Choreographer.doCallbacks(Choreographer.java:670) at android.view.Choreographer.doFrame(Choreographer.java:606) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.refle

最佳答案

改成

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 实现的。

关于java.lang.ClassCastException : cast to created interface issue? 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42465789/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com