行动分享提供商 (java.lang.NullPointerException)
Action Share Provider (java.lang.NullPointerException)
大家好,如果我对 setShareIntent() 进行评论,我的应用程序的以下代码正在执行;声明,但当我取消注释时应用程序崩溃了!
任何人都可以查看代码并帮助我纠正我的错误,因为很长一段时间以来我一直在努力寻找解决方案但还没有解决方案。提前致谢。
<item
android:id="@+id/action_share"
android:orderInCategory="100"
android:title="FS7"
android:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="ifRoom"/>
进口:
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
import android.view.Menu;
import android.view.MenuItem;
Java:
public class MainActivity extends ActionBarActivity {
private ShareActionProvider mShareActionProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
MenuItem item = menu.findItem(R.id.action_share);
mShareActionProvider = (ShareActionProvider) MenuItemCompat
.getActionProvider(item);
mShareActionProvider.setShareIntent(getDefaultSharedPreferences());
return super.onCreateOptionsMenu(menu);
}
private Intent getDefaultSharedPreferences() {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, "FS7");
i.putExtra(Intent.EXTRA_TEXT, "Download my free application from URL...");
return i;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
LOGCAT:
01-19 02:42:48.602: E/AndroidRuntime(2546): FATAL EXCEPTION: main
01-19 02:42:48.602: E/AndroidRuntime(2546): Process: com.example.shareaction, PID: 2546
01-19 02:42:48.602: E/AndroidRuntime(2546): java.lang.NullPointerException
01-19 02:42:48.602:E/AndroidRuntime(2546):atcom.example.shareaction.MainActivity.onCreateOptionsMenu(MainActivity.java:30)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:275)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.support.v7.app.ActionBarActivityDelegate.onCreatePanelMenu(ActionBarActivityDelegate.java:79)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:1056)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.support.v7.app.ActionBarActivityDelegateBase.doInvalidatePanelMenu(ActionBarActivityDelegateBase.java:1259)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.support.v7.app.ActionBarActivityDelegateBase.access0(ActionBarActivityDelegateBase.java:80)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.support.v7.app.ActionBarActivityDelegateBase.run(ActionBarActivityDelegateBase.java:119)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.os.Handler.handleCallback(Handler.java:733)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.os.Handler.dispatchMessage(Handler.java:95)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.os.Looper.loop(Looper.java:136)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-19 02:42:48.602: E/AndroidRuntime(2546): at java.lang.reflect.Method.invokeNative(Native Method)
01-19 02:42:48.602: E/AndroidRuntime(2546): at java.lang.reflect.Method.invoke(Method.java:515)
01-19 02:42:48.602: E/AndroidRuntime(2546): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-19 02:42:48.602: E/AndroidRuntime(2546): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-19 02:42:48.602: E/AndroidRuntime(2546): at dalvik.system.NativeStart.main(Native Method)
Menu/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.example.shareaction.MainActivity" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
<item
android:id="@+id/action_share"
android:orderInCategory="100"
android:title="FS7"
android:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="ifRoom"/>
</menu>
使用 app:actionProviderClass
而不是 android:actionProviderClass
。后者在API level 14才引入,支持库需要自己的属性。
大家好,如果我对 setShareIntent() 进行评论,我的应用程序的以下代码正在执行;声明,但当我取消注释时应用程序崩溃了! 任何人都可以查看代码并帮助我纠正我的错误,因为很长一段时间以来我一直在努力寻找解决方案但还没有解决方案。提前致谢。
<item
android:id="@+id/action_share"
android:orderInCategory="100"
android:title="FS7"
android:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="ifRoom"/>
进口:
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
import android.view.Menu;
import android.view.MenuItem;
Java:
public class MainActivity extends ActionBarActivity {
private ShareActionProvider mShareActionProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
MenuItem item = menu.findItem(R.id.action_share);
mShareActionProvider = (ShareActionProvider) MenuItemCompat
.getActionProvider(item);
mShareActionProvider.setShareIntent(getDefaultSharedPreferences());
return super.onCreateOptionsMenu(menu);
}
private Intent getDefaultSharedPreferences() {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, "FS7");
i.putExtra(Intent.EXTRA_TEXT, "Download my free application from URL...");
return i;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
LOGCAT:
01-19 02:42:48.602: E/AndroidRuntime(2546): FATAL EXCEPTION: main
01-19 02:42:48.602: E/AndroidRuntime(2546): Process: com.example.shareaction, PID: 2546
01-19 02:42:48.602: E/AndroidRuntime(2546): java.lang.NullPointerException
01-19 02:42:48.602:E/AndroidRuntime(2546):atcom.example.shareaction.MainActivity.onCreateOptionsMenu(MainActivity.java:30)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:275)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.support.v7.app.ActionBarActivityDelegate.onCreatePanelMenu(ActionBarActivityDelegate.java:79)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:1056)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.support.v7.app.ActionBarActivityDelegateBase.doInvalidatePanelMenu(ActionBarActivityDelegateBase.java:1259)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.support.v7.app.ActionBarActivityDelegateBase.access0(ActionBarActivityDelegateBase.java:80)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.support.v7.app.ActionBarActivityDelegateBase.run(ActionBarActivityDelegateBase.java:119)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.os.Handler.handleCallback(Handler.java:733)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.os.Handler.dispatchMessage(Handler.java:95)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.os.Looper.loop(Looper.java:136)
01-19 02:42:48.602: E/AndroidRuntime(2546): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-19 02:42:48.602: E/AndroidRuntime(2546): at java.lang.reflect.Method.invokeNative(Native Method)
01-19 02:42:48.602: E/AndroidRuntime(2546): at java.lang.reflect.Method.invoke(Method.java:515)
01-19 02:42:48.602: E/AndroidRuntime(2546): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-19 02:42:48.602: E/AndroidRuntime(2546): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-19 02:42:48.602: E/AndroidRuntime(2546): at dalvik.system.NativeStart.main(Native Method)
Menu/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.example.shareaction.MainActivity" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
<item
android:id="@+id/action_share"
android:orderInCategory="100"
android:title="FS7"
android:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="ifRoom"/>
</menu>
使用 app:actionProviderClass
而不是 android:actionProviderClass
。后者在API level 14才引入,支持库需要自己的属性。