Android的错误java.lang.NoSuchMethodError:android.view.MenuItem.getActionProvider [英] Android error java.lang.NoSuchMethodError: android.view.MenuItem.getActionProvider

查看:148
本文介绍了Android的错误java.lang.NoSuchMethodError:android.view.MenuItem.getActionProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我启用了 ShareActionProvider ,我得到这个错误:

I enabled the ShareActionProvider and I get this error:

java.lang.NoSuchMethodError: android.view.MenuItem.getActionProvider

但我使用这个类的方法是这样的:

But the way I use this class is like this:

    // SHARING ONLY ENABLED in SDK 14 which is Ice Cream Sandwich
    try
    {           
        if ( android.os.Build.VERSION.SDK_INT >= 14 )
        {
            Button share = (Button)findViewById(R.id.share_button); 
            share.setOnClickListener(new Button.OnClickListener() 
            {  
                public void onClick(View v) 
                {                   
                    openOptionsMenu();
                }
            });        
        }
        else
        {
            // HIDE THE TWO PAGE ELEMENTS
            Button share = (Button)findViewById(R.id.share_button); 
            TextView share_prompt = (TextView)findViewById(R.id.share_prompt); 

            share.setVisibility(View.GONE);
            share_prompt.setVisibility(View.GONE);              
        }
    }
    catch ( Exception e )
    {

    }

所以我想我就不会显示较早的SDK的共享按钮,我会好的。但是,我得到了很多崩溃。

So I thought that I would not show the share button for earlier sdk's and I would be ok. But I am getting a lot of crashes.

我真的不能测试这个,因为我没有手机的SDK的早期版本。但这是否意味着,这些页面崩溃,大家谁都有早期SDK版本?或只是人谁点击份额?我如何prevent这个崩溃?

I can not really test this because I don't have a phone with an earlier version of the SDK. But does it mean that these pages crash for everyone who has the earlier SDK version? Or just people who click share? How do I prevent this crashing?

和我在班上这些方法。如果我要是SDK小于14只是没有运行它们?

And I have these methods in the class. Should I just not run them if sdk is less than 14?

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    getMenuInflater().inflate(R.layout.menu, menu);
    MenuItem item = menu.findItem(R.id.menu_item_share);
    myShareActionProvider = (ShareActionProvider)item.getActionProvider();
    myShareActionProvider.setShareHistoryFileName(
      ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
    myShareActionProvider.setShareIntent(createShareIntent());
    return true;
}

private Intent createShareIntent() 
{
       Intent shareIntent = new Intent(Intent.ACTION_SEND);
       shareIntent.setType("text/plain");
       shareIntent.putExtra(Intent.EXTRA_TEXT, 
         "Some text");
       return shareIntent;
}

// Somewhere in the application.
public void doShare(Intent shareIntent) 
{
    // When you want to share set the share intent.
    myShareActionProvider.setShareIntent(shareIntent);
}            

感谢。
亚历克斯

Thanks. Alex

推荐答案

ShareActionProvider 仅供API 14+,这意味着在早期版本中,你不能用它用。如果你想在你的应用程序中添加分享按钮,并支持旧的API等级我可以建议你使用 ActionBarSherlock - 一个图书馆,让你在老版本的Andr​​oid使用的ActionBar的机会。使用这个库,你可以做这样的事情添加分享按钮:

ShareActionProvider is available only for API 14+ which means in earlier versions you can't use it. If you want to add share button in your app and support old API levels I can suggest you to use ActionBarSherlock - a library which gives you the opportunity to use ActionBar in older versions of Android. Using this library you can do something like this to add share button :

MenuItem actionItem = menu.findItem(R.id.menu_item_share_action_provider_action_bar);
ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
actionProvider.setShareIntent(createShareIntent());


private Intent createShareIntent() {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("image/*");
    Uri uri = Uri.fromFile(getFileStreamPath("shared.png"));
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    return shareIntent;
}

这将共享的图像文件。如果您不想支持旧的API级别,我建议你只检查API级别,并根据所使用 ShareActionProvider

希望这有助于。

这篇关于Android的错误java.lang.NoSuchMethodError:android.view.MenuItem.getActionProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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