ClassCastException с CoordinatorLayout в проекте AndroidX - PullRequest
0 голосов
/ 30 мая 2019

Мой проект использует AndroidX и имеет CoordinatorLayout, AppbarLayout ... У меня есть зависимости, перечисленные в файле Gradle, но редактор макета выдает ошибку и продолжает говорить, что классы не могут быть найдены.Это вызывает проблему рендеринга, но проект компилируется, и приложение работает хорошо.

Gradle (приложение):

    dependencies {
//...
//...
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.google.android.material:material:1.1.0-alpha06'
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha05'
    implementation 'com.google.android.material:material:1.1.0-alpha06'
    implementation 'androidx.core:core:1.2.0-alpha01'
    implementation 'androidx.media:media:1.1.0-beta01'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.1.0-alpha06'
    implementation 'androidx.coordinatorlayout:coordinatorlayout:1.1.0-alpha01'
    implementation group: 'androidx.coordinatorlayout', name: 'coordinatorlayout', version: '1.0.0'
}

XML-файл макета:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl_parent_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorBackgroundGrey"
        ads:layout_constraintEnd_toEndOf="parent"
        ads:layout_constraintStart_toStartOf="parent"
        ads:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appBarlayout"
            android:layout_width="match_parent"
            android:layout_height="280dp"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/collapsingToolbarLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                ads:contentScrim="?attr/colorPrimary"
                ads:layout_scrollFlags="scroll|exitUntilCollapsed">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    ads:layout_collapseMode="parallax">

                    <com.balysv.materialripple.MaterialRippleLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        ads:mrl_rippleAlpha="0.2"
                        ads:mrl_rippleColor="@color/colorRipple"
                        ads:mrl_rippleHover="true"
                        ads:mrl_rippleOverlay="true">

                        <ImageView
                            android:id="@+id/imv_image"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:contentDescription="@string/text_image_view_descrip"
                            android:scaleType="centerCrop"
                            ads:layout_collapseMode="none"
                            ads:srcCompat="@drawable/ic_launcher_foreground" />

                    </com.balysv.materialripple.MaterialRippleLayout>


                </RelativeLayout>

                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="@color/demo_dark_transparent"
                    ads:layout_collapseMode="pin"
                    ads:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

            </com.google.android.material.appbar.CollapsingToolbarLayout>

        </com.google.android.material.appbar.AppBarLayout>

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:foreground="?android:attr/selectableItemBackground"
            ads:behavior_overlapTop="64dp"
            ads:cardCornerRadius="3dp"
            ads:cardElevation="3sp"
            ads:cardPreventCornerOverlap="false"
            ads:cardUseCompatPadding="true"
            app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">


            <androidx.core.widget.NestedScrollView
                android:id="@+id/nestedScrollView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="false">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <include
                        android:id="@+id/lyt_main_content"
                        layout="@layout/include_news_content"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentStart="true"
                        android:layout_alignParentTop="true"
                        android:visibility="visible" />

                    <ViewFlipper
                        android:id="@+id/view_flipper2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:background="#F44336">

                    </ViewFlipper>

                </RelativeLayout>

            </androidx.core.widget.NestedScrollView>

        </androidx.cardview.widget.CardView>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Пробовалпочти все, что я мог без успеха.

РЕДАКТИРОВАТЬ:

Класс Java (импорт):

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.app.ActivityCompat;
import androidx.core.app.ActivityOptionsCompat;
import androidx.core.content.ContextCompat;
import androidx.core.view.ViewCompat;
import androidx.core.widget.NestedScrollView;

enter image description here

Ответы [ 4 ]

0 голосов
/ 02 июня 2019

Конвертируйте все макеты в соответствующие макеты поддержки androix.https://developer.android.com/jetpack/androidx/migrate

0 голосов
/ 30 мая 2019

'Сделайте недействительным кеш и перезапустите вашу студию, надеюсь, это будет полезно для вас'

Нажмите File-> Invalidate cache / Restart.

«Надеюсь, вы получите ответ»

0 голосов
/ 30 мая 2019

Бросьте все три своих implementation 'com.google.android.material:material:1.1.0-alpha06'

0 голосов
/ 30 мая 2019

добавить следующие строки в gradle.properties

# Enable configure on demand.
org.gradle.configureondemand=true
android.useAndroidX=true
android.enableJetifier=true

Убедитесь, что следующие зависимости указаны правильно

dependencies {
  implementation fileTree(include: ['*.jar'], dir: 'libs')
  implementation 'androidx.appcompat:appcompat:1.0.2'
  implementation 'com.google.android.material:material:1.1.0-alpha06'
  implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta1'
  implementation 'androidx.legacy:legacy-support-v4:1.0.0'
}

Чистый проект и Перестройка проекта . Если проблема не устранена, попробуйте Invalidate Caches / Restart

...