2013-10-02 64 views
13

空指針異常下面是我的活動的代碼。在此我使用的是支持庫程序兼容性使用支持庫共享行動提供

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 { 


@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.activity_main, menu); 
    MenuItem item = (MenuItem) menu.findItem(R.id.share_options); 
    ShareActionProvider shareAction = (ShareActionProvider) MenuItemCompat 
      .getActionProvider(item); 
    Intent shareIntent = new Intent(Intent.ACTION_SEND) 
      .setAction(Intent.ACTION_SEND) 
      .putExtra(Intent.EXTRA_TEXT, "MobiTexter") 
      .setType("text/plain"); 
    shareAction.setShareIntent(shareIntent); 
    return super.onCreateOptionsMenu(menu); 
} 

}

以下是菜單選項我的XML文件,該文件是一個ShareActionProvider的Widget

 <menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:mobitexter="http://schemas.android.com/apk/res-auto" > 

<item 
    android:id="@+id/share_options" 
    android:actionProviderClass="android.support.v7.widget.ShareActionProvider" 
    android:orderInCategory="100" 
    mobitexter:showAsAction="never" 
    mobitexter:title="@string/share_options"/> 

    </menu> 

在此我得到一個NullPointerException異常。以下是我的日誌貓輸出。

  10-02 12:49:00.813: E/AndroidRuntime(9227): FATAL EXCEPTION: main 
      10-02 12:49:00.813: E/AndroidRuntime(9227): java.lang.NullPointerException 
      10-02 12:49:00.813: E/AndroidRuntime(9227): at net.mobitexter.easyshare.MainActivity.onCreateOptionsMenu(MainActivity.java:31) 
      10-02 12:49:00.813: E/AndroidRuntime(9227): at android.app.Activity.onCreatePanelMenu(Activity.java:2444) 
      10-02 12:49:00.813: E/AndroidRuntime(9227): at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224) 
      10-02 12:49:00.813: E/AndroidRuntime(9227): at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:224) 
      10-02 12:49:00.813: E/AndroidRuntime(9227): at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:141) 
      10-02 12:49:00.813: E/AndroidRuntime(9227): at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199) 
      10-02 12:49:00.813: E/AndroidRuntime(9227): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:280) 
      10-02 12:49:00.813: E/AndroidRuntime(9227): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:392) 
      10-02 12:49:00.813: E/AndroidRuntime(9227): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:743) 
      10-02 12:49:00.813: E/AndroidRuntime(9227): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:2838) 
      10-02 12:49:00.813: E/AndroidRuntime(9227): at android.os.Handler.handleCallback(Handler.java:605) 
      10-02 12:49:00.813: E/AndroidRuntime(9227): at android.os.Handler.dispatchMessage(Handler.java:92) 
      10-02 12:49:00.813: E/AndroidRuntime(9227): at android.os.Looper.loop(Looper.java:137) 
      10-02 12:49:00.813: E/AndroidRuntime(9227): at android.app.ActivityThread.main(ActivityThread.java:4448) 
      10-02 12:49:00.813: E/AndroidRuntime(9227): at java.lang.reflect.Method.invokeNative(Native Method) 
      10-02 12:49:00.813: E/AndroidRuntime(9227): at java.lang.reflect.Method.invoke(Method.java:511) 
      10-02 12:49:00.813: E/AndroidRuntime(9227): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823) 
      10-02 12:49:00.813: E/AndroidRuntime(9227): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590) 
      10-02 12:49:00.813: E/AndroidRuntime(9227): at dalvik.system.NativeStart.main(Native Method) 
+0

哪條線是'MainActivity.java: 31'? – ssantos

+0

'shareAction.setShareIntent(shareIntent);' –

+0

你確定支持庫被包含在構建中嗎? – MikeIsrael

回答

29

我發現了錯誤。問題是支持庫需要有一個自定義的前綴,而不是android:actionProviderClass。我在做什麼錯了,我用的android:actionProviderClass代替customprefix:actionProviderClass

在這裏看到:https://developer.android.com/training/basics/actionbar/adding-buttons.html

+0

這已經解決了NPE,但它仍然不會使分享行動的工作。你是如何解決「getActionProvider:項目未實現SupportMenuItem」問題作爲http://stackoverflow.com/questions/24219842/getactionprovider-item-does-not-implement-supportmenuitem – faizal

+0

檢查這點回答: HTTP: //stackoverflow.com/a/26080597/1112177 –

2

如果該行

shareAction.setShareIntent(shareIntent); 

被扔NullPointerException,這句話

(ShareActionProvider) MenuItemCompat.getActionProvider(item); 

必須返回null

有一個看看下面的文章,其中談到爲何會發生這種事。

NullPointerException on setShareIntent using ActionBarSherlock

+0

我不使用actionbarsherlock –

+0

呀有時的問題標題是誤導,但問題是你可能沒有正確設置屬性'安卓:actionProviderClass'爲您的項目'share_options' – ssantos

+0

在'機器人所用的線:actionProviderClass'從什麼是[鏈接] developer.android.com/guide/ui/actionbar –

0

我改變了這種

android:actionProviderClass=widget.ShareActionProvider" /> 

對此

app:actionProviderClass=support.v7.widget.ShareActionProvider" />