简体   繁体   English

Android:getMenuInflater()。inflate引发android.widget.ShareActionProvider无法转换为android.support.v4.view.ActionProvider

[英]Android: getMenuInflater().inflate throws android.widget.ShareActionProvider cannot be cast to android.support.v4.view.ActionProvider

I'm developing an Android app and I having trouble implementing the share button on the template "Master / detail flow" 我正在开发一个Android应用,无法在模板“主数据/明细数据流”上实现共享按钮

When I create the menu on the ItemDetailActivity which extends AppCompatActivity (however, I've also tried extending ActionBarActivity) I get the following error java.lang.ClassCastException: android.widget.ShareActionProvider cannot be cast to android.support.v4.view.ActionProvider in this line: 当我在扩展AppCompatActivity的ItemDetailActivity上创建菜单时(但是,我也尝试扩展ActionBarActivity),出现以下错误java.lang.ClassCastException:android.widget.ShareActionProvider无法转换为android.support.v4.view。此行中的ActionProvider:

getMenuInflater().inflate(R.menu.menu_ficha, menu); getMenuInflater()。inflate(R.menu.menu_ficha,menu);

Here some code: Java 这里有一些代码:Java

public class ItemDetailActivity extends AppCompatActivity {

ShareActionProvider mShareActionProvider;


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

    // Show the Up button in the action bar.
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    if (savedInstanceState == null) {
        // Create the detail fragment and add it to the activity
        // using a fragment transaction.
        Bundle arguments = new Bundle();
        arguments.putStringArray(ItemDetailFragment.ARG_ITEM_ID,
                      getIntent().getStringArrayExtra(ItemDetailFragment.ARG_ITEM_ID));
        ItemDetailFragment fragment = new ItemDetailFragment();
        fragment.setArguments(arguments);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.item_detail_container, fragment)
                .commit();
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {

        NavUtils.navigateUpTo(this, new Intent(this, ItemListActivity.class));
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_ficha, menu);
    // Access the Share Item defined in menu XML
    MenuItem shareItem = menu.findItem(R.id.menu_item_share);

    // Access the object responsible for
    // putting together the sharing submenu

    mShareActionProvider = (ShareActionProvider)     MenuItemCompat.getActionProvider(shareItem);
    // Create an Intent to share your content
    setShareIntent();

    return true;
};

private void setShareIntent() {

    if (mShareActionProvider != null) {

        // create an Intent with the contents of the TextView
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Testing Oceanbook");
        shareIntent.putExtra(Intent.EXTRA_TEXT, ItemDetailFragment.data[1]);

        // Make sure the provider knows
        // it should work with that Intent
        mShareActionProvider.setShareIntent(shareIntent);
    }
}
}

And the XML of the menu: 菜单的XML:

<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:oceanbook="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/menu_item_share"
    android:title="Compartir"
    oceanbook:showAsAction="ifRoom"
    oceanbook:actionProviderClass="android.widget.ShareActionProvider" />
</menu>

Just in case is important, the XML of the Activity: 以防万一很重要,该活动的XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_detail_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ItemDetailActivity"
tools:ignore="MergeRootFrame" />

Could anyone help me please? 有人可以帮我吗?

Replace: 更换:

oceanbook:actionProviderClass="android.widget.ShareActionProvider"

with: 与:

oceanbook:actionProviderClass="android.support.v7.widget.ShareActionProvider"

If you are using the appcompat-v7 action bar backport, you have to use the ShareActionProvider for the appcompat-v7 action bar backport. 如果您正在使用appcompat-v7操作栏回传,则必须将ShareActionProvider用于appcompat-v7操作栏回传。 Since you are inheriting from AppCompatActivity , you are using this backport. 由于您是继承自AppCompatActivity ,因此您正在使用此反向端口。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将android.support.v4.view.MenuItemCompat转换为android.widget.ShareActionProvider - Cannot cast android.support.v4.view.MenuItemCompat to android.widget.ShareActionProvider 无法从ActionProvider强制转换为ShareActionProvider - Cannot cast from ActionProvider to ShareActionProvider android.support.v7.internal.view.menu.ActionMenuItemView无法转换为android.widget.Button - android.support.v7.internal.view.menu.ActionMenuItemView cannot be cast to android.widget.Button android.support.v7.widget.AppCompatImageView无法转换为android.view.ViewGroup - android.support.v7.widget.AppCompatImageView cannot be cast to android.view.ViewGroup android.support.v4.widget.DrawerLayout无法转换为android.support.v4.widget.DrawerLayout $ DrawerListener - android.support.v4.widget.DrawerLayout cannot be cast to android.support.v4.widget.DrawerLayout$DrawerListener android.support.v4.widget.NestedScrollView无法转换为android.support.v7.widget.RecyclerView - android.support.v4.widget.NestedScrollView cannot be cast to android.support.v7.widget.RecyclerView RelativeLayout无法强制转换为android.support.v7.widget.Toolbar - RelativeLayout cannot be cast to android.support.v7.widget.Toolbar CoordinatorLayout 无法转换为 android.support.v7.widget.Toolbar - CoordinatorLayout cannot be cast to android.support.v7.widget.Toolbar ClassCastException:无法将android.support.v4.widget.SwipeRefreshLayout强制转换为android.widget.ListView - ClassCastException: android.support.v4.widget.SwipeRefreshLayout cannot be cast to android.widget.ListView android.support.v7.widget.AppCompatButton 不能转换为 android.widget.LinearLayout - android.support.v7.widget.AppCompatButton cannot be cast to android.widget.LinearLayout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM