非法参数异常 - 必须使用MeasureSpec.EXACTLY测量DrawerLayout
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了非法参数异常 - 必须使用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没有指出错误行。我已经发布了相关的代码。
我在DrawerLayout上获得非法参数异常必须使用MeasureSpec.Exactly在运行时测量。
base activity.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>
我不知道如何解决这个问题。任何人都可以帮助我。谢谢。
错误:
非法参数异常 - 必须使用MeasureSpec.EXACTLY测量DrawerLayout
从错误日志我可以建议创建扩展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
。
编辑:
base activity.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);
我尝试了很多解决方案没有任何效果。很长一段时间后,我找到了自己的解决方案。
我使用线性布局作为我的膨胀类的父级我刚刚用相对布局替换
之后一切都运行正常。为了您更好的理解,下面给出的是我目前用于充气课程的代码
FrameLayout layoutContainer = (FrameLayout ) findViewById(R.id.container);
LayoutInflater layoutInflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutContainer.addView(layoutInflater.inflate(R.layout.ecom_home_activity, null));
并且ecom_home_activity正在膨胀类。代码如下。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/gray_background_color"
android:fillViewport="true"
android:orientation="vertical"
android:scrollbars="none">
<RelativeLayout
android:id="@+id/no_data_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cc000000"
android:gravity="center">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminate="true"
android:indeterminateTint="@color/offer_activity_background"
android:indeterminateTintMode="src_atop"
tools:ignore="UnusedAttribute" />
<TextView
android:id="@+id/noDataHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBar"
android:layout_centerInParent="true"
android:gravity="center"
android:text="@string/offerlist_please_wait"
android:textColor="@color/white_color"
android:textSize="@dimen/twenty_six_text_size" />
<TextView
android:id="@+id/noDataText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/noDataHeading"
android:layout_centerInParent="true"
android:gravity="center"
android:text="@string/offerlist_while_we_are"
android:textColor="@color/white_color"
android:textSize="@dimen/fourteen_size_text" />
</RelativeLayout>
<ImageView
android:id="@+id/sleeping_bird"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="centerInside" />
<TextView
android:id="@+id/textView_taking_nap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/sleeping_bird"
android:layout_marginTop="@dimen/five_dp_margin_padding"
android:gravity="center"
android:text="@string/offerlist_coucou_taking_nap"
android:textSize="@dimen/sixteen_size_text_heading"
android:visibility="gone" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerListOfferProducts"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:footerDividersEnabled="false"
android:gravity="center"
android:headerDividersEnabled="false"
android:paddingBottom="@dimen/five_dp_margin_padding"
android:paddingLeft="@dimen/eight_dp_margin_padding"
android:paddingRight="@dimen/eight_dp_margin_padding"
android:paddingTop="@dimen/eight_dp_margin_padding"
android:scrollbars="none" />
以前我使用的是ecom_home_activity类的线性布局我只是改变了相对布局的线性布局
在那之后一切正常。在你的情况下如果你使用膨胀类的线性布局然后用相对布局替换。我的答案将帮助你欢呼。
将layout_width和layout_height设置为特定大小,而不是match_parent或fill_parent。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".UserListActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:background="@drawable/common_gradient"
android:layoutDirection="rtl"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2">
<TextView
android:id="@+id/userType_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="نوع المستخدم"
android:textColor="#000000"
android:textSize="20sp"
tools:text="نوع المستخدم" />
<TextView
android:id="@+id/className_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/userType_textView"
android:layout_centerHorizontal="true"
android:text="إسم القسم"
android:textColor="#000000"
android:textSize="16sp"
tools:text="إسم القسم" />
<ImageButton
android:layout_width="30dp"
android:layout_height="20dp"
android:layout_alignBottom="@+id/userType_textView"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:background="@android:color/transparent"
android:contentDescription="@string/desc"
android:onClick="showMenuAction"
android:scaleType="fitCenter"
android:src="@drawable/menu" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.以上是关于非法参数异常 - 必须使用MeasureSpec.EXACTLY测量DrawerLayout的主要内容,如果未能解决你的问题,请参考以下文章
在 PySpark mllib 中使用随机森林的非法参数异常
eclipse 3.5 中的 FreeMarker 编辑器给出非法参数异常
mediaMetadataRetriever.setDataSource(getBaseContext(),uri) 抛出非法参数异常