为什么ViewRecycler会在onMeasure上爆炸

时间:2015-05-23 19:21:31

标签: android android-cardview

我在一个Fragment中使用了CardView的ViewRecycler,它是ActionBarActivity上ViewPager的一部分。所有这些来自支持库。

当我尝试打开应用程序时,它会崩溃并出现以下异常。我错过了一个已知问题或解决方案吗?

java.lang.NullPointerException
        at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
        at android.view.View.measure(View.java:16736)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1456)
        at android.view.View.measure(View.java:16736)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5178)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:16736)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5178)
        at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
        at android.view.View.measure(View.java:16736)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5178)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:16736)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5178)
        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:16736)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5178)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2392)
        at android.view.View.measure(View.java:16736)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2095)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1208)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1394)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1095)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6010)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:799)
        at android.view.Choreographer.doCallbacks(Choreographer.java:599)
        at android.view.Choreographer.doFrame(Choreographer.java:559)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:784)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5872)
        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:852)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668)
        at dalvik.system.NativeStart.main(Native Method)

我尝试了各种建议,例如在我有一个RelativeLayout的地方使用LinearLayouts,但没有任何帮助,所以我还原了。我的布局和代码如下

摇篮

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile 'com.android.support:cardview-v7:21.0.+'
    compile 'com.android.support:recyclerview-v7:21.0.+'
}

活动

public class LaunchActivity extends ActionBarActivity {

    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);
        mSectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
    }

    ...
}

活动布局

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/pager"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context="com.me.LaunchActivity" />

片段

public class RotaFragment extends Fragment {

    private RecyclerView mCardRecycler;

    public static RotaFragment newInstance() {
        ...
    }

    public RotaFragment() { }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_rota, container, false);
        mCardRecycler = (RecyclerView) rootView.findViewById(R.id.card_recycler);
        return rootView;
   }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        List<WorkingFortnight> dummy = new ArrayList<>();
        ....
        mCardRecycler.setLayoutManager( new LinearLayoutManager(getActivity()) );
        mCardRecycler.setAdapter(new RotaCardAdapter(dummy));
    }
}

片段布局

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/card_recycler"
    android:background="@drawable/activity_bg"
    android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>

适配器(在ViewRecycler上设置以设置CardViews)

public class RotaCardAdapter extends RecyclerView.Adapter<RotaCardAdapter.RotaViewHolder> {

    private List<WorkingFortnight> mRotas;

    public RotaCardAdapter(List<WorkingFortnight> rotas) {
        mRotas = rotas;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public RotaViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rota_card, parent, false);
        return new RotaViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RotaViewHolder holder, int position) {
        WorkingFortnight rota = mRotas.get(position);
        holder.setHours( ... );
    }

    public class RotaViewHolder extends RecyclerView.ViewHolder {

        private TextView mCaption;
        private RotaListAdapter mListAdapter;

        public RotaViewHolder(View v) {
            super(v);
            ...
            mList.setAdapter(...);
        }

        ...
     }

适配器视图(卡片)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    card_view:elevation="3dp"
    card_view:cardBackgroundColor="@color/card"
    card_view:cardCornerRadius="4dp">

    <RelativeLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="4dp">

        <ImageView android:id="@+id/card_image"
            android:scaleType="centerCrop"
            android:src="@drawable/wk01"
            android:contentDescription="@string/label_card"
            android:layout_alignParentTop="true"
            android:layout_width="fill_parent"
            android:layout_height="128dp" />

        <TextView android:id="@+id/card_caption"
                android:layout_alignTop="@+id/card_image"
            android:layout_alignRight="@+id/card_image"
            android:layout_marginTop="4dp"
            android:layout_marginRight="8dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ListView android:id="@+id/card_rota"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        </ListView>

    </RelativeLayout>

</android.support.v7.widget.CardView>

我意识到这是一个复杂的嵌套布局,但我希望有人知道我在这里做过的事情的一些问题,也许某些视图不能嵌套但我的搜索中没有出现任何问题,并简化了测试的内容到目前为止没有帮助。

2 个答案:

答案 0 :(得分:1)

出现空指针,因为Recycler视图的布局管理器为空。

您必须尽快设置布局管理器:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_rota, container, false);
    mCardRecycler = (RecyclerView) rootView.findViewById(R.id.card_recycler);
    List<WorkingFortnight> dummy = new ArrayList<>();
    ....
    mCardRecycler.setLayoutManager( new LinearLayoutManager(MyApplication.getApplicationContext()) );
    mCardRecycler.setAdapter(new RotaCardAdapter(dummy));
    return rootView;
}

答案 1 :(得分:0)

将布局分开后,我发现当使用RecyclerView的片段嵌套在ViewPager(android.support.v4.view.ViewPager)中时触发器上的onMeasure异常。使用简单的FrameView作为活动布局避免了这个问题。

我没有将此标记为接下来48小时内接受的答案 - 如果您知道原因,请为此设置解决方法或设法找到相关错误(我没有)报告请发布,我会考虑接受你的回答并从我自己的回答中删除这些评论,谢谢