Android: required android.support.v7.app.ActionBar found android.app.ActionBar

匿名 (未验证) 提交于 2019-12-03 03:02:02

问题:

I am trying to use the getActionBar(); method and I receive this error: required android.support.v7.app.ActionBar found android.app.ActionBar . How can I correct this?

回答1:

This error is occurring because you are creating an object and passing it to a different class reference variable. Because getActionManager gives the object of android.app.ActionBar but you are trying to assign android.app.ActionBar class object to android.support.v7.app.ActionBar.

But both these classes provide the same functionality. android.support.v7.app.ActionBar Class is used when our uses-sdk min version is less the api 11. to get the object of ActionBar below api 11 we need android.support.v7.app.ActionBar class object.

To get Action bar you need to follow one of two approaches.

  1. import android.support.v7.app.ActionBar and use getSupportActionBar() method od activity.

  2. go to AndroidManifest.xml file and change <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="19" /> and import android.app.ActionBar and use getActionBar()



回答2:

If supporting API levels lower than 11 and you will have to add the support v4 library in your project to used an actionbar.

import android.support.v7.app.ActionBar 

If supporting only API level 11 and higher:

import android.app.ActionBar 

For more information go to Official Docs



回答3:

Make sure that you have minimum api level in your manifest file is above 11. Because the package android.support.v7.app.ActionBar is only supported in api level below 11.

So if you want to use an ActionBar from import android.app.ActionBar then you must be having minimun api level 11 in your manifest file. Otherwise you will have to add the support v4 library in your project to user an actionbar from android.support.v7.app.ActionBar package.



回答4:

Make sure your app has app bar or title bar, check you app theme at manifest file or at your style.xml if in your style like this:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">     <!-- your style --> </style> 

It will fine when use ActionBar actionbar = getSupportActionBar();

But if on your style like this:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- your style--> </style> 

it will throw null exception when use ActionBar actionbar = getSupportActionBar();

Make sure you use import android.support.v7.app.ActionBar



回答5:

If running api 11 and higher

import android.app.ActionBar; 

also if it states that it already exists be sure to remove it



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!