当方向更改为横向时,MenuInflater会抛出ClassNotFoundException

时间:2012-03-14 12:36:01

标签: android menu orientation

我在纵向模式下使用了一款完全正常运行的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中的新项目时更新共享意图。

1 个答案:

答案 0 :(得分:0)

试试这个 清单中的这个

android:configChanges="orientation|keyboardHidden"></activity>

这在你的班级

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}

因为“dvmFindClassByName拒绝''”消息由

显示
if (!validateClassName(name)) {
    LOGW("dvmFindClassByName rejecting '%s'\n", name);
    dvmThrowException("Ljava/lang/ClassNotFoundException;", name);
    goto bail;
}

这个函数......并且在更改方向时它是一个空字符串''作为'name'的参数...我认为这是因为活动正在重新启动并且其上下文不可用于菜单inflater ...所以试试这个......