运行时异常:错误膨胀类android.support.v4.widget.DrawerLayout

时间:2014-10-17 09:00:32

标签: android jar navigation-drawer executable-jar

我得到一个错误的膨胀类android.support.v4.widget.DrawerLayout。我在下面的编码和相关的截图中发布了详细描述。

堆栈跟踪:

11-04 07:35:04.404: E/AndroidRuntime(1427): FATAL EXCEPTION: main
11-04 07:35:04.404: E/AndroidRuntime(1427): Process: com.example.steve, PID: 1427
11-04 07:35:04.404: E/AndroidRuntime(1427): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.steve/com.example.steve.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v4.widget.DrawerLayout
11-04 07:35:04.404: E/AndroidRuntime(1427):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at android.os.Looper.loop(Looper.java:136)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at android.app.ActivityThread.main(ActivityThread.java:5017)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at java.lang.reflect.Method.invokeNative(Native Method)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at java.lang.reflect.Method.invoke(Method.java:515)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at dalvik.system.NativeStart.main(Native Method)
11-04 07:35:04.404: E/AndroidRuntime(1427): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v4.widget.DrawerLayout
11-04 07:35:04.404: E/AndroidRuntime(1427):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at android.app.Activity.setContentView(Activity.java:1929)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at com.qrme.quranmadeeasy.ChapterActivity.onCreate(ChapterActivity.java:91)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at android.app.Activity.performCreate(Activity.java:5231)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
11-04 07:35:04.404: E/AndroidRuntime(1427):     ... 11 more
11-04 07:35:04.404: E/AndroidRuntime(1427): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.widget.DrawerLayout" on path: DexPathList[[zip file "/data/app/com.qrme.quranmadeeasy-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.qrme.quranmadeeasy-2, /system/lib]]
11-04 07:35:04.404: E/AndroidRuntime(1427):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at android.view.LayoutInflater.createView(LayoutInflater.java:559)
11-04 07:35:04.404: E/AndroidRuntime(1427):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
11-04 07:35:04.404: E/AndroidRuntime(1427):     ... 20 more

ChapterActivity.java:

package com.example.steve;

import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;

public class MainActivity extends Activity implements OnItemClickListener {

RelativeLayout relativelayoutContainer;
    TextView txtHeading;
    ListView listChapter;
    ListView listSlider;
    ImageView imgBanner;

@SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.activity_main); --->Error line no.91
        initialize();
     }
     private void initialize() {


    relativelayoutContainer = (RelativeLayout) findViewById(R.id.container);
    txtHeading = (TextView) findViewById(R.id.textviewHeading);
    listChapter = (ListView) findViewById(R.id.listviewChapters);
    imgBanner = (ImageView) findViewById(R.id.imageviewBanner);
    listSlider = (ListView) findViewById(R.id.list_slidermenu);

    // Setting font color and size
    Resources resources = ChapterActivity.this.getResources();
     }

    private void displayView(int position) {
        // update the main content by replacing fragments
        Fragment fragment = null;
        switch (position) {
        case 0:
            fragment = new HomeFragment();
            break;
        case 1:
            fragment = new FindPeopleFragment();
            break;
        case 2:
            fragment = new PhotosFragment();
            break;

        default:
            break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment).commit();

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            setTitle(navMenuTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            // error in creating fragment
            Log.e("MainActivity", "Error in creating fragment");
        }
    }
  }

activity_main.xml:

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

     <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <RelativeLayout
        android:id="@+id/container"
        android:layout_width="match_parent"layout_width="wrap_content"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/textviewHeading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="5dp"
            android:fontFamily="fonts/Dosis.otf"
            android:text="@string/heading_chapter"
            android:textSize="25sp"
            android:visibility="gone" />

        <ListView
            android:id="@+id/listviewChapters"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@+id/imageviewBanner"
            android:layout_below="@+id/textviewHeading"
            android:layout_marginTop="5dp" />
        </ListView>

        <ImageView
            android:id="@+id/imageviewBanner"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/banner"
            android:visibility="invisible" />
    </RelativeLayout>


    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
    android:background="@color/list_background"
    android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"        
        android:listSelector="@drawable/list_selector"
        android:background="@color/list_background"/>

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

如果我点击图形布局,我会收到以下异常:

enter image description here

Exception raised during rendering: com.android.layoutlib.bridge.MockView cannot be cast to android.view.ViewGroup
Exception details are logged in Window > Show View > Error Log
The following classes could not be instantiated:
- android.support.v4.widget.DrawerLayout (Open Class, Show Error Log)

然后在 Java构建路径 - &gt;

enter image description here

然后在 Java构建路径 - &gt; 订单并导出

enter image description here

在上面的屏幕截图中,如果我check android.support.v4.jar我在控制台中收到以下多个Dex错误:

[2014-11-04 18:35:30 - Dex Loader] Unable to execute dex: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl;
[2014-11-04 18:35:30 - main-android] Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl;

enter image description here

5 个答案:

答案 0 :(得分:11)

只需检查Android版本和支持lib复选框,它就可以解决您的问题。

答案 1 :(得分:0)

如果我理解正确,您只需在ListView中选择选项时直接调用新活动(并完全省略GalleryFragment)。类似的东西:

private void displayView(int position) {
    if (position == 0) {
        Intent i = new Intent(getActivity(), GalleryActivity.class);
        startActivity(i);
        return
    }

    // ... same as before, for the other fragments

单击抽屉选项时,不必显示片段。

答案 2 :(得分:0)

1.)If it is showing multiple dex file, then remove v4 library 

2.)Show us which project you are adding as a library in this project and in that check the v4 library

3.)Clean the project from Project -> clean and run the project

答案 3 :(得分:0)

用户@Dev建议我如何使用抽屉布局以及这些错误发生在哪里。所以我能够解决这个问题。

运行时异常:错误导致类android.support.v4.widget.DrawerLayout错误

  • 由于抽屉的错误实施而发生此错误 布局。我把一个三个直接的孩子放到Drawer布局错误 会发生的。根据DrawerLayout,它花了两个 孩子直接。

  • 在抽屉布局中,如果我放置两个直接布局解决了问题。 这就是我将Frame Layout用作父布局的原因 抽屉布局最后在框架布局后添加列表视图以显示导航 抽屉。

<强> activity_main.xml中:

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

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <RelativeLayout
            android:id="@+id/container"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" >

            <TextView
                android:id="@+id/textviewHeading"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="5dp"
                android:fontFamily="fonts/Dosis.otf"
                android:text="@string/heading_chapter"
                android:textSize="25sp"
                android:visibility="gone" />

            <ListView
                android:id="@+id/listviewChapters"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_above="@+id/imageviewBanner"
                android:layout_below="@+id/textviewHeading"
                android:layout_marginTop="5dp" >
            </ListView>

            <ImageView
                android:id="@+id/imageviewBanner"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:src="@drawable/banner"
                android:visibility="invisible" />
        </RelativeLayout>
    </FrameLayout>

    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/list_background"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />

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

无法执行dex:多个dex文件定义

  • 由于android-support-v4.jar.This jar发生了这些错误 在我的项目中没有更新。它的大小接近325 kb。所以我 实现从其他示例应用程序检查jar。在那里面 样品应用罐的大小将是530 kb。然后我找到它,qmr 项目jar是一个错误。它不是一个更新的jar。所以我删除了 jar放在qmr中并添加了示例项目jar。然后它解决了我的问题 问题

  • 如果我在这里放一个罐子,就会发生错误 目录(E:\Android Eclipse\Android\sdk\extras\android\support\v4)。这个目录是 适用于添加罐子。但不是在我的项目中。

  • 因为像gson.jar这样的其他jar,libgoogleanalytics.jar都有 E:\workspace\Workspace1\sample-android\libs.中的目录所以在我的项目中,在同一目录中添加一个jar来解决我的问题

答案 4 :(得分:0)

是的,我已经解决了你的问题 只是

Right click on Project
Build Path -> Configure Build Path
add your external support v4 
then check the support v4 from order and export

Make sure your Android and Android Dependencies libraries are checked
Clean & Build your Project

我认为这将解决您的问题