java - Android:getMenuInflater().inflate 抛出 android.widget.ShareActionProvider 无法转换为 android.support.v4.view.ActionProvider

标签 java android menu fragment share

我正在开发 Android 应用程序,但在模板“主/详细流程”上实现共享按钮时遇到问题

当我在扩展 AppCompatActivity 的 ItemDetailActivity 上创建菜单时(但是,我也尝试扩展 ActionBarActivity),出现以下错误 java.lang.ClassCastException:android.widget.ShareActionProvider 无法在此行中转换为 android.support.v4.view.ActionProvider:

getMenuInflater().inflate(R.menu.menu_ficha, 菜单);

这里有一些代码: 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);
    }
}
}

以及菜单的 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>

以防万一, 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" />

有人可以帮我吗?

最佳答案

替换:

oceanbook:actionProviderClass="android.widget.ShareActionProvider"

与:

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

如果您使用 appcompat-v7 操作栏向后移植,则必须使用 ShareActionProvider 进行 appcompat-v7 操作栏向后移植。由于您继承自 AppCompatActivity,因此您正在使用此向后移植。

关于java - Android:getMenuInflater().inflate 抛出 android.widget.ShareActionProvider 无法转换为 android.support.v4.view.ActionProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31412715/

相关文章:

java - 调用 putExtra 时 Activity 未完成

javascript - Uncaught ReferenceError : PhoneGap is not Defined

javascript - 折叠所有菜单并同时隐藏它们?

java - ExceptionMapper的执行顺序

java - XMLWriter 使用 zdef 扩展属性名称?

java - Netty 可以和 ZeroMQ 集成吗?

javascript - 如何阻止页面重定向到之前访问过的页面?

java - NoClassDeFoundError WSServletContextListener 和 XMLStreamBuffer

android - 无法获取未知属性 'android'

android - 蜂窝 ActionBar 项目 - 应用程序崩溃