【问题标题】:RelativeLayout cannot be cast to android.support.v7.widget.Toolbar无法将 RelativeLayout 强制转换为 android.support.v7.widget.Toolbar
【发布时间】:2015-06-16 08:13:24
【问题描述】:

我想为 ToolBar 添加阴影,我使用以下链接: ToolBar Shadow

但正在运行的应用程序在 LogCat 中向我显示此错误:

java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.support.v7.widget.Toolbar

工具栏代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="200dp">

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
    app:theme="@style/MyCustomToolbarTheme"
    android:background="@drawable/main_header">

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

MainActivity XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<include
    android:id="@+id/app_bar"
    layout="@layout/app_bar_main"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/app_bar"
    android:text="ytkbhjk"/>

MainActivity Java:

public class MainActivity extends ActionBarActivity {

private Toolbar toolbar;

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

    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
}

请帮帮我

【问题讨论】:

    标签: java android layout


    【解决方案1】:

    尝试从https://developer.xamarin.com/guides/android/troubleshooting/resolving-library-installation-errors/ 下载android_m2repository_r29.zip 存储库,并使用相同的说明。

    【讨论】:

      【解决方案2】:

      我已删除 relativeLayout,而只使用 android.support.v7.widget.Toolbar。并且 ts 工作。

      【讨论】:

        【解决方案3】:

        您将需要再制作一个包含您的工具栏的 xml 文件: 这个xml的名字是toolbar

        <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"/>
        

        现在像这样改变你的相对布局的 include 标签

        <include
            layout="@layout/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        

        在你的活动中,做这样的事情:

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        

        【讨论】:

          【解决方案4】:

          在 MainActivity XML 中使用&lt;include /&gt; 时,不应将android:id="@+id/app_bar" 放在标签&lt;android.support.v7.widget.Toolbar /&gt; 中。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-09-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-08-26
            • 2017-04-26
            相关资源
            最近更新 更多