我正在编写一个简单的程序,最近2个小时,但没有得到任何解决这个小问题的方法,使用下面的代码:
MainActivity.java: -
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
// Set up ShareActionProvider's default share intent
MenuItem shareItem = menu.findItem(R.id.share);
ShareActionProvider mShareActionProvider = (ShareActionProvider)
MenuItemCompat.getActionProvider(shareItem);
mShareActionProvider.setShareIntent(getDefaultIntent());
return super.onCreateOptionsMenu(menu);
}
private Intent getDefaultIntent() {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "I am using Android App !!!");
return intent;
}
}
main.xml中: -
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/share"
android:title="@string/share"
yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider"
yourapp:showAsAction="ifRoom" />
</menu>
在这个小程序中,我没有弄到错误的地方......请让我知道我的错误并帮助我解决这个问题!
行号:79
mShareActionProvider.setShareIntent(getDefaultIntent());
我遵循了tutorial
日志说:
02-20 05:51:48.814: E/AndroidRuntime(2012): FATAL EXCEPTION: main
02-20 05:51:48.814: E/AndroidRuntime(2012): Process: com.learn2crack.tab, PID: 2012
02-20 05:51:48.814: E/AndroidRuntime(2012): java.lang.NullPointerException
02-20 05:51:48.814: E/AndroidRuntime(2012): at com.learn2crack.tab.MainActivity.onCreateOptionsMenu(MainActivity.java:79)
02-20 05:51:48.814: E/AndroidRuntime(2012): at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
02-20 05:51:48.814: E/AndroidRuntime(2012): at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
02-20 05:51:48.814: E/AndroidRuntime(2012): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
02-20 05:51:48.814: E/AndroidRuntime(2012): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
02-20 05:51:48.814: E/AndroidRuntime(2012): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
02-20 05:51:48.814: E/AndroidRuntime(2012): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
02-20 05:51:48.814: E/AndroidRuntime(2012): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
02-20 05:51:48.814: E/AndroidRuntime(2012): at android.view.Choreographer.doFrame(Choreographer.java:543)
02-20 05:51:48.814: E/AndroidRuntime(2012): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
02-20 05:51:48.814: E/AndroidRuntime(2012): at android.os.Handler.handleCallback(Handler.java:733)
02-20 05:51:48.814: E/AndroidRuntime(2012): at android.os.Handler.dispatchMessage(Handler.java:95)
02-20 05:51:48.814: E/AndroidRuntime(2012): at android.os.Looper.loop(Looper.java:136)
02-20 05:51:48.814: E/AndroidRuntime(2012): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-20 05:51:48.814: E/AndroidRuntime(2012): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 05:51:48.814: E/AndroidRuntime(2012): at java.lang.reflect.Method.invoke(Method.java:515)
02-20 05:51:48.814: E/AndroidRuntime(2012): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-20 05:51:48.814: E/AndroidRuntime(2012): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-20 05:51:48.814: E/AndroidRuntime(2012): at dalvik.system.NativeStart.main(Native Method)
02-20 05:51:51.484: I/Process(2012): Sending signal. PID: 2012 SIG: 9
答案 0 :(得分:3)
对我来说问题出在proGuard上。将此添加到规则文件后问题解决了:
-keep class android.support.v7.widget.ShareActionProvider { *; }
-keep class android.support.v7.widget.SearchView { *; }
答案 1 :(得分:0)
你可以试试这个,它为我工作
ShareActionProvider mShareActionProvider = new ShareActionProvider(getActivity());
MenuItemCompat.setActionProvider(shareItem, mShareActionProvider);