gpt4 book ai didi

java - Android:共享菜单,我的菜单充气器无法解析我的资源

转载 作者:行者123 更新时间:2023-12-02 06:25:54 25 4
gpt4 key购买 nike

我正在尝试向我的应用程序添加一个共享菜单,该菜单将文本从 textview 2 发送到另一个应用程序 I.E KiK,但目前我无法让它识别资源文件,因为菜单是我的代码:

public class ME extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(android.R.menu.main, menu);
MenuItem shareItem = menu.findItem(R.id.action_share);

ShareActionProvider mShare = (ShareActionProvider)shareItem.getActionProvider();
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hi");
mShare.setShareIntent(shareIntent);

return super.onCreateOptionsMenu(menu);
}
}

getMenuInflater().inflate(android.R.menu.main, menu); 是问题

还有我的 XML 资源文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
android:="@+id/TextView 1"
android:orientation="vertical"
android:layout_width="400dp"
android:layout_height="300dp" >
</TextView>

<TextView
android:="@+id/TextView 2"
android:orientation="vertical"
android:layout_width="400dp"
android:layout_height="300dp" >
</TextView>

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/action_share"
android:orderInCategory="100"
android:icon="@drawable/ic_menu_share"
android:actionProviderClass= "android.widget.ShareActionProvider" />
</menu>
</LinearLayout>

最佳答案

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/action_share"
android:orderInCategory="100"
android:icon="@drawable/ic_menu_share"
android:actionProviderClass= "android.widget.ShareActionProvider"
/>
</menu>

应该放在菜单文件夹中的单独 xml 文件中(例如 main.xml)。

并且,

getMenuInflater().inflate(android.R.menu.main, 菜单);

应更正为

getMenuInflater().inflate(R.menu.main, menu);

关于java - Android:共享菜单,我的菜单充气器无法解析我的资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20534339/

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