setSupportActionBar toolbar cannot be applied to (

2019-01-22 17:06发布

I've been looking for an answer and I've tried many possible solutions, but nothing seems to work..

I'm trying to setup a Material Action Bar following this tutorial.

Here's my code:

tool_bar.xml:

<?xml version="1.0" encoding="utf-8"?>
<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/ColorPrimary"
    android:elevation="4dp">

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

activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">
    <!-- The main content view -->
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/app_bar"
            layout="@layout/tool_bar" />
    </RelativeLayout>
    <!-- Navigation Drawer -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="220dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="#1C1C1C"
        android:divider="@android:color/darker_gray"
        android:dividerHeight="1dp" />

</android.support.v4.widget.DrawerLayout>

And finally my activity.java:

import android.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toolbar;


public class rutaActivity extends ActionBarActivity {

private Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ruta);

        getSupportActionBar().hide();//Ocultar ActivityBar anterior

        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar); //HERE'S THE PROBLEM !!!!

Error:

setSupporActionBar (android.support.v7.widget.Toolbar) in ActionBarActivity cannot be applied to (android.widget.Toolbar)

How can I fix this?

11条回答
欢心
2楼-- · 2019-01-22 17:33

I was using previously this code:

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

and extended AppCompatActivity also but I was getting the same error. So in place of using Toolbar class, I have imported the below class and it worked for me:

android.support.v7.widget.Toolbar
查看更多
我只想做你的唯一
3楼-- · 2019-01-22 17:33

For android version above 3 :

import androidx.appcompat.widget.Toolbar;

For android version bellow 3 :

import android.widget.Toolbar;

查看更多
Evening l夕情丶
4楼-- · 2019-01-22 17:35

In using toolbar you should extends AppCompatActivity and then import android.support.v7.widget.Toolbar

查看更多
家丑人穷心不美
5楼-- · 2019-01-22 17:42

I'm having the same problem. My Logcat says

java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor

I have already an action bar so when i use setSupportActionBar(toolbar); my app gets an error for this conflict. To fix this problem add NoActionBar in your app theme style resource file.

Change this

<style name="ParentMaterialTheme" parent="Theme.AppCompat.Light">

To this

<style name="ParentMaterialTheme" parent="Theme.AppCompat.Light.NoActionBar">

查看更多
再贱就再见
6楼-- · 2019-01-22 17:46

For adding a ToolBar that supports Material Design, the official documentation directions are probably the best to follow.

  1. Add the v7 appcompat support library.
  2. Make your activity extend AppCompatActivity.

    public class MyActivity extends AppCompatActivity {
      // ...
    }
    
  3. Declare NoActionBar in the Manifest.

    <application
        android:theme="@style/Theme.AppCompat.Light.NoActionBar"
        />
    
  4. Add a toolbar to your activity's xml layout.

    <android.support.v7.widget.Toolbar
       android:id="@+id/my_toolbar"
       android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
       ...
       />
    
  5. Call setSupportActionBar in the activity's onCreate.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);
    }
    

Note: You will have to import the following in the activity.

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
查看更多
来,给爷笑一个
7楼-- · 2019-01-22 17:46

Adding import android.support.v7.widget.Toolbar to the import list resolve this issue.

Then add the toolbar widget layout file:

<android.support.v7.widget.Toolbar
    android:id="@+id/list_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="?attr/actionBarTheme"
    />

In onCreate method of java code

//call to

Tootbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar);

Source: https://developer.android.com/training/appbar/up-action

查看更多
登录 后发表回答