我正在创建一个片段并读取sqlite并假设要填充屏幕中的数据。但是应用程序崩溃并出现以下错误。
FATAL EXCEPTION: main
Process: uk.ac.gre.facilitiesreviewapp, PID: 6249
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView$LayoutManager.onMeasure(android.support.v7.widget.RecyclerView$Recycler, android.support.v7.widget.RecyclerView$State, int, int)' on a null object reference
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
at android.view.View.measure(View.java:17536)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
at android.view.View.measure(View.java:17536)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1456)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2629)
at android.view.View.measure(View.java:17536)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2102)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1218)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1435)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1106)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6018)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:792)
at android.view.Choreographer.doCallbacks(Choreographer.java:596)
at android.view.Choreographer.doFrame(Choreographer.java:557)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:778)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
我知道如果我没有将layoutmanager
设置为Recyclerview
,就会发生此错误,但我已经按照您在下面的课程中看到的那样做了。
类
public class WifiFragment extends Fragment {
ReviewsDataSource mReviewsDataSource;
List<Review> mReviews;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
public WifiFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_review_list, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.review_recycler_view);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mReviewsDataSource = new ReviewsDataSource(getActivity());
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
try {
mReviewsDataSource.open();
mReviews = mReviewsDataSource.getAllReviewsByFacility(getActivity().getString(R.string.title_section1));
mAdapter = new ReviewAdapter(mReviews);
mRecyclerView.setAdapter(mAdapter);
Toast.makeText(getActivity(), String.valueOf(mReviews.size()), Toast.LENGTH_SHORT).show();
Log.v(getActivity().getString(R.string.title_section1), String.valueOf(mReviews.size()));
} catch (SQLException e) {
e.printStackTrace();
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="uk.ac.gre.facilitiesreviewapp.ReviewList$PlaceholderFragment">
<!-- A RecyclerView with some commonly used attributes -->
<android.support.v7.widget.RecyclerView
android:id="@+id/review_recycler_view"
android:foreground="?selectableItemBackground"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
我已根据某些答案中的建议将LayoutManager部分从onCreateView
移至onActivityCreated
,但仍无法提供帮助。知道为什么会这样吗?
答案 0 :(得分:2)
可能是因为您没有在使用相同布局文件的片段中实现RecylerView。我也有这个问题。