0

我正在改变我内部的东西,onCreateOptionsMenu我遇到了一个非常奇怪的崩溃。Logcat告诉我它崩溃了:

这是崩溃:

at com.mycompany.myapp.MainActivity.onCreateOptionsMenu(MainActivity.java)
at android.app.Activity.onCreatePanelMenu(Activity.java:2476)

这是一个空指针异常。

这是我的onCreateOptionsMenu

        @Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.layout.main_menu, menu);

    MenuItem shareNew = menu.findItem(R.id.shareNew);
    MenuItem shareOld = menu.findItem(R.id.shareOld);

    mShareActionProvider = (ShareActionProvider) shareNew.getActionProvider();
    mShareActionProvider.setShareIntent(createShareIntent());

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
        shareOld.setVisible(true);
        shareNew.setVisible(false);
    } else {
        shareOld.setVisible(false);
        shareNew.setVisible(true);
    }

    MenuItem searchItem = menu.findItem(R.id.action_search);

    mSearchView = (SearchView) searchItem.getActionView();
    mSearchView.setQueryHint("Search articles");

    setupSearchView(searchItem);

    return true;

}

这是我的main_menu.xml

<item
    android:id="@+id/starred"
    android:title="Starred articles"    />

<item
    android:queryHint="Search articles"
    android:id="@+id/action_search"
    android:title="Search"
    android:icon="@drawable/search"
    android:showAsAction="always"
    android:actionViewClass="android.widget.SearchView" />


<item

    android:actionProviderClass="android.widget.ShareActionProvider"
    android:id="@+id/shareNew"
    android:title="Share"
    android:showAsAction="ifRoom"
    android:icon="@drawable/share"/>



<item

    android:id="@+id/shareOld"
    android:title="Share"
    android:showAsAction="ifRoom"
    android:icon="@drawable/share"/>

<item
    android:id="@+id/star"
    android:title="Star article"
    android:showAsAction="ifRoom"
    android:icon="@drawable/star"   />

<item
    android:id="@+id/refresh"
    android:title="Refresh"
    android:icon="@drawable/refresh"    
    android:showAsAction="ifRoom"   />

<item
    android:id="@+id/forward"
    android:title="Forward"
    android:icon="@drawable/forward"
    android:showAsAction="ifRoom"   />

<item
    android:showAsAction="ifRoom"
    android:icon="@drawable/copy"
    android:id="@+id/copy"
    android:title="Copy url"    />

<item
    android:id="@+id/browser"
    android:title="Open in browser" />

<item
    android:id="@+id/startPage"
    android:title="Use as startpage"    />

<item
    android:id="@+id/random"
    android:title="Random article"  />

<item
    android:id="@+id/settings"
    android:title="Settings"    />

帮助会非常感激(并且需要:p)

4

0 回答 0