溢出按钮UnsuportedOperationException

时间:2016-03-24 17:33:55

标签: android xml

在使用操作栏时,我在溢出按钮中添加了一个共享项 当我向其添加一个图标时,它会显示此异常错误

 import android.content.Intent;
 import android.support.design.widget.NavigationView;
 import android.support.v4.app.FragmentTransaction;
 import android.support.v4.widget.DrawerLayout;
 import android.support.v7.app.ActionBarDrawerToggle;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.support.v7.widget.Toolbar;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.widget.ShareActionProvider;

 public class MainActivity extends AppCompatActivity {

private ShareActionProvider mShareActionProvider;

DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
FragmentTransaction fragmentTransaction;
NavigationView navigationView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close);
    navigationView = (NavigationView) findViewById(R.id.navigation_view);
    navigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {



                @Override
                public boolean onNavigationItemSelected(MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.home_id:
                            fragmentTransaction = getSupportFragmentManager().beginTransaction();
                            fragmentTransaction.replace(R.id.main_container, new HomeFragment());
                            fragmentTransaction.commit();
                            getSupportActionBar().setTitle("Home Fragment");
                            item.setChecked(true);
                            drawerLayout.closeDrawers();
                            break;

                        case R.id.message_id:
                            fragmentTransaction = getSupportFragmentManager().beginTransaction();
                            fragmentTransaction.replace(R.id.main_container, new FatCalculator());
                            fragmentTransaction.commit();
                            getSupportActionBar().setTitle("Fat");
                            item.setChecked(true);
                            drawerLayout.closeDrawers();
                            break;

                        case R.id.setting_id:
                            fragmentTransaction = getSupportFragmentManager().beginTransaction();
                            fragmentTransaction.replace(R.id.main_container, new FoodCalculator());
                            fragmentTransaction.commit();
                            getSupportActionBar().setTitle("Food");
                            item.setChecked(true);
                            drawerLayout.closeDrawers();
                            break;

                        case R.id.storage_id:
                            fragmentTransaction = getSupportFragmentManager().beginTransaction();
                            fragmentTransaction.replace(R.id.main_container, new FoodStorage());
                            fragmentTransaction.commit();
                            getSupportActionBar().setTitle("Storage");
                            item.setChecked(true);
                            drawerLayout.closeDrawers();
                            break;

                        case R.id.rate_id:
                            fragmentTransaction = getSupportFragmentManager().beginTransaction();
                            fragmentTransaction.replace(R.id.main_container, new RateME());
                            fragmentTransaction.commit();
                            getSupportActionBar().setTitle("Rate");
                            item.setChecked(true);
                            drawerLayout.closeDrawers();
                            break;
                    }


                    return true;
                }
            });

    drawerLayout.setDrawerListener(actionBarDrawerToggle);
    fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.main_container, new HomeFragment());
    fragmentTransaction.commit();
    getSupportActionBar().setTitle("Home");
}

我已经为膨胀菜单添加了这些代码行

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    // Locate MenuItem with ShareActionProvider

MenuItem item = menu.findItem(R.id.Share_id);


// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) item.getActionProvider();

return true;
 }
 // Call to update the share intent
 private void setShareIntent(Intent shareIntent) {
    if (mShareActionProvider != null) {
        mShareActionProvider.setShareIntent(shareIntent);
    }
 }

以上行

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    actionBarDrawerToggle.syncState();
}

 }

菜单文件在这里

 <?xml version="1.0" encoding="utf-8"?>
 <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
     android:id="@+id/setting_id"
    android:title="Setting"
    app:showAsAction="always" />
<item
    android:id="@+id/About_id"
    android:title="About"
    app:showAsAction="always" />

<item
    android:id="@+id/Contact_id"
    android:title="Conttact"
    app:showAsAction="never" />
<item
    android:id="@+id/Share_id"
    android:title=""
    android:icon="@drawable/share"
    app:showAsAction="always"
    android:actionProviderClass=
        "android.widget.ShareActionProvider"
    />

 </menu>

logcat的

 java.lang.UnsupportedOperationException: This is not supported,use  MenuItemCompat.getActionProvider() at android.support.v7.view.menu.MenuItemImpl.getActionProvider(MenuItemImpl.java:645)
                                                                                at com.example.rajafarid.navigation.MainActivity.onCreateOptionsMenu(MainActivity.java:110)
                                                                                at android.app.Activity.onCreatePanelMenu(Activity.java:2921)
                                                                                at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:341)
                                                                                at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
                                                                                at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:258)
                                                                                at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
                                                                                at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454)
                                                                                at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:61)
                                                                                at android.os.Handler.handleCallback(Handler.java:739)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:145)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5832)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

1 个答案:

答案 0 :(得分:0)

编辑:

使用此:

MenuItemCompat.getActionProvider(item);

还导入此内容:

android.support.v7.widget.ShareActionProvider;

而不是

import android.widget.ShareActionProvider;

有效吗?