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?
Ssnake707 8 / 8 / 11 Регистрация: 02.02.2015 Сообщений: 140 |
||||||||||||
1 |
||||||||||||
16.12.2015, 23:25. Показов 6279. Ответов 2 Метки toolbar (Все метки)
Всем привет. Код MainActivity.java Кликните здесь для просмотра всего текста
Код main.xml Кликните здесь для просмотра всего текста
Код menu_main.xml Кликните здесь для просмотра всего текста
Логи Кликните здесь для просмотра всего текста
12-17 02:20:55.471 32073-32073/com.ssnake.highday W/dalvikvm﹕ VFY: unable to find class referenced in signature (Landroid/view/SearchEvent
0 |
Programming Эксперт 94731 / 64177 / 26122 Регистрация: 12.04.2006 Сообщений: 116,782 |
16.12.2015, 23:25 |
Ответы с готовыми решениями: Ошибка с Toolbar Ошибка при смене toolbar Toolbar ToolBar 2 |
Mikalai 266 / 242 / 96 Регистрация: 11.01.2015 Сообщений: 673 |
||||
17.12.2015, 01:13 |
2 |
|||
Сообщение было отмечено Ssnake707 как решение РешениеSsnake707, в файле styles.xml замени тему от которой наследуется твоя тема. Например так:
1 |
8 / 8 / 11 Регистрация: 02.02.2015 Сообщений: 140 |
|
17.12.2015, 01:15 [ТС] |
3 |
Mikalai, Вот я глупец, спасибо тебе большое, ты не представляешь как ты мне помог
0 |
IT_Exp Эксперт 87844 / 49110 / 22898 Регистрация: 17.06.2006 Сообщений: 92,604 |
17.12.2015, 01:15 |
Помогаю со студенческими работами здесь ToolBar Toolbar Toolbar ToolBar Искать еще темы с ответами Или воспользуйтесь поиском по форуму: 3 |
In Android development, using the ToolBar control instead of the ActionBar control, you need to use the setSupportActionBar() method in the java code, as follows:
1 Toolbar toolbar = (Toolbar) this.findViewById(R.id.toolBar);
2 this.setSupportActionBar(toolbar);
There are two common types of errors:
1. Method parameter error
This kind of error is caused by the wrong class, put the following code
1 import android.widget.Toolbar;
Replace with the following code
1 import android.support.v7.widget.Toolbar;
2. Method name error
Need inheritanceActionBarActivity classorAppCompatActivity class。
== Because the ActionBarActivity class is out of date, it is recommended to inherit the AppCompatActivity class. ==
Note: If you inherit the AppCompatActivity class, you need to use the Theme.AppCompat.Light.NoActionBar theme, a chestnut
1 <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
2 <item name="android:windowNoTitle">true</item>
3 <item name="android:windowActionBar">false</item>
4 </style>
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?
This question is related to
android
android-actionbar
material-design
toolbar
In your Activity.java
import android.support.v7.widget.Toolbar
instead of android.widget.Toolbar
:
import android.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.widget.Toolbar;
public class rutaActivity extends AppCompactActivity {
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); //NO PROBLEM !!!!
Update:
If you are using androidx, replace
import android.support.v7.widget.Toolbar;
import android.support.v7.app.AppCompatActivity;
with newer imports
import androidx.appcompat.widget.Toolbar;
import androidx.appcompat.app.AppCompatActivity;
In Android development, to use the ToolBar control to replace the ActionBar control, you need to use the setSupportActionBar() method in the java code, as follows:
1 Toolbar toolbar = (Toolbar) this.findViewById(R.id.toolBar); 2 this.setSupportActionBar(toolbar);
There are two common types of errors:
1. Method parameter error
This kind of error is because the wrong class is imported, put the following code
1 import android.widget.Toolbar;
Replace with the following code
1 import android.support.v7.widget.Toolbar;
2. Method name error
Need to inherit ActionBarActivity class or AppCompatActivity class.
Because the ActionBarActivity class is obsolete, it is recommended to inherit the AppCompatActivity class.
Note: If you inherit the AppCompatActivity class, you need to use the Theme.AppCompat.Light.NoActionBar theme, for example
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowNoTitle">true</item> <item name="android:windowActionBar">false</item> </style>