我的应用程序使用带有片段的ActionBar选项卡,作为第一步,我仅使用了v4库。但现在我也实现了v7 appcompat。对于API>11的设备,它可以正常工作,因为我只是使用ActivityCompat.invalidateOptionsMenu(this.getActivity());。但对于API<11的设备,每次调用getActivity().supportInvalidateOptionsMenu();时,应用程序似乎会崩溃,并显示inflate异常:android.view.InflateException:Binary XML file line #85:Error inflating class。在dalvikvm中:Could not find class 'android.support.v7.widget.SearchView$5',referenced from method android.support.v7.widget.SearchView.addOnLayoutChangeListenerToDropDownAnchorSDK11。
我在网上搜索了最近三个星期,找到了以下信息:
首先是这个。
虽然这还不足以解决问题,但接着我也发现了这个。
现在我使用的是eclipse,但阅读上面链接中的Issue 67388后,我下载了android studio 0.5.4,以查看它是否适用于我。我需要将Build gradle设置为版本0.9.0,并将android.support.v4/android.support.v7设置为版本19.0.1,因为使用19.1.0时应用程序仍会崩溃。
现在,在API>11的设备上运行良好,例如Android 2.2.3、2.3等,但如果您旋转设备,则会由于NullPointerException而崩溃。
根据日志记录,导致异常的代码是:
mSearchView = new MySearchView(getActivity());
在Android 4.4上运行正常,但在Android 3.0或4.0 / 4.1 / 4.2.1等版本上运行应用程序时,应用程序会再次崩溃,并出现android.view.InflateException: Binary XML file line #85: Error inflating class的错误信息。并且当我旋转设备时也会崩溃。
有没有任何方法可以解决这个问题?如何解决? 我真的不知道该怎么办了。 提前谢谢。
以下是完整的日志:
InflateException
E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #85: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:513)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.support.v7.widget.SearchView.<init>(SearchView.java:262)
at android.support.v7.widget.SearchView.<init>(SearchView.java:254)
at com.samer.deutschwordbookapp.Words$MySearchView.<init>(Words.java:459)
at com.samer.deutschwordbookapp.Words.onCreateOptionsMenu(Words.java:94)
at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:1582)
at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1967)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:225)
at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
at android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:617)
at android.support.v7.app.ActionBarActivityDelegateBase.supportInvalidateOptionsMenu(ActionBarActivityDelegateBase.java:421)
at android.support.v7.app.ActionBarActivity.supportInvalidateOptionsMenu(ActionBarActivity.java:172)
at com.samer.deutschwordbookapp.Words.onPrepareOptionsMenu(Words.java:117)
at android.support.v4.app.Fragment.performPrepareOptionsMenu(Fragment.java:1596)
at android.support.v4.app.FragmentManagerImpl.dispatchPrepareOptionsMenu(FragmentManager.java:1998)
at android.support.v4.app.FragmentActivity.onPreparePanel(FragmentActivity.java:481)
at android.support.v7.app.ActionBarActivity.superOnPreparePanel(ActionBarActivity.java:236)
at android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:644)
at android.support.v7.app.ActionBarActivityDelegateBase.supportInvalidateOptionsMenu(ActionBarActivityDelegateBase.java:421)
at android.support.v7.app.ActionBarActivity.supportInvalidateOptionsMenu(ActionBarActivity.java:172)
at com.samer.deutschwordbookapp.Words.onPrepareOptionsMenu(Words.java:117)
at android.support.v4.app.Fragment.performPrepareOptionsMenu(Fragment.java:1596)
at android.support.v4.app.FragmentManagerImpl.dispatchPrepareOptionsMenu(FragmentManager.java:1998)
at android.support.v4.app.FragmentActivity.onPreparePanel(FragmentActivity.java:481)
at android.support.v7.app.ActionBarActivity.superOnPreparePanel(ActionBarActivity.java:236)
at android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:644)
at android.support.v7.app.ActionBarActivityDelegateBase.supportInvalidateOptionsMenu(ActionBarActivityDelegateBase.java:421)
at android.support.v7.app.ActionBarActivity.supportInvalidateOptionsMenu(ActionBarActivity.java:172)
at com.samer.deutschwordbookapp.Words.onPrepareOptionsMenu(Words.java:117)
at android.support.v4.app.Fragment.performPrepareOptionsMenu(Fragment.java:1596)
at android.support.v4.app.FragmentManagerImpl.dispatchPrepareOptionsMenu(FragmentManager.java:1998)
at android.support.v4.app.FragmentActivity.onPreparePanel(FragmentActivity.java:481)
at android.support.v7.app.ActionBarActivity.superOnPreparePanel(ActionBarActivity.java:236)
at android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:644)
at android.support.v7.app.ActionBarActivityDelegateBase.supportInvalidateOptionsMenu(ActionBarActivityDelegateBase.java:421)
at android.support.v7.app.ActionBarActivity.supportInvalidateOptionsMenu(ActionBarActivity.java:172)
at com.samer.deutschwordbookapp.Words.onPrepareOptionsMenu(Words.java:117)
at android.support.v4.app.Fragment.performPrepareOptionsMenu(Fragment.java:1596)
空指针异常
。E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at android.view.ViewConfiguration.get(ViewConfiguration.java:211)
at android.view.View.<init>(View.java:1814)
at android.view.View.<init>(View.java:1856)
at android.view.View.<init>(View.java:1834)
at android.view.ViewGroup.<init>(ViewGroup.java:285)
at android.widget.LinearLayout.<init>(LinearLayout.java:115)
at android.support.v7.widget.SearchView.<init>(SearchView.java:258)
at android.support.v7.widget.SearchView.<init>(SearchView.java:254)
at com.samer.deutschwordbookapp.Expressions$MySearchView.<init>(Expressions.java:432)
at com.samer.deutschwordbookapp.Expressions.onCreateOptionsMenu(Expressions.java:89)
at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:1582)
at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1956)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:225)
at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
at android.support.v7.app.ActionBarActivityDelegateBase$1.run(ActionBarActivityDelegateBase.java:70)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)