基于一项活动和一些片段的应用程序。 片段启动时,应用程序崩溃并显示日志:
java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.alexsuvorov.paistewiki/ru.alexsuvorov.paistewiki.StartDrawer}: java.lang.NullPointerException
...
Caused by: java.lang.NullPointerException
at ru.alexsuvorov.paistewiki.Fragment.NewsFragment.onCreateView(NewsFragment.java:42)
我只是初始化一些数组位置,然后尝试显示它
现在我编辑代码
public class NewsFragment extends Fragment {
private static final String TAG = "NewsFragment";
private ImageSliderAdapter sliderAdapter;
private List<NewsMonth> monthArray;
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.news_fragment, container, false);
}
@Override
public void onViewCreated(@NonNull View view,
Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
ViewPager viewPager = view.findViewById(R.id.view_pager);
sliderAdapter = new ImageSliderAdapter(getContext());
viewPager.setAdapter(sliderAdapter);
initializeData();
RecyclerView recyclerView = view.findViewById(R.id.newsList);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(llm);
NewsAdapter adapter = new NewsAdapter(monthArray); /*this.getContext(), R.layout.news_item, monthArray*/
recyclerView.setAdapter(adapter);
}
private void initializeData() {
monthArray = new ArrayList<>();
monthArray.add(new NewsMonth("www.ya.ru", "Январь", null));
monthArray.add(new NewsMonth("www.on.ru", "Февраль", null));
monthArray.add(new NewsMonth("www.ti.ru", "Март", null));
}
}
我的XML文件-回收清单和卡片项目: ... .... ...
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/newsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:orientation="vertical"
android:padding="8dp">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
layout_constraintEnd_toStartOf="@+id/newsList"
android:layout_width="match_parent"
android:layout_height="103dp"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</android.support.v4.view.ViewPager>
<ScrollView
android:id="@+id/newsScroll"
android:layout_width="match_parent"
android:layout_height="433dp"
android:paddingEnd="@dimen/fab_margin"
app:layout_anchor="@+id/view_pager"
app:layout_anchorGravity="bottom"
app:layout_constraintTop_toBottomOf="@id/view_pager"
tools:layout_constraintTop_toBottomOf="@+id/view_pager"
android:paddingRight="@dimen/fab_margin">
<android.support.v7.widget.RecyclerView
android:id="@+id/newsList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|bottom|left|right"
android:orientation="vertical"
app:layout_anchor="@+id/view_pager"
app:layout_anchorGravity="bottom|center"
app:layout_constraintTop_toBottomOf="@id/view_pager"
tools:layout_editor_absoluteX="3dp"
tools:layout_editor_absoluteY="112dp">
</android.support.v7.widget.RecyclerView>
</ScrollView>
和卡片项目: 一些字符的文本,以使堆栈不会发誓 一些字符的文本,以使堆栈不会发誓 一些字符的文本,以使堆栈不会发誓 一些字符的文本,以使堆栈不会发誓
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:background="@color/black"
app:cardCornerRadius="8dp"
app:cardUseCompatPadding="true"
app:contentPadding="0dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="@+id/news_label"
android:layout_width="255dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:background="@color/colorAccent"
tools:text="Who is.. James bond??!" />
<TextView
android:id="@+id/news_category"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/news_label"
android:layout_alignBottom="@+id/news_label"
android:layout_toEndOf="@+id/news_label"
android:layout_toRightOf="@+id/news_label"
android:background="@android:color/holo_green_light"
tools:text="Artist" />
</RelativeLayout>
<TextView
android:id="@+id/month_name"
android:layout_width="150dp"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:background="@color/colorPrimaryDark"
android:fontFamily="sans-serif-condensed"
android:gravity="center"
android:textSize="16sp"
android:textStyle="bold"
tools:text="Июнь" />
答案 0 :(得分:1)
请勿在OnCreateView内编写findViewById()语句。
仅使用OnCreateView()扩大视图。
在OnViewCreated()方法中编写findViewById()方法,该方法将在视图创建完成后立即调用。
通过在onCreateView()中编写findViewById(),您试图在容器View仍处于创建过程中并且您可能尚未创建视图的情况下查找视图。