gpt4 book ai didi

android - CollapsingToolbarLayout 无法转换为 android.support.v7.widget.Toolbar

转载 作者:太空狗 更新时间:2023-10-29 13:08:31 24 4
gpt4 key购买 nike

在这个折叠工具栏布局上工作一切正常,但是当我在 java 类 Main Activity 中添加代码时,出现错误并且应用程序崩溃。没有 java 代码,它可以正常工作。

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>

主要 Activity

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)

最佳答案

删除

 android:id="@+id/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" />

关于android - CollapsingToolbarLayout 无法转换为 android.support.v7.widget.Toolbar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44220588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com