简体   繁体   English

当我在项目中使用app-compact v7来使用工具栏时出错

[英]Error when I use app-compact v7 in my project for using tool bar

I am getting an error when I try to add a toolbar to my application using following code 尝试使用以下代码向我的应用程序添加工具栏时出现错误

@SuppressWarnings("deprecation")

public class MainActivity extends ActionBarActivity implements 
IChangeFragementListener {
private DrawerLayout mDrawerLayout;

private ListView mDrawerList;

public static final String TAG = "MainActivity";

ArrayList<String> mGroupItem = new ArrayList<String>();

private boolean mReInstance;
ImageView menu_iconImageView;
int flag=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setGroupData();
    Toolbar toolbar=(Toolbar)findViewById(R.id.tool);
    setSupportActionBar(toolbar);
    }

My toolbar.xml is 我的toolbar.xml是

<Toolbar 
android:id="@+id/tool"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#819FF7"
android:elevation="4dp"
   />

And the error shown is 而显示的错误是

android.support.v7.app.AppCompatDelegateImplV14 01-29 13:34:46.336: E/AndroidRuntime (16928): at android.support.v7.app.AppCompatDelegate.create(AppCompatDelega 01-29 13:34:46.336: E/AndroidRuntime (16928): at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:117) 01-29 13:34:46.336: E/AndroidRuntime (16928): at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:456) android.support.v7.app.AppCompatDelegateImplV14 01-29 13:34:46.336:E / AndroidRuntime(16928):at android.support.v7.app.AppCompatDelegate.create(AppCompatDelega 01-29 13:34:46.336:E / AndroidRuntime(16928):位于android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:117)01-29 13:34:46.336:E / AndroidRuntime(16928):位于android.support.v7.app.AppCompatActivity .getDelegate(AppCompatActivity.java:456)

<android.support.v7.widget.Toolbar 
    android:id="@+id/tool"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="#819FF7"
    android:elevation="4dp" />

your toolbar layout should be like this, as you are trying use support library v-7 您的工具栏布局应如下所示,因为您尝试使用支持库v-7

    <android.support.v7.widget.Toolbar
    android:id="@+id/my_custom_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay"/>

code would be something like this 代码将是这样的

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

this Toolbar is supposed to be imported from here 该工具栏应该从此处导入

import android.support.v7.widget.Toolbar,

Kindly pay attention ActionBarActivity has been deprecated use AppCompatActivity to subclass your activity 请注意, ActionBarActivity已被弃用,请使用AppCompatActivity您的活动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM