Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法将android.widget.Toolbar转换为android.support.v7.widget.Toolbar_Android_Android Layout - Fatal编程技术网

无法将android.widget.Toolbar转换为android.support.v7.widget.Toolbar

无法将android.widget.Toolbar转换为android.support.v7.widget.Toolbar,android,android-layout,Android,Android Layout,我有一个toolbar.xml: <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/color_primary" android:elevation="4d

我有一个toolbar.xml:

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_primary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark" />
但是,我在运行应用程序时遇到以下错误:

Error:(26, 29) error: incompatible types: android.widget.Toolbar cannot be converted to android.support.v7.widget.Toolbar
此外,当我在编辑器中查看布局时,我会收到以下消息:

Missing styles. Is the correct theme chosen for this layout?
Use the theme combo box above the layout to choose a different layout, or to fix the theme style references.
Failed to find style 'toolbarStyle' in current layout.
和my styles.xml:

<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/color_primary</item>
    <item name="colorPrimaryDark">@color/color_primaryDark</item>
    <item name="colorAccent">@color/color_accent</item>
    <item name="colorControlNormal">@color/color_accent</item>
</style>

@彩色/彩色原色
@颜色/原色暗
@颜色/颜色口音
@颜色/颜色口音


我看不出我错在哪里。在编辑器和我的清单文件中选择了正确的主题。我们将一如既往地感谢您的帮助

您在XML中使用的是
android.support.v7.widget.Toolbar
,但在java代码中导入的是另一种类型的
android.widget.Toolbar
。将导入更改为android.support.v7.widget.Toolbar,它应该可以工作。

如果您的项目已经迁移到AndroidX,则导入此
android.support.v7.widget.Toolbar toolbar;


toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
导入androidx.appcompat.widget.Toolbar


否则通过重构迁移到AndroidX迁移到AndroidX XML和Java的完整代码示例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".Events.Events_Main_Activity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbarlayout_id"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsingtoolbar_id"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="8dp"
            app:expandedTitleMarginStart="8dp"
            app:layout_scrollFlags="exitUntilCollapsed|scroll"
            app:title="Anime Title"
            app:titleEnabled="true">
            <ImageView
                android:id="@+id/aa_thumbnail"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/bg_gradient" />
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar_sec"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:title="shshhs" />
        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>



package com.uafappdevelopers.example;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
//note this import
import androidx.appcompat.widget.Toolbar;
import com.uafappdevelopers.uafportal.R;
public class Events_Second_Activity extends AppCompatActivity {
//add this for the navigation back button click event
@Override
public boolean onSupportNavigateUp() {
    onBackPressed();
    finish();
    return super.onSupportNavigateUp();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_event_second);
    //change id to Your id
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_sec);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    setTitle("University of Agriculture Faisalabad");
}
}

包com.uafappdevelopers.example;
导入androidx.appcompat.app.appcompat活动;
导入android.os.Bundle;
//注意这一点
导入androidx.appcompat.widget.Toolbar;
导入com.uafappdevelopers.uafportal.R;
公共类事件\u第二个\u活动扩展了AppCompatActivity{
//为“导航后退”按钮单击事件添加此选项
@凌驾
公共布尔onSupportNavigateUp(){
onBackPressed();
完成();
返回super.onSupportNavigateUp();
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u event\u second);
//把身份证换成你的身份证
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar\u sec);
设置支持操作栏(工具栏);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
setTitle(“费萨拉巴德农业大学”);
}
}

这适用于最新的android版本:

Toolbar toolbar = findViewById(R.id.toolbarflwrs);
setSupportActionBar(toolbar);
请注意,从以下位置导入工具栏:

androidx.appcompat.widget.Toolbar
发件人:

Toolbar Toolbar=findviewbyd(R.id.Toolbar); 设置支持操作栏(工具栏)

至:

androidx.appcompat.widget.Toolbar-Toolbar=findviewbyd(R.id.Toolbar);
设置支持操作栏(工具栏)

在活动的顶部必须有
import android.support.v7.widget.Toolbar
导入android.widget.Toolbar@Steve nvm Steve,我也这么做了,你似乎错过了代码块中的一行代码。你好,傀儡,欢迎来到StackOverflow。这与M.Noman Zaman的答案基本相同(之前的答案),请在发布答案之前仔细检查。此外,您应该将代码封装在代码块中,您可能需要查看一下。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".Events.Events_Main_Activity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbarlayout_id"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsingtoolbar_id"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="8dp"
            app:expandedTitleMarginStart="8dp"
            app:layout_scrollFlags="exitUntilCollapsed|scroll"
            app:title="Anime Title"
            app:titleEnabled="true">
            <ImageView
                android:id="@+id/aa_thumbnail"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/bg_gradient" />
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar_sec"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:title="shshhs" />
        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>



package com.uafappdevelopers.example;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
//note this import
import androidx.appcompat.widget.Toolbar;
import com.uafappdevelopers.uafportal.R;
public class Events_Second_Activity extends AppCompatActivity {
//add this for the navigation back button click event
@Override
public boolean onSupportNavigateUp() {
    onBackPressed();
    finish();
    return super.onSupportNavigateUp();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_event_second);
    //change id to Your id
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_sec);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    setTitle("University of Agriculture Faisalabad");
}
}
Toolbar toolbar = findViewById(R.id.toolbarflwrs);
setSupportActionBar(toolbar);