0
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)

i have search a lot of answer, most of them was because LayoutManager was not been set. but this is not my reason.

my condition is a fragment in the viewpager ,and the layout is

<ScrollView>
    <RecyclerView></RecyclerView>
</ScrollView>

here is the java code

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

here is the xml layout code , there are two recyclerview in the layout

<?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>

and the layout picture the selected part is the first recyclerview

enter image description here

Jeffery Ma
  • 3,051
  • 1
  • 23
  • 26
  • finally i got the reason, because the special xml layout code . i have two RecyclerView in the layout , but when i do the test , i only initial first RecyclerView . so i got the conclusion , if the RecyclerView appear in the layout file , you should initial all of them , except you set the visible is gone – Jeffery Ma Sep 24 '15 at 12:07
  • thanks god you find the result. I am looking your question and thinking that what you really want to make in layout file. – Bhavdip Sagar Sep 24 '15 at 12:09
  • any way , thank you very much for checking my code . – Jeffery Ma Sep 24 '15 at 12:12
  • Yes correct, If you declare the RecyclerView in your xml file and do not use it and try to run the application it might be crash due to of empty RecyclerView – Bhavdip Sagar Sep 24 '15 at 12:15
  • yes , so it is the experience . official document did not mention it . thanks,man! – Jeffery Ma Sep 24 '15 at 12:20

1 Answers1

0

i got it finally, i have two RecycleView in my layout. but when i test the project, i only initial one of them .

so , the conclusion, when more than one RecycleView appear in the layout you should initial all of them in java code. except you set the visible attribute is gone.

enter image description here

enter image description here

Jeffery Ma
  • 3,051
  • 1
  • 23
  • 26