修改Material Design库的版本导致TabLayout无法使用的问题解决
主要错误:
1.java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chagu.quhecha/com.chagu.quhecha.activity.TeaChatActivity}:android.view.InflateException: Binary XML file line #75: Error inflating class android.support.design.widget.TabLayout
2.Caused by: android.view.InflateException: Binary XML file line #75: Error inflating class android.support.design.widget.TabLayout
3.Caused by: java.lang.reflect.InvocationTargetException
4.Caused by: java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) withthe design library.
完整错误:
03-09 11:12:38.231 20555-20555/com.chagu.quhecha E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.chagu.quhecha, PID: 20555
java.lang.RuntimeException: Unableto start activity ComponentInfo{com.chagu.quhecha/com.chagu.quhecha.activity.TeaChatActivity}: android.view.InflateException: Binary XML file line #75: Error inflating class android.support.design.widget.TabLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2526)
at android.app.ActivityThread.access$800(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:222)
at android.app.ActivityThread.main(ActivityThread.java:5484)
at java.lang.reflect.Method.invokeNative(NativeMethod)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:676)
at dalvik.system.NativeStart.main(NativeMethod)
Caused by: android.view.InflateException:Binary XML file line #75: Error inflating class android.support.design.widget.TabLayout
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:347)
at android.app.Activity.setContentView(Activity.java:1993)
at com.chagu.quhecha.activity.TeaChatActivity.setContentView(TeaChatActivity.java:88)
at com.chagu.quhecha.common.BaseFragmentActivity.onCreate(BaseFragmentActivity.java:48)
at android.app.Activity.performCreate(Activity.java:5463)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2418)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2526)
at android.app.ActivityThread.access$800(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:222)
at android.app.ActivityThread.main(ActivityThread.java:5484)
at java.lang.reflect.Method.invokeNative(NativeMethod)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:676)
at dalvik.system.NativeStart.main(NativeMethod)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(NativeMethod)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:347)
at android.app.Activity.setContentView(Activity.java:1993)
at com.chagu.quhecha.activity.TeaChatActivity.setContentView(TeaChatActivity.java:88)
at com.chagu.quhecha.common.BaseFragmentActivity.onCreate(BaseFragmentActivity.java:48)
at android.app.Activity.performCreate(Activity.java:5463)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2418)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2526)
at android.app.ActivityThread.access$800(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:222)
at android.app.ActivityThread.main(ActivityThread.java:5484)
at java.lang.reflect.Method.invokeNative(NativeMethod)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:676)
at dalvik.system.NativeStart.main(NativeMethod)
Caused by: java.lang.IllegalArgumentException:You need to use a Theme.AppCompat theme (or descendant) with the design library.
at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:34)
at android.support.design.widget.TabLayout.
at android.support.design.widget.TabLayout.
at java.lang.reflect.Constructor.constructNative(NativeMethod)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:347)
at android.app.Activity.setContentView(Activity.java:1993)
at com.chagu.quhecha.activity.TeaChatActivity.setContentView(TeaChatActivity.java:88)
at com.chagu.quhecha.common.BaseFragmentActivity.onCreate(BaseFragmentActivity.java:48)
at android.app.Activity.performCreate(Activity.java:5463)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2418)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2526)
at android.app.ActivityThread.access$800(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:222)
at android.app.ActivityThread.main(ActivityThread.java:5484)
at java.lang.reflect.Method.invokeNative(NativeMethod)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:676)
at dalvik.system.NativeStart.main(Native Method)
分析:
因为项目中使用了Viewpager+fragment,需要传进去v4包下的FragmentManager(getSupportFragmentManager()
),所以activity继承的FragmentActivity
,而
,之前Material Design库的版本是23.0.1,修改成23.4.0之后,因为activity使用的主题不是AppCompat下的主题,所以报错了,至于为什么版本是23.0.1的时候不用AppCompat的主题也能使用,这个就不是很清楚了。
解决:
所以现在我们需要使用AppCompat的主题,单独为继承FragmentActivity的activity单独设置主题,在style里定义一个parent是AppCompat
主题的style,在activity的onCreate()方法里调用setTheme(R.style.XX),记得要在setContentView()之前。