我有一个ViewRecycler,里面使用了CardView,这是在ViewPager的ActionBarActivity中的Fragment中使用的。所有这些都来自Support库。
当我尝试打开应用程序时,它会崩溃并出现以下异常。是否已知有问题或我错过了解决方案?
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,但是没有起到帮助作用,所以我回退了。我的布局和代码如下。 Gradle
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>
我知道这是一个复杂的嵌套布局,但希望有人能找出我在这里做错了什么,也许某些视图不能嵌套,但我的搜索中没有相关问题,简化测试也没有帮助。