0

にリサイクルビューを設定しながら、私は私のAndroidアプリの実行時に、この特定のエラーを取得しています:nullポインタ例外フラグメント

java.lang.NullPointerException: Attempt to invoke virtual method`'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference 
                       at com.monkeyengineer.project.MyFragment.onCreateView(MyFragment.java:36) 

私はアンドロイドのリサイクルビューとアダプタのマニュアルをチェックしているが、それは私を助けていませんなぜこれがクラッシュしているのかと。私はコンテナビュー上findViewByIdメソッド(import文を簡潔にするため省略されている)を呼び出していますのはここ

は私の断片に私のコードです:ここでは

public class MyFragment extends Fragment { 

    private List<Project> data; 
    private RecyclerView recyclerView; 
    private TextView textView; 

    private boolean mIsRecyclerViewVisible = true; 

    public MyFragment() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     View mView = inflater.inflate(R.layout.my_fragment, container, false); 
     textView = (TextView) container.findViewById(R.id.text_view); 
     recyclerView = (RecyclerView) container.findViewById(R.id.recycler_view); 
     recyclerView.setHasFixedSize(true); 

     LinearLayoutManager mLayoutManager = new LinearLayoutManager(getContext()); 
     mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
     recyclerView.setLayoutManager(mLayoutManager); 

     Adapter adapter = new ProjectsAdapter(data); 
     recyclerView.setAdapter(adapter); 

     if (data != null) { 
      textView.setVisibility(View.INVISIBLE); 
      mIsRecyclerViewVisible = true; 
     } else { 
      recyclerView.setVisibility(View.INVISIBLE); 
      mIsRecyclerViewVisible = false; 
     } 
     return mView; 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
     if (mIsRecyclerViewVisible) { 
      recyclerView.setVisibility(View.VISIBLE); 
      textView.setVisibility(View.INVISIBLE); 
     } else { 
      recyclerView.setVisibility(View.INVISIBLE); 
      textView.setVisibility(View.VISIBLE); 
     } 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 
     mIsRecyclerViewVisible = data != null; 
    } 
} 

を私のフラグメントのレイアウトです:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context="com.monkeyengineer.project.MyFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recycler_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scrollbars="vertical" /> 

    <TextView 
     android:id="@+id/text_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:height="45dp" 
     android:drawableLeft="@android:drawable/ic_input_add" 
     android:drawableStart="@android:drawable/ic_input_add" 
     android:drawablePadding="8dp" 
     android:gravity="start|center" 
     android:padding="8dp" 
     android:text="Add stuff." 
     android:textStyle="bold" 
     android:layout_gravity="center_horizontal" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" /> 

</RelativeLayout> 

なぜ、nullポインタ例外が発生するのですか?それは、リサイクラビューオブジェクトがnull値に割り当てられているためですが、理由を考えることができないためです。

ありがとうございました。

答えて

2

あなたはそれを間違って膨張させています。 containerからmViewに意見を拡大してください。以下のように変更してください:

textView = (TextView) mView.findViewById(R.id.text_view); 
    recyclerView = (RecyclerView) mView.findViewById(R.id.recycler_view); 
+0

@TomFinetこれがあなたを助けたら私の答えを受け入れてください。 :) – SripadRaj

+0

ありがとう、それはそのような明白な間違いだった。 –