android.widget.Toolbar cannot be converted to android.support.v7.widget.Toolbar

前端 未结 4 2084
青春惊慌失措
青春惊慌失措 2020-12-09 02:10

I have a toolbar.xml:



        
相关标签:
4条回答
  • 2020-12-09 02:45

    if your project is already migrated to AndroidX then import this
    import androidx.appcompat.widget.Toolbar;

    else migrate to AndroidX by
    Refactor>Migrate to AndroidX Full code Example of XML and 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");
    }
    }
    
    0 讨论(0)
  • 2020-12-09 02:49
    android.support.v7.widget.Toolbar toolbar;
    
    
    toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    
    0 讨论(0)
  • 2020-12-09 02:50

    this is working for the latest android versions:

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

    Notice, import Toolbar from:

    androidx.appcompat.widget.Toolbar

    0 讨论(0)
  • 2020-12-09 03:00

    You use android.support.v7.widget.Toolbar in your XML, but in your java code you are importing android.widget.Toolbar which is a different type. Change your import to android.support.v7.widget.Toolbar and it should work.

    0 讨论(0)
提交回复
热议问题