我有一个在cardView中显示列表的recyclerView。此recyclerview位于viewPager选项卡中显示的一个片段中。我想在点击cardview中的缩略图时转到新的片段。
我实施了以下onBindViewHolder
以获得此功能:
holder.thumbnail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment productDetailFragment = new ProductDetailFragment();
(FragmentActivity)mContext).getSupportFragmentManager().beginTransaction()
.replace(R.id.grid_page_recycler_view, productDetailFragment).commit();
}
});
但是在跑步之后,它会返回:
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at android.support.v7.widget.RecyclerView.findMinMaxChildLayoutPositions(RecyclerView.java:3757)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3494)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3019)
at android.view.View.measure(View.java:15819)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
at android.view.View.measure(View.java:15819)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1658)
at android.view.View.measure(View.java:15819)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:15819)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1081)
at android.view.View.measure(View.java:15819)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4890)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15819)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4890)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15819)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4890)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
at android.view.View.measure(View.java:15819)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1965)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1146)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1356)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1046)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4603)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
是什么原因?值得注意的是,如果我想调用另一个活动,则很容易实现。
感谢。
我必须说这里有几个类似的问题,但我无法使用它们。
答案 0 :(得分:2)
public void replaceFragment(Fragment fragment){
String backStateName = fragment.getClass().getName();
FragmentManager manager = getSupportFragmentManager();
boolean fragmentPopped = manager.popBackStackImmediate(backStateName, 0);
if (!fragmentPopped && manager.findFragmentByTag(backStateName) == null) {
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.frame_container, fragment, backStateName);
ft.addToBackStack(backStateName);
ft.commit();
}
}