不能在Android中使用android.support.v7.widget.DrawerLayout

问题描述 投票:3回答:2

我是Android的绝对初学者。我开始学习如何在android中使用动作栏与导航抽屉。我已经在xml文件中使用android.support.v4.widget.DrawerLayout布局创建了一个简单的应用程序。

它工作正常。但是当我使用android.support.v7.widget.DrawerLayout时,它会抛出错误。但是当我把它改回android.support.v4.widget.DrawerLayout时,它运行正常。

这是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- This LinearLayout represents the contents of the screen  -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- The ActionBar displayed at the top -->
        <include
            layout="@layout/action_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <!-- The main content view where fragments are loaded -->
        <FrameLayout
            android:id="@+id/main_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

    <!-- The navigation drawer that comes from the left -->
    <!-- Note that `android:layout_gravity` needs to be set to 'start' -->
    <android.support.design.widget.NavigationView
        android:id="@+id/nvView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/white"/>
        <!--app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_view" />-->

</android.support.v7.widget.DrawerLayout> 

这是action_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">

</android.support.v7.widget.Toolbar>

这是我的MainActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这是我运行它时logcat中的错误

01-29 11:22:43.026 20642-20642/? D/dalvikvm: Late-enabling CheckJNI
01-29 11:22:43.118 20642-20642/com.blog.waiyanhein.llks.llks W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
01-29 11:22:43.122 20642-20642/com.blog.waiyanhein.llks.llks I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
01-29 11:22:43.122 20642-20642/com.blog.waiyanhein.llks.llks W/dalvikvm: VFY: unable to resolve interface method 17962: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
01-29 11:22:43.122 20642-20642/com.blog.waiyanhein.llks.llks D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
01-29 11:22:43.122 20642-20642/com.blog.waiyanhein.llks.llks I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
01-29 11:22:43.122 20642-20642/com.blog.waiyanhein.llks.llks W/dalvikvm: VFY: unable to resolve interface method 17966: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
01-29 11:22:43.122 20642-20642/com.blog.waiyanhein.llks.llks D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
01-29 11:22:43.190 20642-20642/com.blog.waiyanhein.llks.llks D/AndroidRuntime: Shutting down VM
01-29 11:22:43.190 20642-20642/com.blog.waiyanhein.llks.llks W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa617d908)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime: FATAL EXCEPTION: main
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blog.waiyanhein.llks.llks/com.blog.waiyanhein.llks.llks.MainActivity}: android.view.InflateException: Binary XML file line #3: Error inflating class android.support.v7.widget.DrawerLayout
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5041)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:  Caused by: android.view.InflateException: Binary XML file line #3: Error inflating class android.support.v7.widget.DrawerLayout
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at com.blog.waiyanhein.llks.llks.MainActivity.onCreate(MainActivity.java:15)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5104)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:141) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5041) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:  Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.widget.DrawerLayout" on path: /data/app/com.blog.waiyanhein.llks.llks-2.apk
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.LayoutInflater.createView(LayoutInflater.java:552)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at com.blog.waiyanhein.llks.llks.MainActivity.onCreate(MainActivity.java:15) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5104) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:141) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5041) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
01-29 11:22:43.210 20642-20642/com.blog.waiyanhein.llks.llks E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 

我的代码出了什么问题?

android android-navigation-drawer
2个回答
4
投票

你得到一个ClassNotFoundException,因为v7 appcompat库中没有DrawerLayout类。

他们没有更新这个特定的小部件,大概是因为在功能上没有任何改进,也没有任何需要改造的视觉效果,因为它只是一个空的透明布局。

更新的其他组件 - 例如ActionBarDrawerToggle类 - 旨在与DrawerLayout的v4支持库版本一起使用,事实上,v7库依赖于v4。

TL; DR - 坚持使用v4 DrawerLayout


0
投票

而不是使用v7使用v4。改变

android.support.v7.widget.DrawerLayout

android.support.v4.widget.DrawerLayout

将解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.