gpt4 book ai didi

java - 无法将 android.support.v4.view.MenuItemCompat 转换到 android.widget.ShareActionProvider

转载 作者:行者123 更新时间:2023-11-29 20:38:34 26 4
gpt4 key购买 nike

我目前正在使用在线教程学习 android,可以在 http://www.raywenderlich.com/78576/android-tutorial-for-beginners-part-2 找到该教程

到目前为止一切顺利,但我现在遇到了一些问题,因为即使我的代码与教程中的代码匹配,我仍然收到上面的错误消息(该线程的标题)。

我的导入如下...

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.widget.ShareActionProvider;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Button;

我在下面的 if 语句中遇到问题...

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu.
// Adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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
if (shareItem != null) {
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, "Android Development");
shareIntent.putExtra(Intent.EXTRA_TEXT, mainTextView.getText());

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

我认为这可能是由于最近的更新导致一些代码过时了,但我真的没有太多想法,因为我对 android 没有那么多经验。

感谢帮助

最佳答案

您需要从支持库中导入 ShareActionProvider,如下所示:

import android.support.v7.widget.ShareActionProvider;

由于您支持较旧的 Android 版本并使用 MenuItemCompat 的支持库实现,因此您还需要使用与其交互的其他类的支持版本(如果它们可用)。使用自动导入时需要注意,如果有选择,请选择支持的版本。

关于java - 无法将 android.support.v4.view.MenuItemCompat 转换到 android.widget.ShareActionProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31214821/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com