动作共享提供程序(java.lang.NullPointerException)

时间:2015-01-19 08:11:14

标签: android shareactionprovider

Hello Everyone如果我对setShareIntent()发表评论,我的app下面的代码正在执行声明,但当我取消注释时应用程序崩溃! 任何人都可以查看代码并帮助我纠正我的错误,因为我已经尝试找到解决方案很长一段时间但还没有解决方案。提前谢谢。

<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;

爪哇:

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$1.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.access$100(ActionBarActivityDelegateBase.java:80)
01-19 02:42:48.602: E/AndroidRuntime(2546):     at android.support.v7.app.ActionBarActivityDelegateBase$1.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)

菜单/ 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>

1 个答案:

答案 0 :(得分:1)

使用app:actionProviderClass代替android:actionProviderClass。后者仅在API级别14中引入,支持库需要其自己的属性。