开发者

SlidingDrawer cannot have UNSPECIFIED dimensions

Hi I am getting a run time exception:

SlidingDrawer cannot have UNSPECIFIED dimensions

From the below XML, Can any help on this.

 <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scroll" android:layout_width="fill_parent" android:layout_height="fill_parent"  android:fillViewport="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:padding="10px" android:background="@drawable/formbg">


 <LinearLayout android:id="@+id/linearLayout1" 
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:orientation="horizontal" android:layout_alignParentTop="true" 
        android:layout_alignParentLeft="true" android:layout_alignParentRight="true">

    <TextView android:id="@+id/label" 
              android:layout_width="120px" 
              android:layout_height="wrap_content" 
              android:text="Question Title" 
              android:textStyle="bold"
              android:textSize="12sp" 
              android:textColor="#000000" 
    android:paddingLeft="5px"
    android:paddingRight="5px"
    android:paddingTop="10px"
    android:paddingBottom="10px"
              />

    <EditText android:id="@+id/entry" 
              android:layout_width="match_parent" 
              android:layout_height="wrap_content" 
              android:textColor="#000000"   
    android:paddingLeft="10px"
    android:paddingRight="5px"
    android:paddingTop="10px"
    android:paddingBottom="10px"
              />

 </LinearLayout>


   <LinearLayout android:id="@+id/linearLayout2"
        android:layout_below="@id/linearLayout1" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:orientation="horizontal"
        android:layout_alignParentLeft="true" android:layout_alignParentRight="true"  android:paddingRight="0px" > 

   <TextView android:id="@+id/pass" 
              android:layout_width="120px" 
              android:layout_height="wrap_content" 
              android:text="Type Description" 
              android:textStyle="bold"
              android:textSize="12sp" 
              android:textColor="#000000" 
    android:paddingLeft="5px"
    android:paddingRight="5px"
    android:paddingTop="10px"
    android:paddingBottom="10px"
    />

    <EditText android:id="@+id/passentry" 
              android:layout_width="match_parent" 
              android:layout_height="100px" 
              android:textColor="#000000"   
    android:paddingLeft="10px"
    android:paddingRight="5px"
    android:paddingTop="10px"
    android:paddingBottom="10px"
              />

   </LinearLayout>


   <LinearLayout android:id="@+id/linearLayout3"
        android:layout_below="@id/linearLayout2" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:orientation="horizontal"
        android:layout_alignParentLeft="true" android:layout_alignParentRight="true">

    <TextView android:id="@+id/labelbounty" 
              android:layout_width="120px" 
              android:layout_height="wrap_content" 
              android:text="Enter Amount  $" 
              android:textStyle="bold"
              android:textSize="12sp" 
              android:textColor="#000000" 
    android:paddingLeft="5px"
    android:paddingRight="5px"
    android:paddingTop="10px"
    android:paddingBottom="10px" 
               />

     <EditText android:id="@+id/bounty" 
              android:numeric="decimal"
              android:layout_width="match_parent" 
              android:layout_height="wrap_content" 
              android:textColor="#000000" 
    android:paddingLeft="10px"
    android:paddingRight="5px"
    android:paddingTop="10px"
    android:paddingBottom="10px"
               />          


   </LinearLayout>


    <LinearLayout android:id="@+id/linearLayout4"
        android:layout_below="@id/linearLayout3" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:orientation="horizontal"
        android:layout_alignParentLeft="true" android:layout_alignParentRight="true">



   <TextView android:id="@+id/duedatelabel" 
              android:layout_width="120px" 
              android:layout_height="wrap_content" 
              android:text="Due Date" 
              android:textStyle="bold"
              android:textSize="12sp" 
              android:textColor="#000000" 
    android:paddingLeft="10px"
    android:paddingRight="5px"
    android:paddingTop="10px"
    android:paddingBottom="10px" />

  <Button android:id="@+id/pickDate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000" 
   android:paddingLeft="10px"
    android:paddingRight="5px"
    android:paddingTop="10px"
    android:paddingBottom="10px"
        android:text="Select Due Date"/>

   </LinearLayout>

    <LinearLayout android:id="@+id/linearLayout5"
        android:layout_below="@id/linearLayout4" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:orientation="horizontal"
        android:layout_alignParentLeft="true" android:layout_alignParentRight="true">


     <TextView android:id="@+id/labelprimary" 
              android:layout_width="120px" 
              android:layout_height="wrap_content" 
              android:text="Category" 
              android:textStyle="bold"
              android:textSize="12sp" 
              android:textColor="#000000" 
    android:paddingLeft="10px"
    android:paddingRight="5px"
    android:paddingTop="10px"
    android:paddingBottom="10px"
               />

    <Spinner 
        android:id="@+id/primarycategory"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/planet_prompt"
        android:textColor="#000000"
        android:paddingLeft="15px"
    android:paddingRight="5px"
    android:paddingTop="10px"
    android:paddingBottom="10px" 
    /> 
    </LinearLayout>


    <LinearLayout android:id="@+id/linearLayout6"
        android:layout_below="@id/linearLayout5" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:orientation="horizontal"
        android:layout_alignParentLeft="true" android:layout_alignParentRight="true">

     <TextView android:id="@+id/labelsecondary" 
              android:layout_width="120px" 
              android:layout_height="wrap_content" 
              android:text="Sub Category" 
              android:textStyle="bold"
              android:textSize="12sp" 
              android:textColor="#000000" 
     android:paddingLeft="10px"
    android:paddingRight="5px"
    android:paddingTop="10px"
    android:paddingBottom="10px"
               />

    <Spinner 
        android:id="@+id/secondarycategory"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/secondary_category"
        android:textColor="#000000" 
    android:paddingLeft="15px"
    android:paddingRight="5px"
    android:paddingTop="10px"
    android:paddingBottom="10px" 
    />              
    </LinearLayout>


    <LinearLayout android:id="@+id/linearLayout7"
        android:layout_below="@id/linearLayout6" android:layout_width="wrap_content"
        android:padding="10px"
        android:layout_height="wrap_content" android:orientation="horizontal"
        android:layout_alignParentLeft="true" android:layout_alignParentRight="true"  android:paddingRight="10px" android:paddingLeft="10px">

    <Button android:id="@+id/button1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Add Attachment"
    >

    </Button>

    </LinearLayout>


     <LinearLayout android:id="@+id/linearLayout8"
        android:layout_below="@id/linearLayout7" android:layout_width="wrap_content"
        android:padding="10px"
        android:layout_height="wrap_content" android:orientation="horizontal"
        android:layout_alignParentLeft="true" android:layout_alignParentRight="true"  android:paddingRight="10px" android:paddingLeft="10px">


    <Button android:id="@+id/askNow" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:layout_alignParentRight="true"
            android:text="Post Question" />

    </LinearLayout>
    <LinearLayout android:id="@+id/linearLayout9"
        android:layout_below="@id/linearLayout8" 
        android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:gravity="bottom"
              android:orientation="vertical">


         <SlidingDrawer  android:id="@+id/SlidingDrawer" android:handle="@+id/slideHandleButton" android:content="@+id/contentLayout" android:topOffset="50dip" android:layout_width="fill_parent" android:layout_height="fill_parent">
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/slideHandleButton" android:background="@drawable/closearrow"></Button>
            <LinearLayout android:id="@+id/contentLayout"  andr开发者_JS百科oid:layout_width="match_parent" android:layout_height="match_parent"
                            android:orientation="vertical" android:gravity="center" 
                            android:background="#FF444444">
            <Button android:id="@+id/asknew" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ask New Question"></Button>
            <Button android:id="@+id/logout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Log Out"></Button>
            </LinearLayout>
        </SlidingDrawer>
    </LinearLayout>

    <android.view.SurfaceView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/preview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    </android.view.SurfaceView>

</RelativeLayout>
</ScrollView>


I was having the same issue so I went digging in the source code. The basic answer is that something called measure() on your SlidingDrawer using MeasureSpec.UNSPECIFIED, i.e. the SlidingDrawer is allowed to set its own size.

It could be caused by the containing layout (or its parents) being base-aligned or having WRAP_CONTENT as its height or width. If the containing LinearLayout has baselineAligned set true, it asks all its children for their size to find the largest one. And of course it also asks its children for their size if it's supposed to wrap them.

Set android:baselineAligned="false" in any LinearLayout that contains the sliding drawer and ensure that nothing containing the SlidingDrawer has WRAP_CONTENT for height or width. In other words, the SlidingDrawer must have an exact size defined, which means its parents must also if you use FILL_PARENT.


Because your SlidingDrawer is in ScrollView. My guess is, because ScrollView is unable to provide height, but SlidingDrawer inherit the height from ScrollView, and caused the exception to throw. (I get the clue from http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/widget/SlidingDrawer.java)


You have to specify a dimension for layout_height attribute, like android:layout_height="100dp"


I'm not actually sure why this is causing your height or width values to be unspecified, but I can tell you that wrapping your sliding drawer in a LinearLayout is probably causing the problem. Turn this code into attributes on the SlidingDrawer and remove the LinearLayout:

android:layout_below="@id/linearLayout8" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom"

Hopefully this fixes it for you.


I am afraid the issue might be with ScrollView: Seems like you can't place a SlidingDrawer in a ScrollView. Check out this thread for the same issue. The guy's solution was to have the ScrollView Wrap everything EXCEPT the SlidingDrawer. Then he put both, the ScrollView and the SlidingDrawer into a RelativeLayout - seems to have worked for him, you should give it a shot (as will I).

[EDIT] I gave it a shot and here is how it works for me:

        RelativeLayout mainContainer = new RelativeLayout(context);
        mainContainer.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));

        ScrollView scrollView = new ScrollView(context);
        scrollView.setLayoutParams(new ScrollView.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
        // Add furhter children to scrollview if you wish to
        // Add the scrollview to the main container
        mainContainer.addView(scrollView);
        // Create your SlidingViewer, for instance be inflating it using the layout inflater
        // Inflate from pre-written layout
        // getSystemService is only available from an Activity
        LayoutInflater inflater = (LayoutInflater)getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        // We dont have the parent view yet, will be added by the layouter
        SlidingDrawer slidingDrawer = (SlidingDrawer) inflater.inflate(R.layout.sliding_drawer_layout, null);

        // Add Sliding drawer at the bottom
        mainContainer.addView(new SlidingDrawerWidget(context, new AttributeContainer()).getView());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜