web-dev-qa-db-fra.com

L'utilisation de CardView et RecyclerView dans mes fichiers de mise en page génère une exception.

Je me suis donc lancé dans la conception matérielle d'Android Preview L. J'ai importé les bibliothèques CardView et RecyclerView.

J'utilise l'aperçu de la version 0.8.0 d'Android Studio. Avoir les derniers packages SDK installés.

Une fois que je les utilise dans mes fichiers de mise en page, l'aperçu génère une exception pour les deux.

Voici l'exception pour la CardView:

Java.lang.ClassFormatError: Illegal field name "CardView.Dark" in class Android/support/v7/cardview/R$style
at Java.lang.ClassLoader.defineClass1(Native Method)
at Java.lang.ClassLoader.defineClass(ClassLoader.Java:792)
at Java.lang.ClassLoader.defineClass(ClassLoader.Java:635)
at org.jetbrains.Android.uipreview.ProjectClassLoader.findClass(ProjectClassLoader.Java:63)
at Java.lang.ClassLoader.loadClass(ClassLoader.Java:424)
at Java.lang.ClassLoader.loadClass(ClassLoader.Java:411)
at Java.lang.ClassLoader.loadClass(ClassLoader.Java:357)
at Android.support.v7.widget.CardView.initialize(CardView.Java:69)
at Android.support.v7.widget.CardView.<init>(CardView.Java:60)
at Sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at Sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.Java:57)
at Sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.Java:45)
at Java.lang.reflect.Constructor.newInstance(Constructor.Java:526)
at org.jetbrains.Android.uipreview.ViewLoader.createNewInstance(ViewLoader.Java:375)
at org.jetbrains.Android.uipreview.ViewLoader.loadView(ViewLoader.Java:100)
at com.Android.tools.idea.rendering.LayoutlibCallback.loadView(LayoutlibCallback.Java:172)
at Android.view.BridgeInflater.loadCustomView(BridgeInflater.Java:207)
at Android.view.BridgeInflater.createViewFromTag(BridgeInflater.Java:132)
at Android.view.LayoutInflater.inflate(LayoutInflater.Java:478)
at Android.view.LayoutInflater.inflate(LayoutInflater.Java:381)
at com.Android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.Java:395)
at com.Android.layoutlib.bridge.Bridge.createSession(Bridge.Java:329)
at com.Android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.Java:332)
at com.Android.tools.idea.rendering.RenderService$3.compute(RenderService.Java:575)
at com.Android.tools.idea.rendering.RenderService$3.compute(RenderService.Java:564)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.Java:932)
at com.Android.tools.idea.rendering.RenderService.createRenderSession(RenderService.Java:564)
at com.Android.tools.idea.rendering.RenderService.render(RenderService.Java:691)
at org.jetbrains.Android.uipreview.AndroidLayoutPreviewToolWindowManager.doRender(AndroidLayoutPreviewToolWindowManager.Java:586)
at org.jetbrains.Android.uipreview.AndroidLayoutPreviewToolWindowManager.access$1900(AndroidLayoutPreviewToolWindowManager.Java:80)
at org.jetbrains.Android.uipreview.AndroidLayoutPreviewToolWindowManager$6$1.run(AndroidLayoutPreviewToolWindowManager.Java:528)
at com.intellij.openapi.progress.impl.ProgressManagerImpl$2.run(ProgressManagerImpl.Java:178)
at com.intellij.openapi.progress.ProgressManager.executeProcessUnderProgress(ProgressManager.Java:209)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.Java:212)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.Java:171)
at org.jetbrains.Android.uipreview.AndroidLayoutPreviewToolWindowManager$6.run(AndroidLayoutPreviewToolWindowManager.Java:523)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.Java:320)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.Java:310)
at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.Java:254)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.Java:269)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.Java:227)
at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.Java:217)
at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.Java:238)
at com.intellij.util.Alarm$Request$1.run(Alarm.Java:327)
at Java.util.concurrent.Executors$RunnableAdapter.call(Executors.Java:471)
at Java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.Java:334)
at Java.util.concurrent.FutureTask.run(FutureTask.Java:166)
at Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1145)
at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:615)
at Java.lang.Thread.run(Thread.Java:724)

Et voici l'exception pour RecyclerView:

Java.lang.NullPointerException
at Android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.Java:1310)
at Android.view.View.measure(View.Java:16987)
at Android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.Java:722)
at Android.widget.RelativeLayout.onMeasure(RelativeLayout.Java:458)
at Android.view.View.measure(View.Java:16987)
at Android.view.ViewGroup.measureChildWithMargins(ViewGroup.Java:5257)
at Android.widget.FrameLayout.onMeasure(FrameLayout.Java:314)
at Android.view.View.measure(View.Java:16987)
at Android.view.ViewGroup.measureChildWithMargins(ViewGroup.Java:5257)
at com.Android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.Java:447)
at Android.view.View.measure(View.Java:16987)
at Android.view.ViewGroup.measureChildWithMargins(ViewGroup.Java:5257)
at Android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.Java:1436)
at Android.widget.LinearLayout.measureHorizontal(LinearLayout.Java:1083)
at Android.widget.LinearLayout.onMeasure(LinearLayout.Java:615)
at Android.view.View.measure(View.Java:16987)
at Android.view.ViewGroup.measureChildWithMargins(ViewGroup.Java:5257)
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:16987)
at Android.widget.LinearLayout.measureVertical(LinearLayout.Java:875)
at Android.widget.LinearLayout.onMeasure(LinearLayout.Java:613)
at Android.view.View.measure(View.Java:16987)
at com.Android.layoutlib.bridge.impl.RenderSessionImpl.measureView(RenderSessionImpl.Java:621)
at com.Android.layoutlib.bridge.impl.RenderSessionImpl.render(RenderSessionImpl.Java:521)
at com.Android.layoutlib.bridge.Bridge.createSession(Bridge.Java:331)
at com.Android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.Java:332)
at com.Android.tools.idea.rendering.RenderService$3.compute(RenderService.Java:575)
at com.Android.tools.idea.rendering.RenderService$3.compute(RenderService.Java:564)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.Java:932)
at com.Android.tools.idea.rendering.RenderService.createRenderSession(RenderService.Java:564)
at com.Android.tools.idea.rendering.RenderService.render(RenderService.Java:691)
at org.jetbrains.Android.uipreview.AndroidLayoutPreviewToolWindowManager.doRender(AndroidLayoutPreviewToolWindowManager.Java:586)
at org.jetbrains.Android.uipreview.AndroidLayoutPreviewToolWindowManager.access$1900(AndroidLayoutPreviewToolWindowManager.Java:80)
at org.jetbrains.Android.uipreview.AndroidLayoutPreviewToolWindowManager$6$1.run(AndroidLayoutPreviewToolWindowManager.Java:528)
at com.intellij.openapi.progress.impl.ProgressManagerImpl$2.run(ProgressManagerImpl.Java:178)
at com.intellij.openapi.progress.ProgressManager.executeProcessUnderProgress(ProgressManager.Java:209)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.Java:212)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.Java:171)
at org.jetbrains.Android.uipreview.AndroidLayoutPreviewToolWindowManager$6.run(AndroidLayoutPreviewToolWindowManager.Java:523)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.Java:320)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.Java:310)
at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.Java:254)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.Java:269)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.Java:227)
at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.Java:217)
at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.Java:238)
at com.intellij.util.Alarm$Request$1.run(Alarm.Java:327)
at Java.util.concurrent.Executors$RunnableAdapter.call(Executors.Java:471)
at Java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.Java:334)
at Java.util.concurrent.FutureTask.run(FutureTask.Java:166)
at Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1145)
at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:615)
at Java.lang.Thread.run(Thread.Java:724)

Cela rend la configuration du bâtiment presque impossible en utilisant ces deux (étonnantes) Views, ce qui m'intéresse le plus.

Existe-t-il déjà une solution à ce problème, ou s'agit-il vraiment d'un comportement attendu, vu qu'ils sont "personnalisés" Views, dans un sens?

12
tolgap

Ceci est un bug dans Android Studio. L'outil de prévisualisation ne sait pas comment initialiser les widgets. Je crois que oui, car la même exception est levée de votre application si vous oubliez d'appeler recyclerView.setLayoutManager() dans votre code.

19
user7610

J'utilise Eclipse et suis confronté au même problème. Comme suggéré par user7610, vous devez appeler recyclerView.setLayoutManager() pour résoudre ce problème. 

Voici comment je l'ai résolu .. 

Créer une variable membre .. 

RecyclerView.LayoutManager mLayoutManager;

En onCreate() ou onCreateView() 

recyclerView = (RecyclerView) view
                .findViewById(R.id.business_recycler_view);
recyclerView.setHasFixedSize(true);

mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);

Cela a résolu ma NullPointerException.

8
Vamsi Challa

J'ai trouvé un problème similaire. En tant que tel, si nous gonflons uniquement la mise en page qui consiste en RecyclerView, alors que setcontentView est généré, le gestionnaire de mise en page n'est pas défini pour le fichier Recyclerview Defined in Layout.

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.searchResultsList);
LinearLayoutManager layoutmanager = new LinearLayoutManager(this);
layoutmanager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutmanager);
2
Ronak Poriya

Si quelqu'un fait face au même problème pour la vue Recycler dans fragment, utilisez ce code 

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

        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView

        mRecyclerView.setHasFixedSize(true);
        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);


        String[] abc = {"hi","how are you","this is recycler"};
        // specify an adapter (see also next example)
        mAdapter = new RecyclerViewAdapter(abc);
        mRecyclerView.setAdapter(mAdapter);
        return rootView;
    }
0
sanath_p