简体   繁体   English

设置ActionBar后,Android 2.x中出现错误

[英]Error in Android 2.x after setting the ActionBar

Hi guys I set this code in my MainActivity.java to add a share button on my app. 嗨,大家好,我在MainActivity.java中设置了此代码,以在应用程序上添加共享按钮。 I´m aware that action bar doesnt work well on previous android versions. 我知道操作栏在早期的android版本上无法正常工作。 One of the customers that has android 2.3 told me that when he hits the phone menu button the apps crash and its forced to close. 一位装有android 2.3的客户告诉我,当他按下电话菜单按钮时,应用程序崩溃并被迫关闭。 From version 3.0 to above everythings goes fine, you see the share button. 从3.0版到更高版本,一切正常,您会看到“共享”按钮。 Is there some line of code I could add prior to this function to override o prevent using the share button action if the android version is below 3.0?? 我可以在此功能之前添加一些代码行来覆盖o如果Android版本低于3.0,则阻止使用共享按钮操作?

This is what I have in my code: 这是我的代码中的内容:

@Override

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        MenuItem shareItem = (MenuItem) menu.findItem(R.id.action_share);
        ShareActionProvider mShare = (ShareActionProvider)shareItem.getActionProvider();

        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, "Download here https://www.apps.com");

        mShare.setShareIntent(shareIntent);
        return true;
    }

My menu.xml file has the following (shows the icon, works ok): 我的menu.xml文件具有以下内容(显示图标,可以正常运行):

<item android:id="@+id/action_share" android:title="@string/menu_share"
      android:icon="@drawable/menu_share" android:showAsAction="ifRoom"
      android:actionProviderClass="android.widget.ShareActionProvider"></item>

ActionBar is not available out of the box in Android 2.x. 在Android 2.x中无法立即使用ActionBar。 The crash is happening because the MenuItem does not understand getActionProvider() , which was introduced in API 14 (Android 4.0 Ice Cream Sandwich). 发生崩溃是因为MenuItem无法理解getActionProvider() ,它是API 14(Android 4.0 Ice Cream Sandwich)中引入的。

You have two options: 您有两种选择:

  1. use the v7 appcompat library in order to use the ActionBar in older versions of Android; 使用v7 appcompat库 ,以便在旧版Android中使用ActionBar;
  2. or, you could implement the good-old 2.x menus. 或者,您可以实施老式的2.x菜单。

This is what you could do to avoid the crash on 2.x devices. 这是可以避免在2.x设备上崩溃的方法。 Guard the use of the newer APIs by checking that the device is running Ice Cream Sandwich or later: 通过检查设备是否正在运行Ice Cream Sandwich或更高版本来保护使用较新的API:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        MenuItem shareItem = (MenuItem) menu.findItem(R.id.action_share);
        ShareActionProvider mShare = (ShareActionProvider)shareItem.getActionProvider();

        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, "Download here https://www.apps.com");

        mShare.setShareIntent(shareIntent);
    }
    return true;
}

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

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