gpt4 book ai didi

Android :This is not supported, 使用 MenuItemCompat.getActionProvider()

转载 作者:行者123 更新时间:2023-11-29 15:45:51 28 4
gpt4 key购买 nike

我的 Activity 中有一个 TextView,我从 SQLite 将数据加载到该 TextView 中

在该 Activity 中,我有一个菜单选项“分享”。
当我单击该图标时,我的 Activity 崩溃了。

这是代码和 LogCat 错误

 ShareActionProvider provider;
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
detailtext= (TextView) findViewById(R.id.detail);
if (id==R.id.menu_item_share)
{
doShare();
}
return super.onOptionsItemSelected(item);
}

public void doShare() {
// populate the share intent with data
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "This is a message for you");
provider.setShareIntent(intent);
}

菜单.xml

<item android:id="@+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:icon="@mipmap/menu_item_share"
android:actionProviderClass="android.widget.ShareActionProvider" />

日志:

java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.getActionProvider()
at android.support.v7.internal.view.menu.MenuItemImpl.getActionProvider(MenuItemImpl.java:645)
at com.example.aeiltech.sidd.DetailActivity.onCreateOptionsMenu(DetailActivity.java:115)
at android.app.Activity.onCreatePanelMenu(Activity.java:2661)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:262)
at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:267)
at android.support.v7.app.AppCompatDelegateImplV7.preparePanel(AppCompatDelegateImplV7.java:1221)
at android.support.v7.app.AppCompatDelegateImplV7.doInvalidatePanelMenu(AppCompatDelegateImplV7.java:1501)
at android.support.v7.app.AppCompatDelegateImplV7.access$100(AppCompatDelegateImplV7.java:90)
at android.support.v7.app.AppCompatDelegateImplV7$1.run(AppCompatDelegateImplV7.java:128)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5641)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1288)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1104)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

首先,将 android.widget.ShareActionProvider 与 appcompat-v7 操作栏反向移植(例如 ActionBarActivity)结合使用。要么使用 ShareActionProvider 的 appcompat-v7 版本,要么将所有内容移至 native 操作栏。

So Change public class Activity extends AppCompatActivity and import the class import android.support.v7.widget.ShareActionProvider;

我想分享 textview 的详细文本,所以使用

SelectedText=detailtext.getText().toString();

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.menu_detail, menu);
MenuItem shareItem = menu.findItem(R.id.menu_item_share);

mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);

return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

if (id==R.id.menu_item_share)
{
doShare();


}



return super.onOptionsItemSelected(item);
}
public void doShare() {
// populate the share intent with data

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,SelectedText);
mShareActionProvider.setShareIntent(intent);

}

关于Android :This is not supported, 使用 MenuItemCompat.getActionProvider(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33434728/

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