我试图整合NaviogationView。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NavigationView navigationView = new NavigationView(this);
setContentView(navigationView);
}
但它会抛出java.lang.RuntimeException: Failed to resolve attribute at index 0
之后我尝试在AndroidManifest中添加android:theme="@style/Theme.AppCompat"
<activity android:theme="@style/Theme.AppCompat" android:name="MyActivity"
android:label="@string/app_name">
它并没有崩溃。有没有使用标准Android主题和NavigationView的解决方案?
这是一个完整的堆栈跟踪:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.MyActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class android.support.design.internal.NavigationMenuView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class android.support.design.internal.NavigationMenuView
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.support.design.internal.NavigationMenuPresenter.getMenuView(NavigationMenuPresenter.java:93)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:149)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:88)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:84)
at com.example.myapp.MyActivity.onCreate(MyActivity.java:15)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.support.design.internal.NavigationMenuPresenter.getMenuView(NavigationMenuPresenter.java:93)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:149)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:88)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:84)
at com.example.myapp.MyActivity.onCreate(MyActivity.java:15)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 0
at android.content.res.TypedArray.getDrawable(TypedArray.java:747)
at android.widget.AbsListView.<init>(AbsListView.java:815)
at android.widget.ListView.<init>(ListView.java:151)
at android.widget.ListView.<init>(ListView.java:147)
at android.support.design.internal.NavigationMenuView.<init>(NavigationMenuView.java:39)
at android.support.design.internal.NavigationMenuView.<init>(NavigationMenuView.java:35)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.support.design.internal.NavigationMenuPresenter.getMenuView(NavigationMenuPresenter.java:93)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:149)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:88)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:84)
at com.example.myapp.MyActivity.onCreate(MyActivity.java:15)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:3)
似乎我们是唯一拥有此例外的人。 :)无论如何,有关此错误的一些相关问题。解决方案是使用NavigationView
兼容主题。
<style name="Theme.YourApplication" parent="Theme.AppCompat.Light.NoActionBar">
...
</style>
然后在AndroidManifest中使用该样式:
<application
...
android:theme="@style/Theme.YourApplication">
<activity
答案 1 :(得分:0)
NavigationView旨在将其嵌套在DrawerLayout中。有关一些代码段示例,请参阅此处Android Developers Blog。