我想打开片段,当点击菜单项,但它不工作,我不知道为什么。我试图调试,但没有任何帮助。如果有人可以提供帮助,我会很高兴的。提前谢谢。
menu_main.xml
var book1Name = Book1.Name;
var book2Name = Book2.Name;
MainActivity.class
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.lineup.android.webbrowser.activities.MainActivity">
<item
android:id="@+id/clear_spinner"
android:icon="@drawable/ic_clear"
android:orderInCategory="50"
android:title="Clear"
app:showAsAction="collapseActionView|ifRoom" />
<item
android:id="@+id/action_menu"
android:icon="@drawable/ic_menu"
android:orderInCategory="200"
android:title="@string/menu"
app:showAsAction="always">
<menu>
<item
android:id="@+id/downloads"
android:title="@string/downloads"
android:icon="@drawable/ic_download"/>
<item
android:id="@+id/draw"
android:title="@string/draw"
android:icon="@drawable/ic_paint"/>
<item
android:id="@+id/history"
android:title="@string/history"
android:icon="@drawable/ic_action_clock"/>
<item
android:id="@+id/settings"
android:title="@string/settings"
android:icon="@drawable/ic_settings"/>
</menu>
</item>
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_search"
android:orderInCategory="100"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom" />
DownloadsFragment.class
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.downloads:
DownloadsFragment dlfrag = new DownloadsFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.container, dlfrag)
.commit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
答案 0 :(得分:0)
在onCreate
方法中添加setHasOptionsMenu(true);
答案 1 :(得分:0)
return super.onCreateView(inflater, container, savedInstanceState);
您需要退回view
此处