gpt4 book ai didi

android - 非法参数异常 - DrawerLayout 必须使用 MeasureSpec.EXACTLY 进行测量

转载 作者:行者123 更新时间:2023-11-29 14:24:19 26 4
gpt4 key购买 nike

堆栈跟踪:

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没有指出错误行,我贴出相关代码

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

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>

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

最佳答案

错误:

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

根据错误日志,我可以建议创建自定义类来扩展 DrawerLayout 并强制执行正确的 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

编辑:

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);

关于android - 非法参数异常 - DrawerLayout 必须使用 MeasureSpec.EXACTLY 进行测量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32515527/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com