当我尝试向操作栏添加“共享”按钮时,应用程序崩溃。下面是我的res / main.xml以及我的onCreateOptionsMenu,包括我的logCat。失败的行号是1077,我在下面突出显示。
FAILING LINE - > myShareActionProvider.setShareIntent(myIntent);
在我的res / main.xml中:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.devmav.focusoutfacebook.MainActivityCircularSeekbar"
>
<item android:id="@+id/menu_item_share"
android:showAsAction="always"
android:title="Share"
android:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
在我的主要活动扩展课程中,以下是我的 onCreateOptionsMenu 实施。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
ShareActionProvider myShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_SEND);
myIntent.putExtra(Intent.EXTRA_TEXT, "Whatever message you want to share");
myIntent.setType("text/plain");
//THIS IS LINE 1077 THAT IS FAILING
myShareActionProvider.setShareIntent(myIntent);
return true;
//return super.onCreateOptionsMenu(menu);
}
LogCat错误输出:
06-10 03:48:20.466: E/AndroidRuntime(1765): java.lang.NullPointerException
**06-10 03:48:20.466: E/AndroidRuntime(1765): at com.mavdev.focusoutfacebook.MainActivityCircularSeekbar.onCreateOptionsMenu(MainActivityCircularSeekbar.java:1077)**
06-10 03:48:20.466: E/AndroidRuntime(1765): at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
06-10 03:48:20.466: E/AndroidRuntime(1765): at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
06-10 03:48:20.466: E/AndroidRuntime(1765): at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
06-10 03:48:20.466: E/AndroidRuntime(1765): at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:146)
06-10 03:48:20.466: E/AndroidRuntime(1765): at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
06-10 03:48:20.466: E/AndroidRuntime(1765): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:293)
06-10 03:48:20.466: E/AndroidRuntime(1765): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
06-10 03:48:20.466: E/AndroidRuntime(1765): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
06-10 03:48:20.466: E/AndroidRuntime(1765): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
06-10 03:48:20.466: E/AndroidRuntime(1765): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
06-10 03:48:20.466: E/AndroidRuntime(1765): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
06-10 03:48:20.466: E/AndroidRuntime(1765): at android.view.Choreographer.doFrame(Choreographer.java:543)
06-10 03:48:20.466: E/AndroidRuntime(1765): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
06-10 03:48:20.466: E/AndroidRuntime(1765): at android.os.Handler.handleCallback(Handler.java:733)
06-10 03:48:20.466: E/AndroidRuntime(1765): at android.os.Handler.dispatchMessage(Handler.java:95)
06-10 03:48:20.466: E/AndroidRuntime(1765): at android.os.Looper.loop(Looper.java:136)
06-10 03:48:20.466: E/AndroidRuntime(1765): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-10 03:48:20.466: E/AndroidRuntime(1765): at java.lang.reflect.Method.invokeNative(Native Method)
06-10 03:48:20.466: E/AndroidRuntime(1765): at java.lang.reflect.Method.invoke(Method.java:515)
06-10 03:48:20.466: E/AndroidRuntime(1765): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-10 03:48:20.466: E/AndroidRuntime(1765): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-10 03:48:20.466: E/AndroidRuntime(1765): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
尝试将其置于空检查中,如下所示:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.feed, menu);
// Set up ShareActionProvider's default share intent
MenuItem shareItem = menu.findItem(R.id.action_share);
mShareActionProvider = (ShareActionProvider) MenuItemCompat
.getActionProvider(shareItem);
if (mShareActionProvider != null) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, mStory.getTitle());
intent.putExtra(Intent.EXTRA_TEXT, mStory.getDescription() + "\n"
+ mStory.getUrl() + "\n\n\n" + "Blah blah blah");
mShareActionProvider.setShareIntent(intent);
}
super.onCreateOptionsMenu(menu, inflater);
}
另外,请确保extras
中的Intent
null
不是shareActionProvider
!在过去,我创建了NullPointerException
并收到了Intent-Extras
,因为null
是{{1}}!