gpt4 book ai didi

android - 为什么ViewRecycler会在onMeasure上爆炸

转载 作者:太空宇宙 更新时间:2023-11-03 10:42:56 29 4
gpt4 key购买 nike

我让 ViewRecycler 在 Fragment 中使用 CardView,它是 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.+'
}

Activity

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

...
}

Activity 布局

<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" />

fragment

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

fragment 布局

<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(...);
}

...
}

适配器 View (卡片)

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

我意识到这是一个复杂的嵌套布局,但我希望有人知道我在这里所做的事情存在一些问题,也许某些 View 无法嵌套,但我的搜索中没有任何结果,并且简化了要测试的东西'到目前为止有帮助。

最佳答案

出现空指针是因为您的 Recycler View 的布局管理器为空。

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

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

关于android - 为什么ViewRecycler会在onMeasure上爆炸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30416680/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com