如何修复“呼叫需要API级别11”与Android活动? [英] How to fix "Call requires API level 11" with Android Activity?

查看:99
本文介绍了如何修复“呼叫需要API级别11”与Android活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始做Android开发,
或以上的 http://developer.android.com/training/basics/firstapp/starting-activity.html 教程,
和一件事情会阻止我:
I有两个类,都是使用Android Activity创建向导创建的:

I'm just starting to do Android development, more-or-less following the http://developer.android.com/training/basics/firstapp/starting-activity.html tutorial, and one thing keeps bugging me: I have two classes, both created using the "Android Activity" creation wizard:

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不断给我以下错误:

Eclipse kept giving me the following errors:


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

Call requires API level 11 (current min is 8): android.app.Activity#getActionBar

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

Call requires API level 11 (current min is 8): android.app.ActionBar#setDisplayHomeAsUpEnabled

,直到我注释掉一个的行(如上所示)。
现在,应用程序似乎在我的Android设备
上编译并运行,没有错误。 (嗯,至少没有 错误)

until I commented out one of the lines (as shown above). Now the application seems to compile and run on my Android device without errors. (Well, at least without that error).

所以:


  • 为什么 getActionBar()。setDisplayHomeAsUpEnabled(true); 在一个类中给出错误,但是完全相同的行不是错误 - 甚至不一个警告 - 在另一个类?

  • Android Activity创建向导中有一个错误,不知何故 getActionBar()。setDisplayHomeAsUpEnabled(true) ; 语句在一个活动中是错误的,但在另一个活动中是完美的?

  • Eclipse Lint中可能有一个错误,并且该声明实际上可以在两个活动(或者两个活动中可能是错误的)?

  • 我该怎么办才能解决这个问题,而不会将minSdkVersion =8更改为minSdkVersion =11?我的印象是,android-support-v4.jar文件是一个运行在版本8设备上的兼容性库,用于处理在版本11设备中本机处理的东西。

  • 什么是当我看到这个错误的时候,真的很正确吗?

  • 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.
  • What's really the Right Thing to Do when I see this error?

推荐答案

使用 getSupportActionBar()而不是 getActionBar()。这将提供API低于11的 ActionBar 功能。

Use getSupportActionBar() instead of getActionBar(). That will provide ActionBar features on API levels below 11.

这篇关于如何修复“呼叫需要API级别11”与Android活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆