如何在Android Activity中修复“调用需要API级别11”的问题?

8

我刚开始学习Android开发,基本遵循http://developer.android.com/training/basics/firstapp/starting-activity.html的教程。但有一件事情让我困扰:我有两个类,都是使用“Android活动”创建向导创建的:

public class PlacesActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_places);
        // I manually commented out the following line to get it to compile.
        // getActionBar().setDisplayHomeAsUpEnabled(true);
    }
    // ... other methods
}

并且

public class ShowOnePlace extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_one_place);
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
    // ... other methods
}

Eclipse一直给我以下错误提示:

调用需要API级别11(当前最小值为8):android.app.Activity#getActionBar

调用需要API级别11(当前最小值为8):android.app.ActionBar#setDisplayHomeAsUpEnabled

直到我注释掉了其中的一行(如上所示),现在应用程序在我的Android设备上似乎可以编译和运行而没有错误。(好吧,至少没有那个错误)
所以:
  • 为什么getActionBar().setDisplayHomeAsUpEnabled(true);在一个类中会出现这个错误,但是在另一个类中这一行代码完全没有错,甚至没有警告?
  • “Android Activity”创建向导中是否存在错误,某种方式下getActionBar().setDisplayHomeAsUpEnabled(true);语句在一个活动中是错误的,但在另一个活动中却完全正确?
  • Eclipse Lint是否存在错误,该语句是否实际上在两个活动中都是正确的(或者可能在两个活动中都是错误的)?
  • 我需要怎样做才能在不将minSdkVersion从“8”更改为“11”的情况下解决此问题?我认为“android-support-v4.jar”文件是一个兼容性库,可在版本8的设备上运行,以处理在版本11设备中本地处理的东西。
  • 当我看到这个错误时,真正正确的做法是什么?
7个回答

16

使用getSupportActionBar()代替getActionBar()。这将在API级别低于11的设备上提供ActionBar功能。


14
简而言之,Action Bar在较旧版本的Android中不可用,而Google的支持库也没有提供该功能。这就是为什么lint会抱怨 - 您正在使用不存在于API版本8中的代码,而该版本被设置为您的应用程序所支持的最低API级别。如果您想在旧版Android上使用操作栏,请使用名为“Action Bar Sherlock”的开源库。
现在回答您的问题:
  1. 这应该是两个类中的错误。其中一个类没有出现错误的原因并不重要。
  2. 不是
  3. 两个活动都有问题
  4. 您必须使用提供旧版Android上的操作栏的开源库,例如ActionBar Sherlock适用于此目的。
  5. 真正正确的做法是打开Android Developers网站并阅读支持库的限制。

另外,http://developer.android.com 允许您设置所需的API级别,以便您不会意外地包含在您的应用程序中无法工作的内容。 - DigCamara

4
如果您使用了appcompat-v7及以上版本,则无法使用ActionBarSherlock,因为它们都定义了相同的属性。因此,这些库在资源上存在冲突。
要使用支持库完成此操作,您需要: (我希望您已经下载并将支持库集成到项目中)
1. 您必须使用来自支持库的ActionBar,因此必须使用正确的ActionBarActivity实现扩展您的活动。 导入:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;

活动:

public class PlacesActivity extends ActionBarActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_places);
        // I manually commented out the following line to get it to compile.
        // getActionBar().setDisplayHomeAsUpEnabled(true);
    }
    // ... other methods
}

因此,您可以从支持旧版本的Activity中获取actionBar实现:

 ActionBar actionBar = activity.getSupportActionBar();

编写代码,享受乐趣!


3

我通常使用"@TargetApi(12)"注释来解决这个问题。

   @TargetApi(11)
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_show_one_place);
      getActionBar().setDisplayHomeAsUpEnabled(true);
   }

1
Why is "getActionBar().setDisplayHomeAsUpEnabled(true);" give that error in one class, but that exact same line is not an error -- not even a warning -- in the other class?

只需编辑第二个文件并保存即可。这样会出现错误!

Is there a bug in the "Android Activity" creation wizard, and somehow that "getActionBar().setDisplayHomeAsUpEnabled(true);" statement is wrong in one activity, but perfectly fine in the other?

可能是

Is maybe there a bug in Eclipse Lint, and that statement is actually OK in both activities (or perhaps wrong in both activities)?

在给定的上下文中,两者都是错误的。

What do I have to do so that I can fix this without changing minSdkVersion="8" to minSdkVersion="11"? I was under the impression that the "android-support-v4.jar" file was a compatibility library that ran on version 8 devices to handle stuff that's handled natively in version 11 devices.

我猜只有更改minSDK才是解决方法。如果你想使用 Functions API 11。


0

只需在AndroidManifest中进行更改

就像这样

<uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

-2
问题在于当你在Eclipse中创建一个Android应用程序时,会被要求选择最小和最大的API支持版本。默认情况下,Eclipse会选择8作为最小版本,19作为最大版本。如果你遇到上述错误,这意味着你正在使用的函数版本高于8。
为了解决这个问题,我只需要进入AndroidManifest.xml文件并更改以下内容。
<uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />

minsdkversion 默认是8


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接