Android抽屉布局上的侧边栏导航标题

时间:2017-12-21 05:46:19

标签: android android-layout drawerlayout

我正在我的项目中构建侧边栏导航我在LinearLayout中创建了LinearLayout的横幅。如果我删除了LinearLayout并将RealtiveLayout放到应用程序正常工作。

以下是我的代码的完整堆栈跟踪:

请帮我摆脱这个错误
        E/AndroidRuntime: FATAL EXCEPTION: main
                          Process: com.example.lenovo.jdstudio, PID: 7912
                          java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lenovo.jdstudio/com.example.lenovo.jdstudio.MainActivity}: android.view.InflateException: Binary XML file line #0: Error inflating class android.support.design.widget.NavigationView
                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
                              at android.app.ActivityThread.access$800(ActivityThread.java:151)
                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
                              at android.os.Handler.dispatchMessage(Handler.java:110)
                              at android.os.Looper.loop(Looper.java:193)
                              at android.app.ActivityThread.main(ActivityThread.java:5333)
                              at java.lang.reflect.Method.invokeNative(Native Method)
                              at java.lang.reflect.Method.invoke(Method.java:515)
                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
                              at dalvik.system.NativeStart.main(Native Method)
                           Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class android.support.design.widget.NavigationView
                              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 android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
                              at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                              at com.example.lenovo.jdstudio.MainActivity.onCreate(MainActivity.java:16)
                              at android.app.Activity.performCreate(Activity.java:5343)
                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429) 
                              at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342) 
                              at android.os.Handler.dispatchMessage(Handler.java:110) 
                              at android.os.Looper.loop(Looper.java:193) 
                              at android.app.ActivityThread.main(ActivityThread.java:5333) 
                              at java.lang.reflect.Method.invokeNative(Native Method) 
                              at java.lang.reflect.Method.invoke(Method.java:515) 
                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 
                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
                              at dalvik.system.NativeStart.main(Native Method) 
                           Caused by: java.lang.reflect.InvocationTargetException
                              at java.lang.reflect.Constructor.constructNative(Native Method)
                              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 android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
                              at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
                              at com.example.lenovo.jdstudio.MainActivity.onCreate(MainActivity.java:16) 
                              at android.app.Activity.performCreate(Activity.java:5343) 
                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) 
                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331) 
                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429) 
                              at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342) 
                              at android.os.Handler.dispatchMessage(Handler.java:110) 
                              at android.os.Looper.loop(Looper.java:193) 
                              at android.app.ActivityThread.main(ActivityThread.java:5333) 
                              at java.lang.reflect.Method.invokeNative(Native Method) 
                              at java.lang.reflect.Method.invoke(Method.java:515) 
                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 
                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
                              at dalvik.system.NativeStart.main(Native Method) 
                           Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x12
                              at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:464)
                              at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:6402)
                              at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:2048)
                              at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1960)
                              at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:61)
                              at android.view.LayoutInflater.rInflate(LayoutInflater.java:757)
                              at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
                              at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                              at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                              at android.support.design.internal.NavigationMenuPresenter.inflateHeaderView(NavigationMenuPresenter.java:211)
                              at android.support.design.widget.NavigationView.inflateHeaderView(NavigationView.java:267)
                              at android.support.design.widget.NavigationView.<init>(NavigationView.java:176)
                              at android.support.design.widget.NavigationView.<init>(NavigationView.java:98)
                              at java.lang.reflect.Constructor.constructNative(Native Method) 
                              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 android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
                              at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
                              at com.example.lenovo.jdstudio.MainActivity.onCreate(MainActivity.java:16) 
                              at android.app.Activity.performCreate(Activity.java:5343) 
                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) 
                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331) 
                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429) 
                              at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342) 
                              at android.os.Handler.dispatchMessage(Handler.java:110) 
                              at android.os.Looper.loop(Looper.java:193) 
                              at android.app.ActivityThread.main(ActivityThread.java:5333) 
                              at java.lang.reflect.Method.invokeNative(Native Method) 
                              at java.lang.reflect.Method.invoke(Method.java:515) 
                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 
                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
                              at dalvik.system.NativeStart.main(Native Method) 
        D/dalvikvm: threadid=11: exiting
        D/dalvikvm: threadid=11: bye!
        Application terminated.

以下是我的navigation_header.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary">
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="20dp">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:layout_marginRight="@id/textView"
        app:srcCompat="@mipmap/ic_home_white_24dp" />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:layout_toEndOf="@+id/imageView2"
        android:text="Home"
        android:textAlignment="center"
        android:textColor="#ffffff"
        android:textSize="17sp" />

</LinearLayout>
</LinearLayout>

以下是我的MainActivity.java

package com.example.lenovo.jdstudio;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout ;
    private ActionBarDrawerToggle mToggle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mDrawerLayout =(DrawerLayout) findViewById(R.id.drawer_layout);
        mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);

        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(mToggle.onOptionsItemSelected(item))
        {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

2 个答案:

答案 0 :(得分:0)

你已经在边缘推出了视图ID 将dp放在保证金上可能会解决您的问题

答案 1 :(得分:0)

尝试从您的android:layout_marginRight="@id/textView"移除 ImageView 并在 DP

中应用保证金
<ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:layout_marginRight="20dp"
        app:srcCompat="@mipmap/ic_home_white_24dp" />