Je reçois une exception suivante pendant que j'essaie d'afficher la vue de recyclage:
Java.lang.NullPointerException: Attempt to invoke virtual method 'boolean Android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()' on a null object reference
at Android.support.v7.widget.RecyclerView.computeVerticalScrollRange(RecyclerView.Java:1216)
at Android.view.View.onDrawScrollBars(View.Java:13900)
at Android.view.View.draw(View.Java:16275)
at Android.support.v7.widget.RecyclerView.draw(RecyclerView.Java:2440)
at Android.view.View.updateDisplayListIfDirty(View.Java:15192)
at Android.view.View.getDisplayList(View.Java:15215)
at Android.view.View.draw(View.Java:15990)
at Android.view.ViewGroup.drawChild(ViewGroup.Java:3708)
at Android.view.ViewGroup.dispatchDraw(ViewGroup.Java:3502)
at Android.view.View.draw(View.Java:16272)
at Android.view.View.updateDisplayListIfDirty(View.Java:15192)
at Android.view.View.getDisplayList(View.Java:15215)
at Android.view.ViewGroup.recreateChildDisplayList(ViewGroup.Java:3692)
at Android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.Java:3671)
at Android.view.View.updateDisplayListIfDirty(View.Java:15152)
at Android.view.View.getDisplayList(View.Java:15215)
at Android.view.ViewGroup.recreateChildDisplayList(ViewGroup.Java:3692)
at Android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.Java:3671)
at Android.view.View.updateDisplayListIfDirty(View.Java:15152)
at Android.view.View.getDisplayList(View.Java:15215)
at Android.view.ViewGroup.recreateChildDisplayList(ViewGroup.Java:362)
at Android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.Java:3671)
at Android.view.View.updateDisplayListIfDirty(View.Java:15152)
at Android.view.View.getDisplayList(View.Java:15215)
at Android.view.ViewGroup.recreateChildDisplayList(ViewGroup.Java:3692)
at Android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.Java:3671)
at Android.view.View.updateDisplayListIfDirty(View.Java:15152)
at Android.view.View.getDisplayList(View.Java:15215)
at Android.view.ViewGroup.recreateChildDisplayList(ViewGroup.Java:3692)
at Android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.Java:3671)
at Android.view.View.updateDisplayListIfDirty(View.Java:15152)
at Android.view.View.getDisplayList(View.Java:15215)
at Android.view.ViewGroup.recreateChildDisplayList(ViewGroup.Java:3692)
at Android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.Java:3671)
at Android.view.View.updateDisplayListIfDirty(View.Java:15152)
at Android.view.View.getDisplayList(View.Java:15215)
at Android.view.ViewGroup.recreateChildDisplayList(ViewGroup.Java:3692)
at Android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.Java:3671)
at Android.view.View.updateDisplayListIfDirty(View.Java:15152)
at Android.view.View.getDisplayList(View.Java:15215)
at Android.view.ViewGroup.recreateChildDisplayList(ViewGroup.Java:3692)
at Android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.Java:3671)
at Android.view.View.updateDisplayListIfDirty(View.Java:15152)
at Android.view.View.getDisplayList(View.Java:15215)
at Android.view.ViewGroup.recreateChildDisplayList(ViewGroup.Java:3692)
at Android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.Java:3671)
at Android.view.View.updateDisplayListIfDirty(View.Java:15152)
at Android.view.View.getDisplayList(View.Java:15215)
at Android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.Java:307)
at Android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.Java:313)
at Android.view.ThreadedRenderer.draw(ThreadedRenderer.Java:352)
at Android.view.ViewRootImpl.draw(ViewRootImpl.Java:2811)
at Android.view.ViewRootImpl.performDraw(ViewRootImpl.Java:2634)
at Android.view.ViewRootImpl.performTraversals(ViewRootImpl.Java:2222)
at Android.view.ViewRootImpl.doTraversal(ViewRootImpl.Java:1230)
at Android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.Java:6726)
at Android.view.Choreographer$CallbackRecord.run(Choreographer.Java:777)
at Android.view.Choreographer.doCallbacks(Choreographer.Java:590)
at Android.view.Choreograph
et mon xml est ceci:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
style="@style/global_screen_linear_layout"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:orientation="vertical" >
<LinearLayout
Android:id="@+id/lilAdvancedSearchParent"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:background="@color/gray_general"
Android:gravity="center_vertical"
Android:orientation="horizontal"
Android:padding="10dp" >
<LinearLayout
Android:id="@+id/lilAdvancedSearch"
Android:layout_width="0dp"
Android:layout_height="match_parent"
Android:layout_weight="6"
Android:gravity="center_vertical" >
<ImageView
Android:id="@+id/imageView2"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:src="@drawable/arrow_white" />
<TextView
Android:id="@+id/txvAvencedSearch"
Android:layout_width="0dp"
Android:layout_height="wrap_content"
Android:layout_marginRight="10dp"
Android:layout_weight="4.5"
Android:text="@string/global_fragment_advenced_search"
Android:textColor="@color/white"
Android:textSize="@dimen/global_screen_layout_text_view_advenced_search"
Android:textStyle="bold" />
</LinearLayout>
<EditText
Android:id="@+id/edtSearch"
Android:layout_width="0dp"
Android:layout_height="wrap_content"
Android:layout_weight="9"
Android:drawableLeft="@drawable/navigation_search"
Android:ems="10"
Android:imeOptions="actionSearch"
Android:inputType="text"
Android:textColor="@color/white"
Android:textColorHint="@color/white"
Android:textSize="15sp" >
<requestFocus />
</EditText>
</LinearLayout>
<Android.support.v7.widget.RecyclerView
Android:id="@+id/my_recycler_view"
Android:layout_width="match_parent"
Android:layout_height="800dp"
Android:scrollbars="vertical" >
</Android.support.v7.widget.RecyclerView>
</LinearLayout>
et mon code Java est le suivant:
public class TendersFragment extends BaseFragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.global_screen_layout, container, false);
initUI(v);
initListeners();
setEditSearchHint(getString(R.string.tenders_fragment_search_tenders));
setAdvancedSearchBackground(getResources().getColor(R.color.green_general));
return v;
}
@Override
protected void initUI(View view) {
lsvTendersJobsMessages = (ListView) view.findViewById(R.id.lsvGlobal);
mRecyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
setAdapterForListview(lsvTendersJobsMessages);
edtSearch = (EditText) view.findViewById(R.id.edtSearch);
txvAdvencedSearch = (TextView) view.findViewById(R.id.txvAvencedSearch);
lilAdvancedSearch = (LinearLayout) view.findViewById(R.id.lilAdvancedSearch);
lilAdvancedSearchParent = (LinearLayout) view.findViewById(R.id.lilAdvancedSearchParent);
}
@Override
protected void setAdapterForListview(ListView lsv) {
mRecyclerView.setHasFixedSize(true);
mLinearLayoutManager = new LinearLayoutManager(getActivity());
mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
ArrayList<ItemType> arlItems = new ArrayList<ItemType>();
arlItems.add(ItemType.MAIN_ITEM);
arlItems.add(ItemType.MAIN_ITEM);
arlItems.add(ItemType.MAIN_ITEM);
arlItems.add(ItemType.MAIN_ITEM);
arlItems.add(ItemType.MAIN_ITEM);
arlItems.add(ItemType.MAIN_ITEM);
arlItems.add(ItemType.MAIN_ITEM);
arlItems.add(ItemType.MAIN_ITEM);
arlItems.add(ItemType.MAIN_ITEM);
// specify an adapter (see also next example)
mainItemAdapter = new MainItemAdapter(arlItems, getActivity());
mRecyclerView.setAdapter(mainItemAdapter);
}
@Override
protected void onServiceConnected() {
// TODO Auto-generated method stub
}
protected void initListeners() {
final AdvancedSearchFragment advancedSearchFragment = new AdvancedSearchFragment();
Bundle bundle = new Bundle();
bundle.putInt(FragmentConst.BUNDLE_KEY_INT_WHICH_FRAGMENT,FragmentConst.TENDERS_ID);
advancedSearchFragment.setArguments(bundle);
lilAdvancedSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getActivity().getSupportFragmentManager().beginTransaction()
.add(Android.R.id.content,advancedSearchFragment).addToBackStack(null)
.commit();
}
});
}
}
je tente également de définir la hauteur de mise en page de la vue de recyclage comme match_parent et wrap_contant, mais j'obtiens toujours la même erreur.
Voici comment j'ai finalement résolu ce problème.
J'ai supprimé Android:scrollbars="vertical"
de la déclaration RecyclerView:
<Android.support.v7.widget.RecyclerView
Android:id="@+id/task_recycler_view"
Android:scrollbars="vertical"
Android:layout_width="match_parent"
Android:layout_height="match_parent"/>
Supprimez cette ligne du fichier XML:
Android: barres de défilement = "vertical"
Je pense que vous devez initialiser RecyclerView
en code Java lier cette
//1. get a reference to recyclerView
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
//2. set layoutManger
recyclerView.setLayoutManager(new LinearLayoutManager(this));
// 3. create an adapter
MyAdapter mAdapter = new MyAdapter(itemsData);
// 4. set adapter
recyclerView.setAdapter(mAdapter);
Déclarez-vous recyclerView dans n’importe quel fichier d’agencement qui n’a pas été traité dans le contrôleur Java (Activity/Fragment)?
Je déclare deux schémas de recycleur en deux fragments, je traite le 1er recycleur dans mes codes Java, je n’ai pas traité le second. Après avoir supprimé la déclaration recyclerView du fichier fragment2-layout-file, le problème du pointeur null a été résolu.
Remplacer
mLinearLayoutManager = new LinearLayoutManager(getActivity());
mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
avec
final LinearLayoutManager mLayoutManager= new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, true);
mRecyclerView.setLayoutManager(mLayoutManager);