2016-03-02 71 views
0

我的代碼在Fragment以下部分:findViewById上RecyclerView總是空

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View root = inflater.inflate(R.layout.fragment_smartzone_category, container, false); 

    final Context context = getActivity(); 

    mPresenter = new SmartzonePresenter(context, this); 
    // 
    mProgressBar = (ProgressBar) root.findViewById(R.id.spinner); 
    mProgressBar.setVisibility(View.VISIBLE); 

    // set list 
    mStaggeredLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL); 
    mStaggeredLayoutManager.setSpanCount(3); 

    mRecyclerView = (RecyclerView) root.findViewById(R.id.smartzonecategorylist); 
    mRecyclerView.setLayoutManager(mStaggeredLayoutManager); 
    mRecyclerView.setVisibility(View.INVISIBLE);; 

    // display list 
    mListAdapter = new VideoCategoryAdapter(context, mCategories); 
    mRecyclerView.setAdapter(mListAdapter); 
    mListAdapter.setOnItemClickListener(new VideoCategoryAdapter.OnItemClickListener() { 
     @Override 
     public void onItemClick(View view, int position) { 
      // get the category 
      VideoCategory vc = mListAdapter.getItem(position); 
      // display the sub-smartzone category page 
      mCallback.onSelectCategory(vc, mCategories); 
     } 
    }); 

    // 
    retrieveVideoList(); 

    return root; 
} 

mRecyclerViewFragment類的頭被宣佈。

佈局對應於這個XML文件:

<FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:padding="8dp"> 

<ProgressBar 
    android:id="@id/spinner" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    style="?android:attr/progressBarStyle" 
    android:layout_centerInParent="true" /> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/smartzonecategorylist" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
</FrameLayout> 

但是清洗/重建和喜歡我不斷收到此logcat的錯誤後:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference 
                     at com.kidmixapp.commoncode.fragments.SmartzoneCategoryFragment.onCreateView(SmartzoneCategoryFragment.java:125) 

其中提到這條線:

mRecyclerView.setLayoutManager(mStaggeredLayoutManager); 

我也用調試線做了一些評估,看看有什麼root.findViewById(R.id.smartzonecategorylist);返回,它是空的?

我不確定是什麼原因造成的。對類似問題的其他搜索沒有找到我能找到的修復。

FIX:

它出現改變XML佈局指@id/android:list和改變findViewByIdfindViewById(android.R.id.list);像這樣固定它。我不知道爲什麼。

新的XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_weight="1"> 

<ProgressBar 
    android:id="@id/spinner" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true"/> 

<android.support.v7.widget.RecyclerView 
    android:id="@id/android:list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

</RelativeLayout> 

新的片斷ONCREATEVIEW

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View root = inflater.inflate(R.layout.fragment_smartzone_category, container, false); 

    final Context context = getActivity(); 

    mPresenter = new SmartzonePresenter(context, this); 
    // 
    mProgressBar = (ProgressBar) root.findViewById(R.id.spinner); 
    mProgressBar.setVisibility(View.VISIBLE); 

    // set list 
    mStaggeredLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL); 
    mStaggeredLayoutManager.setSpanCount(3); 

    mRecyclerView = (RecyclerView) root.findViewById(android.R.id.list); 
    mRecyclerView.setLayoutManager(mStaggeredLayoutManager); 
    mRecyclerView.setVisibility(View.INVISIBLE);; 

    // display list 
    mListAdapter = new VideoCategoryAdapter(context, mCategories); 
    mRecyclerView.setAdapter(mListAdapter); 
    mListAdapter.setOnItemClickListener(new VideoCategoryAdapter.OnItemClickListener() { 
     @Override 
     public void onItemClick(View view, int position) { 
      // get the category 
      VideoCategory vc = mListAdapter.getItem(position); 
      // display the sub-smartzone category page 
      mCallback.onSelectCategory(vc, mCategories); 
     } 
    }); 

    // 
    retrieveVideoList(); 

    return root; 
} 
+0

是您的'smartzonecategorylist'屬於'fragment_smartzone_category'佈局 – Mohit

+0

是的,它屬於該佈局。 – Tukajo

+0

嘗試清理和重建您的項目 –

回答

-1

在您的佈局XML,你的代碼是:

<ProgressBar 
android:id="@id/spinner" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
style="?android:attr/progressBarStyle" 
android:layout_centerInParent="true" /> 

但我認爲代碼應該是:

<ProgressBar 
android:id="@+id/spinner" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
style="?android:attr/progressBarStyle" 
android:layout_centerInParent="true" /> 
+0

我會改變這一點,但是這會導致問題,因爲'RecyclerView'正在拋出'null'? – Tukajo

+0

兩者有什麼區別? –

+0

當我做了這個改變;我現在在findViewById(R.id.spinner);和我的其他錯誤。它似乎改變它爲「@ + ID」導致null,任何想法爲什麼? – Tukajo