非法参数异常-必须使用MeasureSpec.EXACTLY测量DrawerLayout [英] Illegal Argument Exception - DrawerLayout must be measured with MeasureSpec.EXACTLY

查看:105
本文介绍了非法参数异常-必须使用MeasureSpec.EXACTLY测量DrawerLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

堆栈跟踪:

09-10 07:56:56.448  24867-24867/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalArgumentException: DrawerLayout must be measured with MeasureSpec.EXACTLY.
            at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:814)
            at android.view.View.measure(View.java:15848)
            at com.android.internal.widget.ActionBarView.onMeasure(ActionBarView.java:1098)
            at android.view.View.measure(View.java:15848)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.widget.ActionBarContainer.onMeasure(ActionBarContainer.java:271)
            at android.view.View.measure(View.java:15848)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
            at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
            at android.view.View.measure(View.java:15848)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
            at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:229)
            at android.view.View.measure(View.java:15848)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
            at android.view.View.measure(View.java:15848)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
            at android.view.Choreographer.doCallbacks(Choreographer.java:562)
            at android.view.Choreographer.doFrame(Choreographer.java:532)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

Logcat没有指出错误行.我已经发布了相关代码.

Logcat doesn't pointed out the error line.I have posted the relevant code.

我在DrawerLayout上收到非法参数异常,必须在运行时使用MeasureSpec进行测量.

I'm getting illegal argument exception at DrawerLayout must be measured with MeasureSpec.Exactly at runtime.

BaseActivity.java:

  private ListView mDrawerList;
  private DrawerLayout mDrawerLayout;
  private ArrayList<Chapter> chapterArray;
  ChapterListAdapter adapter;
  SqliteDbHelper dbHelper;


  dbHelper = new SqliteDbHelper(this);

  dbHelper.openDataBase();

  LayoutInflater mInflater = (LayoutInflater) getApplicationContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null); 

  mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  mDrawerList = (ListView) findViewById(R.id.list_menu);

  chapterArray=new ArrayList<Chapter>();
  chapterArray=dbHelper.getChapterDetails();

  adapter = new ChapterListAdapter(this,chapterArray);
  mDrawerList.setAdapter(adapter);

custom_actionbar.xml:

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#5467F7">

        <TextView
            android:id="@+id/title_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:text="Chapters"
            android:textColor="#FFFFFF"
            android:textStyle="bold" />

    </RelativeLayout>

    <ListView
        android:id="@+id/list_menu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#E36666"
        android:choiceMode="singleChoice"
        android:dividerHeight="1dp"
        />

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

我不知道如何解决这个问题.任何人都可以帮助我.谢谢.

I dont know how to solve this one.Anyone can help me with this.Thank You.

推荐答案

错误:

非法参数异常-DrawerLayout必须使用 MeasureSpec.EXACTLY

Illegal Argument Exception - DrawerLayout must be measured with MeasureSpec.EXACTLY

从错误日志中,我建议创建一个扩展DrawerLayout并强制正确的Measure_Specs的自定义类:

From error log i can suggest to create custom class that extends DrawerLayout and forcing the correct Measure_Specs:

public class MyDrawerLayout extends DrawerLayout {

    public MyDrawerLayout(Context context) {
        super(context);
    }

    public MyDrawerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        widthMeasureSpec = MeasureSpec.makeMeasureSpec(
                MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(
                MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

}

或作为一种临时的解决方法,尝试将layout_width和/或layout_height设置为特定的大小(例如500dp)而不是match_parent.

or As a temporary workaround try to set layout_width and/or layout_height to a specific size (e.g. 500dp) instead of match_parent.

BaseActivity.java

import com.steve.database.MyDrawerLayout;

private MyDrawerLayout mDrawerLayout;
private ListView mDrawerList;


LayoutInflater mInflater = (LayoutInflater) getApplicationContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);

mDrawerLayout = (MyDrawerLayout)mCustomView.findViewById(R.id.drawer_layout);
mDrawerList = (ListView)mCustomView.findViewById(R.id.list_menu);

这篇关于非法参数异常-必须使用MeasureSpec.EXACTLY测量DrawerLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆