09-24 19:27:06.711 3474-3474/com.mstation.firstblood E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mstation.firstblood, PID: 3474
java.lang.NullPointerException
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
at android.view.View.measure(View.java:17221)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5156)
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:17221)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:17221)
at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1613)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.widget.ScrollView.onMeasure(ScrollView.java:563)
at android.view.View.measure(View.java:17221)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1488)
at android.view.View.measure(View.java:17221)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:17221)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5156)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:17221)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5156)
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:17221)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5156)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2481)
at android.view.View.measure(View.java:17221)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2317)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1179)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1554)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1066)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6245)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5117)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
我搜索了很多答案,其中大部分是因为没有设置 LayoutManager。但这不是我的理由。
我的情况是 viewpager 中的一个片段,布局是
<ScrollView>
<RecyclerView></RecyclerView>
</ScrollView>
这是java代码
public class CartoonFragment extends BaseLazyFragment {
private AutoScrollViewPager autoScrollViewPager;
private ArrayList<AdvertiseFragment> advertiseList = new ArrayList<>();
private RecyclerView latestRecyclerView;
private RecyclerView.LayoutManager layoutManager;
private LatestCartoonAdapter latestCartoonAdapter;
private LinearLayout waveLineLayout;//分类布局的 波浪线
private int POINTER_WIDTH = 20;//波浪线圆点的宽度 单位:dp
private boolean isPrepared;//fragment中view创建是否完成。
private int AD_NUM = 4;//广告页面数
private int SCROLL_FACTOR = 7;//广告viewpager页面,切换的速度因子。
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_cartoon_page, null);
//do init
autoScrollViewPager = (AutoScrollViewPager) view.findViewById(R.id.advertise_viewpager);
Log.e("autoScrollViewPager", "findViewById-------------");
latestRecyclerView = (RecyclerView) view.findViewById(R.id.latest_recycler_view);
layoutManager = new GridLayoutManager(getActivity(),3);
latestCartoonAdapter = new LatestCartoonAdapter();
latestRecyclerView.setHasFixedSize(true);
//i have set the layout manager , and debug layoutmanager is not null
latestRecyclerView.setLayoutManager(layoutManager);
latestRecyclerView.setAdapter(latestCartoonAdapter);
waveLineLayout = (LinearLayout) view.findViewById(R.id.waveline_layout);
initWaveLine();
for (int i = 0; i < AD_NUM; i++) {
AdvertiseFragment advertiseFragment = new AdvertiseFragment();
advertiseList.add(advertiseFragment);
}
ADFragmentAdapter adFragmentAdapter = new ADFragmentAdapter(getActivity().getSupportFragmentManager(), advertiseList);
configViewpager(adFragmentAdapter);
//init finish
isPrepared = true;
//lazyload
lazyLoad();
return view;
}
这是xml布局代码,布局中有两个recyclerview
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.mstation.firstblood.customer.AutoScrollViewPager
android:id="@+id/advertise_viewpager"
android:layout_width="match_parent"
android:layout_height="200dp" />
<LinearLayout
android:id="@+id/classify_layout"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_below="@id/advertise_viewpager"
android:background="@color/themeColor"
android:orientation="horizontal">
<TextView
style="@style/CartoonPage_Classify_text"
android:background="@drawable/round_text_bg_white"
android:text="分类" />
<TextView
style="@style/CartoonPage_Classify_text"
android:background="@drawable/round_text_bg_white"
android:text="排行" />
<TextView
style="@style/CartoonPage_Classify_text"
android:background="@drawable/round_text_bg_white"
android:text="专题" />
<TextView
style="@style/CartoonPage_Classify_text"
android:background="@drawable/round_text_bg_white"
android:text="安利" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/round_pointer_size_half"
android:layout_below="@id/classify_layout"
android:background="@color/themeColor"
android:orientation="vertical" />
<LinearLayout
android:id="@+id/waveline_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/round_pointer_size"
android:layout_below="@id/classify_layout"
android:orientation="horizontal" />
<LinearLayout
android:id="@+id/latest_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/waveline_layout"
android:layout_marginTop="20dp"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="最近更新"
android:textColor="@color/themeColor"
android:textSize="15sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:text="更多"
android:textColor="@color/gray"
android:textSize="15sp" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/latest_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="200dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/recommend_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/latest_layout"
android:layout_marginTop="20dp"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="每日推荐"
android:textColor="@color/themeColor"
android:textSize="15sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:text="更多"
android:textColor="@color/gray"
android:textSize="15sp" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recommend_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
并且layout图片选中的部分是第一个recyclerview