簡體   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

我正在開發一個Android應用,無法在模板“主數據/明細數據流”上實現共享按鈕

當我在擴展AppCompatActivity的ItemDetailActivity上創建菜單時(但是,我也嘗試擴展ActionBarActivity),出現以下錯誤java.lang.ClassCastException:android.widget.ShareActionProvider無法轉換為android.support.v4.view。此行中的ActionProvider:

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

這里有一些代碼: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>

以防萬一很重要,該活動的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 ,因此您正在使用此反向端口。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM