CollapsingToolbarLayout cannot be cast to android.support.v7.widget.Toolbar

坚强是说给别人听的谎言 提交于 2021-02-16 19:03:11

问题


Working on this Collapsing Toolbar layout everything works fine but the moment I add code in the java class Main activity, error shows up and app crashes. Without java code it was working properly.

XML:

     <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="app.com.alphaapps.android.cordinator.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true"
            >

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="350dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleTextAppearance="@android:color/transparent"
            android:fitsSystemWindows="true"
            >

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="350dp" 
                app:layout_collapseMode="parallax"
                android:src="@drawable/firework"
                android:contentDescription="@string/app_name"
                android:scaleType="centerCrop"

                />

            <android.support.v7.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                android:title="@string/recent_news"
                app:layout_collapseMode="pin" />


        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"

        >

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            app:cardElevation="5dp"
            app:cardUseCompatPadding="true"
            >



            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="12dp"
                    android:textSize="20sp"
                    android:layout_marginTop="12dp"
                    android:textStyle="bold"
                    android:textColor="@color/colorPrimary"
                    android:text="@string/recent_news"
                    />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:padding="12dp"
                    >

                    <ImageView
                        android:contentDescription="@string/app_name"
                        android:src="@drawable/ic_clock"
                        android:layout_weight="1"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content" />

                    <TextView
                        android:layout_gravity="center_vertical"
                        android:layout_width="0dp"
                        android:layout_weight="9"
                        android:layout_height="wrap_content"
                        android:padding="12dp"
                        android:textSize="14sp"
                        android:layout_marginTop="12dp"
                        android:textStyle="bold"
                        android:textColor="@color/colorPrimary"
                        android:text="@string/publish_date"
                        />


                </LinearLayout>


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="12dp"
                    android:textSize="13sp"
                    android:lineSpacingMultiplier="1.5"
                    android:layout_marginTop="12dp"

                    android:text="@string/in_news"
                    />



            </LinearLayout>

        </android.support.v7.widget.CardView>

    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_fav"
        android:elevation="6dp"
        app:pressedTranslationZ="12dp"
        app:layout_anchor="@id/app_bar_layout"
        app:layout_anchorGravity="bottom|right|end"

        />

</android.support.design.widget.CoordinatorLayout>

MAINACTIVITY

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar()!=null)
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

LOGCAT

java.lang.RuntimeException: Unable to start activity
ComponentInfo{app.com.alphaapps.android.cordinator/app.com.alphaapps.android.
cordinator.MainActivity}: java.lang.ClassCastException:
 android.support.design.widget.CollapsingToolbarLayout cannot be cast to
android.support.v7.widget.Toolbar

                                                                                                                Caused by: java.lang.ClassCastException: android.support.design.widget.CollapsingToolbarLayout cannot be cast to android.support.v7.widget.Toolbar
                                                                                                  at app.com.alphaapps.android.cordinator.MainActivity.onCreate(MainActivity.java:14)

回答1:


Remove

 android:id="@+id/toolbar"

from collapsing bar layout and put it in toolbar

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            android:title="@string/recent_news"
            app:layout_collapseMode="pin" />


来源:https://stackoverflow.com/questions/44220588/collapsingtoolbarlayout-cannot-be-cast-to-android-support-v7-widget-toolbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!