こんにちは、私は以下のエラーがあります。解決できないようです。RecyclerViewレイアウトマネージャでnullポインタ例外を取得する
私が持っている
のjava.lang.NullPointerException: '無効android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView $のLayoutManager)' 仮想メソッドを呼び出すための試みnullオブジェクトリファレンスで
私が持っているコードは、アクティビティではなくフラグメントでこれをやっているので、通常のサンプルと少し異なります。
これは私のコードです。
私は、全体のフラグメントのためのXMLを持っています。そして、
<FrameLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="mtr.MainActivityFragmentTwo">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/grades"
android:textStyle="bold"
android:textSize="40dp"
android:gravity="center_horizontal"
android:layout_gravity="center"
/>
</FrameLayout>
I持ってRecyclerViewを保持しているcontent_fragment
public class MainActivityFragmentTwo extends Fragment {
private Realm realm;
private RecyclerView recyclerView;
public MainActivityFragmentTwo() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main_activity_fragment_two, container, false);
}
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
realm.init(getActivity());
realm = Realm.getDefaultInstance();
recyclerView = (RecyclerView) getActivity().findViewById(R.id.realm_recycler_view);
setupRecyclerView();
}
public void setupRecyclerView() {
Log.d("Found grades", "not showing grades tho");
recyclerView.setLayoutManager(new LinearLayoutManager(this.getContext()));
recyclerView.setAdapter(new gradeRealmAdapter(this, realm.where(Grade.class).findAllAsync()));
recyclerView.setHasFixedSize(true);
}
}
を次のように
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="@+id/realm_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
はその後、私の断片的に私のコードが見えます私が作成したアダプタは
です3210エラーが、私はこの問題を解決することができないよう、次の行
recyclerView.setLayoutManager(new LinearLayoutManager(this.getContext()));
上のフラグメント・クラスから来ています。
誰かが私が間違っているのを見ることができますか?
onCreate
にonViewCreated()
ないであなたのリサイクルビューを膨らますることが保証されている、あまりにも内
onAttach
、そのあなたは確かにrecyclerViewはnullではありませんか? XMLレイアウトにも存在しますか? 'onCreateView'で' RecyclerView'を初期化してみませんか? 'onCreate()'が呼び出され/呼び出され/実行されていることが確実ですか? – Sharkthis.getContext())をgetActivity()に置き換えてみてください – Adilp
....なぜアクティビティからビューを取得しようとしていますか? – EpicPandaForce