Java 为什么 MenuItemCompat.getActionProvider 返回 null?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19358510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why MenuItemCompat.getActionProvider returns null?
提问by hago
I tried to use android.support.v7.widget.ShareActionProvider on actionbar in my app. So I followed the example from android document but got some issues.
Here's my menu xml:
我尝试在我的应用程序的操作栏上使用 android.support.v7.widget.ShareActionProvider。所以我遵循了 android 文档中的示例,但遇到了一些问题。
这是我的菜单 xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/action_share"
android:orderInCategory="100"
android:icon="@drawable/ic_action_share"
android:title="@string/action_share"
myapp:showAsAction="ifRoom"
myapp:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
here's my code to create the share action button:
这是我创建共享操作按钮的代码:
@Override
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.share, menu);
MenuItem shareItem = menu.findItem(R.id.action_share);
ShareActionProvider mShareActionProvider = (ShareActionProvider)MenuItemCompat.getActionProvider(shareItem);
mShareActionProvider.setShareIntent(getDefaultIntent());
super.onCreateOptionsMenu(menu, inflater);
}
My question is:
我的问题是:
- MenuItemCompat.getActionProvider(shareItem) always returns null for me, why's that?
- When I comment those lines, the share button appears on the bar but does nothing while clicking, how to fix it(if question 1 can't be solved)?
- MenuItemCompat.getActionProvider(shareItem) 总是为我返回 null,这是为什么?
- 当我评论这些行时,共享按钮出现在栏上,但单击时什么也不做,如何解决(如果问题 1 无法解决)?
btw, I checked codes of MenuItemCompat.getActionProvider, it looks like this method will check if the menu item implements SupportMenuItem interface and returns fail if it isn't. How could I deal with it?
顺便说一句,我检查了 MenuItemCompat.getActionProvider 的代码,看起来这个方法会检查菜单项是否实现了 SupportMenuItem 接口,如果不是,则返回失败。我怎么处理呢?
回答by Skytile
Make sure that your class extends AppCompatActivity instead of just Activity.
确保您的类扩展 AppCompatActivity 而不仅仅是 Activity。
Note: Edited to reflect the updated app compat library.
注意:已编辑以反映更新的应用程序兼容库。
回答by Climbatize
I had the same nullPointer probleme in my app. As @josh527 stated, I forgot to define a custom xml namespace for my application. I know it was not your probleme, but some people may reach your post like me and not see that so I just wanted to spot it ;)
我的应用程序中有相同的 nullPointer 问题。正如@josh527 所说,我忘记为我的应用程序定义自定义 xml 命名空间。我知道这不是你的问题,但有些人可能会像我一样看到你的帖子而没有看到,所以我只是想发现它;)
回答by markussvensson
I had the same type of error. MenuItemCompat.getActionProvider returned null.
我有同样类型的错误。MenuItemCompat.getActionProvider 返回 null。
My problem was in ProGuard. Turning ProGuard off solved it for me.
我的问题出在 ProGuard 中。关闭 ProGuard 为我解决了这个问题。
回答by Razec Luar
Here this is the only solution that works to make ShareActionProvider not null...i use set ActionProvider instead...see the code below:
这是使 ShareActionProvider 不为 null 的唯一解决方案...我使用 set ActionProvider 代替...请参阅下面的代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.messages_activity_menu, menu);
MenuItem menuItem = menu.findItem(R.id.menu_item_share);
shareActionProvider = new ShareActionProvider(this);
MenuItemCompat.setActionProvider(menuItem, shareActionProvider);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.menu_item_share){
onShareAction();
}
return super.onOptionsItemSelected(item);
}
private void onShareAction(){
// Create the share Intent
String playStoreLink = "https://play.google.com/store/apps/details?id=" + getPackageName();
String yourShareText = getResources().getString(R.string.share_text) + playStoreLink;
Intent shareIntent = ShareCompat.IntentBuilder.from(this).setType("text/plain").setText(yourShareText).getIntent();
// Set the share Intent
if (shareActionProvider != null) {
shareActionProvider.setShareIntent(shareIntent);
}
}
and...xml
和... xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_item_share"
android:icon="@drawable/ic_action_share"
android:showAsAction="ifRoom|withText"
android:title="@string/menu_item_share" />
</menu>
and other things that may be checked:
以及其他可能被检查的事项:
the activity has to extends ActionBarActivity:
该活动必须扩展 ActionBarActivity:
MyActivity extends ActionBarActivity
check and use this imports:
检查并使用此导入:
import android.support.v4.app.ShareCompat;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.OnNavigationListener;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
In AndroidManifest.xml put this line in your activity's tag attributes:
在 AndroidManifest.xml 中,将此行放在您的活动的标签属性中:
android:theme="@style/Theme.AppCompat.Light"
If you dont know how to import v7 and v4 compatibility libraries see: http://developer.android.com/tools/support-library/setup.html
如果您不知道如何导入 v7 和 v4 兼容性库,请参阅:http: //developer.android.com/tools/support-library/setup.html
回答by itReverie
After some reading and that includes probably some of you responses, finally this issued is solved:
经过一些阅读,其中可能包括你们中的一些回应,最后这个问题得到了解决:
Share_Menu.xml. Make sure you have a custom namespaceand the actionProvider class is from that custom namespace as well as the correct value: android.support.v7.widget.ShareActionProvider
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/action_share" android:title="@string/action_detail_share" myapp:showAsAction="always" myapp:actionProviderClass="android.support.v7.widget.ShareActionProvider"></item> </menu>
Detail_Activity.java
2.1. Inherit from ActionBarActivityinstead of Activity
2.2. Add the correct importsimport android.support.v4.app.Fragment; import android.support.v4.view.MenuItemCompat; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.ShareActionProvider;
AndroidManifest.xmlAdd the android:theme="@style/Theme.AppCompat.Light"
<activity android:name=".detail_activity" android:label="@string/title_activity_detail_activity" android:theme="@style/Theme.AppCompat.Light" android:parentActivityName=".main_activity" >
Build.gradle
4.1. In my case just to stay in the safe side I turn offf ProGuard in Debug.debug { runProguard false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' }
Share_Menu.xml。确保您有一个自定义命名空间,并且 actionProvider 类来自该自定义命名空间以及正确的值:android.support.v7.widget.ShareActionProvider
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/action_share" android:title="@string/action_detail_share" myapp:showAsAction="always" myapp:actionProviderClass="android.support.v7.widget.ShareActionProvider"></item> </menu>
Detail_Activity.java
2.1。继承自ActionBarActivity而不是 Activity
2.2。添加正确的导入import android.support.v4.app.Fragment; import android.support.v4.view.MenuItemCompat; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.ShareActionProvider;
AndroidManifest.xml添加 android:theme=" @style/Theme.AppCompat.Light"
<activity android:name=".detail_activity" android:label="@string/title_activity_detail_activity" android:theme="@style/Theme.AppCompat.Light" android:parentActivityName=".main_activity" >
构建.gradle
4.1。就我而言,为了安全起见,我在 Debug 中关闭了 ProGuard。debug { runProguard false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' }
4.2. Make sure that you have the following compile section in dependencies
4.2. 确保您在依赖项中有以下编译部分
`compile 'com.android.support:appcompat-v7:20.0.+'`
回答by Den
Try to add to unit of activity
尝试添加到活动单元
import android.support.v4.view.MenuItemCompat;
I've had the same problem and it was solution.
我遇到了同样的问题,这是解决方案。
回答by Sebastiaan van Dorst
the variable: Android.Support.V7.Widget.ShareActionProvider shareActionProvider;
变量:Android.Support.V7.Widget.ShareActionProvider shareActionProvider;
this.MenuInflater.Inflate(Resource.Menu.share_action_provider, menu);
var shareItem = menu.FindItem(Resource.Id.menu_item_share_action_provider_action_bar);
MenuItemCompat.SetShowAsAction (shareItem, MenuItemCompat.ShowAsActionIfRoom);
var actionprov = new Android.Support.V7.Widget.ShareActionProvider (this);
MenuItemCompat.SetActionProvider (shareItem, actionprov);
var test = MenuItemCompat.GetActionProvider (shareItem);
shareActionProvider = test.JavaCast<Android.Support.V7.Widget.ShareActionProvider>();
var intent = new Intent(Intent.ActionSend);
intent.SetType("text/plain");
intent.PutExtra(Intent.ExtraText, "ActionBarCompat is Awesome! Support Lib v7 #Xamarin");
shareActionProvider.SetShareIntent (intent);
return base.OnCreateOptionsMenu(menu);
this did the trick with me... i just created my own shareactionprovider ! and i set it myself, then get it... and maybe theres some code even thats not needed.. but its a lot of casting AND MAKE SURE you use the right ones all the time, if you just type "ShareActionProvider" you are in fact using V4..! instead of V7
这对我有用……我刚刚创建了自己的 shareactionprovider !我自己设置了它,然后得到它......也许有一些代码甚至不需要......但它有很多铸造并确保你一直使用正确的,如果你只是输入“ShareActionProvider”你是事实上使用V4..!而不是 V7
回答by yshahak
If someone wants to keep progaurd
on and still use the code:
如果有人想继续progaurd
并仍然使用代码:
ShareActionProvider mShareActionProvider = (ShareActionProvider)MenuItemCompat.getActionProvider(shareItem);
Just need to add to proguard
:
只需要添加到proguard
:
-keep class android.support.v7.widget.ShareActionProvider { *; }
回答by radistao
In my case it was wrong namespacein menu.xml
:
在我的情况下,它是错误的命名空间中menu.xml
:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/menu_item_share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
Pay attention to app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
: it should have
注意app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
:应该有
- correct package(
android.widgetandroid.support.v7.widget) - correct namespace(
androidapp).
- 正确的包(
android.widgetandroid.support.v7.widget) - 正确的命名空间(
android应用程序)。
Unfortunatelly, the compiler compiles it without errors, only Android Studio makes notification with underlining.
不幸的是,编译器编译没有错误,只有 Android Studio 发出带有下划线的通知。
回答by LeoCoder
You should change the declaration and definition of onCreateOptionsMenu function. you have changed the function declaration from that of the base class. this means the method is not overridden.
您应该更改 onCreateOptionsMenu 函数的声明和定义。您已经更改了基类的函数声明。这意味着该方法不会被覆盖。
Try this :
尝试这个 :
@Override
public boolean onCreateOptionsMenu (Menu menu) {
inflater.inflate(R.menu.share, menu);// declare a local layout inflater
MenuItem shareItem = menu.findItem(R.id.action_share);
ShareActionProvider mShareActionProvider = (ShareActionProvider)MenuItemCompat.getActionProvider(shareItem);
mShareActionProvider.setShareIntent(getDefaultIntent());
// super.onCreateOptionsMenu(menu, inflater); <--- Remove this line or put in the first line because base class constructor should be called in the first line of the method.
return super.onCreateOptionsMenu(menu);//<-- Add this line to set the menu after adding menu items
}