java 相对布局不能转换为 android.support.v7.widget.Toolbar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29564313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:30:36  来源:igfitidea点击:

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

javaandroidlayout

提问by Mohammad

I want to add shadow to ToolBar and I use the following link: ToolBar Shadow

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

but running app show me this error in LogCat :

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

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

Toolbar code :

工具栏代码:

<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 :

主要活动 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 :

主活动 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);
}

please help me

请帮我

采纳答案by Vikalp

You are going to need to make one more xml file which contains your toolbar: Name of this xml is toolbar

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

<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"/>

Now change your relative layout's includetag like this

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

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

And in your activity, do something like this:

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

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

回答by Leon Joosse

Where you use the <include />in your MainActivity XML, you should not put the android:id="@+id/app_bar", but instead in the tag <android.support.v7.widget.Toolbar />.

<include />MainActivity XML 中使用 的地方,不应将android:id="@+id/app_bar",放在标记中<android.support.v7.widget.Toolbar />

回答by Aman Srii

I have removed relativeLayoutinstead just use android.support.v7.widget.Toolbar. And ts work.

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

回答by Vishnu

Try downloading android_m2repository_r29.ziprepository from https://developer.xamarin.com/guides/android/troubleshooting/resolving-library-installation-errors/with instruction given in same.

尝试android_m2repository_r29.ziphttps://developer.xamarin.com/guides/android/troubleshooting/resolving-library-installation-errors/下载存储库,并提供相同的说明。