2016-05-04 15 views
0

を示していない私のメニューリソースファイル共有アクション・プロバイダはここで

<item 
    android:id="@+id/action_share" 
    android:title="@string/action_share" 
    android:orderInCategory="2" 
    app:showAsAction="ifRoom" 
    android:actionProviderClass="android.widget.ShareActionProvider"> 
</item> 
</menu> 

そして、ここでは私のJavaコードは、これは、共有アクション・プロバイダが表示されていない

public boolean onCreateOptionsMenu(Menu menu) 
{ 
    getMenuInflater().inflate(R.menu.menu_main,menu); 
    MenuItem menuItem = menu.findItem(R.id.action_share); 
    shareActionProvider =(ShareActionProvider)menuItem.getActionProvider(); 
    // setIntent("COOL NIKS"); 
    return super.onCreateOptionsMenu(menu); 
} 

です。どうすれば修正できますか?

+0

を使用するようにMainActivityを更新する必要がありますあなたは 'onCreateOptionsMenu(メニューメニュー)'メソッドに置かれている 'Activity'ご提示ください - また、簡単なテストとしては、あなたは '' app:showAsAction''を '' ifRoom''の代わりに ''常に ''に変更することができます – ishmaelMakitla

答えて

0

完全な動作例がどのように見えるかは次のとおりです。これはAppCompatライブラリを使用しています。

メニュー項目XMLにhttp://schemas.android.com/apk/res-autoスキーマを追加する必要があります。

<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" > 
    <item 
     android:id="@+id/action_share" 
     app:actionProviderClass="android.support.v7.widget.ShareActionProvider" 
     app:showAsAction="always" 
     android:title="Share" /> 
</menu> 

今、あなたはShareActionProvider

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.menu_main, menu); 
     // Get the menu item. 
     MenuItem menuItem = menu.findItem(R.id.action_share); 
     // Get the provider and hold onto it to set/change the share intent. 
     mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem); 
     // Set share Intent. 
     // Note: You can set the share Intent afterwords if you don't want to set it right now. 
     mShareActionProvider.setShareIntent(createShareIntent()); 
     return true; 
    } 

    // Create and return the Share Intent 
    private Intent createShareIntent() { 
     Intent shareIntent = new Intent(Intent.ACTION_SEND); 
     shareIntent.setType("text/plain"); 
     shareIntent.putExtra(Intent.EXTRA_TEXT, "http://google.com"); 
     return shareIntent; 
    } 

    // Sets new share Intent. 
    // Use this method to change or set Share Intent in your Activity Lifecycle. 
    private void changeShareIntent(Intent shareIntent) { 
     mShareActionProvider.setShareIntent(shareIntent); 
    } 
} 
関連する問題