1

處理此摺疊工具欄佈局一切工作正常,但我在Java類中添加代碼的時刻主要活動,錯誤顯示和應用程序崩潰。沒有Java代碼,它工作正常。CollapsingToolbarLayout無法轉換爲android.support.v7.widget.Toolbar

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) 
+0

你可以發佈完整的XML代碼 – jagapathi

回答

2

刪除

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" /> 
相關問題