У меня полностью работающее приложение для Android в портретном режиме. Он использует настраиваемое меню ActionBar (Api-Level> 14) с раскрывающимся меню общего доступа, в котором население составляет onCreateOptionsMenu
:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater mInflater = new MenuInflater(getApplicationContext());
mInflater.inflate(R.menu.menu, menu);
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_share).getActionProvider();
// Set the share intent
Intent i = createShareIntent();
if (i != null)
mShareActionProvider.setShareIntent(i);
// Searching
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
return true;
}
Данные представлены через ViewPagerAdater.
Если я поворачиваю экран (либо в AVD, либо на моем телефоне), MenuInflater выдает ошибку java.lang.ClassNotFoundException
:
03-14 13:21:39.563: W/dalvikvm(1354): dvmFindClassByName rejecting ''
03-14 13:21:39.583: W/MenuInflater(1354): Cannot instantiate class:
03-14 13:21:39.583: W/MenuInflater(1354): java.lang.ClassNotFoundException:
03-14 13:21:39.583: W/MenuInflater(1354): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
03-14 13:21:39.583: W/MenuInflater(1354): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
03-14 13:21:39.583: W/MenuInflater(1354): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
03-14 13:21:39.583: W/MenuInflater(1354): at android.view.MenuInflater$MenuState.newInstance(MenuInflater.java:454)
03-14 13:21:39.583: W/MenuInflater(1354): at android.view.MenuInflater$MenuState.setItem(MenuInflater.java:415)
03-14 13:21:39.583: W/MenuInflater(1354): at android.view.MenuInflater$MenuState.addItem(MenuInflater.java:436)
03-14 13:21:39.583: W/MenuInflater(1354): at android.view.MenuInflater.parseMenu(MenuInflater.java:173)
03-14 13:21:39.583: W/MenuInflater(1354): at android.view.MenuInflater.inflate(MenuInflater.java:95)
03-14 13:21:39.583: W/MenuInflater(1354): at net.stackueberflow.excusewidget.ExcuseBrowseActivity.onCreateOptionsMenu(ExcuseBrowseActivity.java:128)
03-14 13:21:39.583: W/MenuInflater(1354): at android.app.Activity.onCreatePanelMenu(Activity.java:2444)
03-14 13:21:39.583: W/MenuInflater(1354): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:388)
03-14 13:21:39.583: W/MenuInflater(1354): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:739)
03-14 13:21:39.583: W/MenuInflater(1354): at android.app.Activity.invalidateOptionsMenu(Activity.java:2552)
03-14 13:21:39.583: W/MenuInflater(1354): at net.stackueberflow.excusewidget.ExcuseBrowseActivity$ViewPagerAdapter.instantiateItem(ExcuseBrowseActivity.java:224)
03-14 13:21:39.583: W/MenuInflater(1354): at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
03-14 13:21:39.583: W/MenuInflater(1354): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)
03-14 13:21:39.583: W/MenuInflater(1354): at android.support.v4.view.ViewPager.populate(ViewPager.java:783)
03-14 13:21:39.583: W/MenuInflater(1354): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1016)
03-14 13:21:39.583: W/MenuInflater(1354): at android.view.View.measure(View.java:12723)
03-14 13:21:39.583: W/MenuInflater(1354): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
03-14 13:21:39.583: W/MenuInflater(1354): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
03-14 13:21:39.583: W/MenuInflater(1354): at android.view.View.measure(View.java:12723)
03-14 13:21:39.583: W/MenuInflater(1354): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
03-14 13:21:39.583: W/MenuInflater(1354): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
03-14 13:21:39.583: W/MenuInflater(1354): at android.view.View.measure(View.java:12723)
03-14 13:21:39.583: W/MenuInflater(1354): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
03-14 13:21:39.583: W/MenuInflater(1354): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
03-14 13:21:39.583: W/MenuInflater(1354): at android.view.View.measure(View.java:12723)
03-14 13:21:39.583: W/MenuInflater(1354): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
03-14 13:21:39.583: W/MenuInflater(1354): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
03-14 13:21:39.583: W/MenuInflater(1354): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
03-14 13:21:39.583: W/MenuInflater(1354): at android.view.View.measure(View.java:12723)
03-14 13:21:39.583: W/MenuInflater(1354): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
03-14 13:21:39.583: W/MenuInflater(1354): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
03-14 13:21:39.583: W/MenuInflater(1354): at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 13:21:39.583: W/MenuInflater(1354): at android.os.Looper.loop(Looper.java:137)
03-14 13:21:39.583: W/MenuInflater(1354): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-14 13:21:39.583: W/MenuInflater(1354): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 13:21:39.583: W/MenuInflater(1354): at java.lang.reflect.Method.invoke(Method.java:511)
03-14 13:21:39.583: W/MenuInflater(1354): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-14 13:21:39.583: W/MenuInflater(1354): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-14 13:21:39.583: W/MenuInflater(1354): at dalvik.system.NativeStart.main(Native Method)
Метод ViewPagerAdapter.instantiateItem
вызывает invalidateOptionsMenu();
для обновления намерения общего ресурса при выборе нового элемента внутри ViewPagerAdapter.