2012-12-25 3 views
1

SlidingDrawer을 구현하려고합니다. 내 응용 프로그램이 충돌합니다.SlidingDrawer를 구현하는 동안 handle 속성이 기존 자식을 참조해야합니다.

XML 파일

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <SlidingDrawer 
     android:id="@+id/drawer" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:content="@+id/content" 
     android:handle="@+id/handle" > 
    </SlidingDrawer> 

    <LinearLayout 
     android:id="@+id/contentLayout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#C0C0C0" 
     android:gravity="center|top" 
     android:orientation="vertical" 
     android:padding="10dip" > 

     <include 
      android:id="@+id/include1" 
      android:layout_width="fill_parent" 
      android:layout_height="60dp" 
      layout="@layout/header_history" > 
     </include> 

     <ListView 
      android:id="@android:id/list" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:cacheColorHint="#00000000" 
      android:divider="@android:color/black" 
      android:dividerHeight="1.0sp" 
      android:textColor="#000000" /> 
    </LinearLayout> 

</LinearLayout> 

로그 캣

사용자가 지정한대로이 오류가 발생한다
12-25 17:40:33.582: E/AndroidRuntime(454): FATAL EXCEPTION: main 
12-25 17:40:33.582: E/AndroidRuntime(454): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demohistory/com.example.demohistory.History}: java.lang.IllegalArgumentException: The handle attribute is must refer to an existing child. 
12-25 17:40:33.582: E/AndroidRuntime(454): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
12-25 17:40:33.582: E/AndroidRuntime(454): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
12-25 17:40:33.582: E/AndroidRuntime(454): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
12-25 17:40:33.582: E/AndroidRuntime(454): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
12-25 17:40:33.582: E/AndroidRuntime(454): at android.os.Handler.dispatchMessage(Handler.java:99) 
12-25 17:40:33.582: E/AndroidRuntime(454): at android.os.Looper.loop(Looper.java:123) 
12-25 17:40:33.582: E/AndroidRuntime(454): at android.app.ActivityThread.main(ActivityThread.java:4627) 
12-25 17:40:33.582: E/AndroidRuntime(454): at java.lang.reflect.Method.invokeNative(Native Method) 
12-25 17:40:33.582: E/AndroidRuntime(454): at java.lang.reflect.Method.invoke(Method.java:521) 
12-25 17:40:33.582: E/AndroidRuntime(454): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
12-25 17:40:33.582: E/AndroidRuntime(454): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
12-25 17:40:33.582: E/AndroidRuntime(454): at dalvik.system.NativeStart.main(Native Method) 
12-25 17:40:33.582: E/AndroidRuntime(454): Caused by: java.lang.IllegalArgumentException: The handle attribute is must refer to an existing child. 
12-25 17:40:33.582: E/AndroidRuntime(454): at android.widget.SlidingDrawer.onFinishInflate(SlidingDrawer.java:239) 
12-25 17:40:33.582: E/AndroidRuntime(454): at android.view.LayoutInflater.rInflate(LayoutInflater.java:626) 
12-25 17:40:33.582: E/AndroidRuntime(454): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621) 
12-25 17:40:33.582: E/AndroidRuntime(454): at android.view.LayoutInflater.inflate(LayoutInflater.java:407) 
12-25 17:40:33.582: E/AndroidRuntime(454): at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 
12-25 17:40:33.582: E/AndroidRuntime(454): at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 
12-25 17:40:33.582: E/AndroidRuntime(454): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198) 
12-25 17:40:33.582: E/AndroidRuntime(454): at android.app.Activity.setContentView(Activity.java:1647) 
12-25 17:40:33.582: E/AndroidRuntime(454): at com.example.demohistory.History.onCreate(History.java:59) 
12-25 17:40:33.582: E/AndroidRuntime(454): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
12-25 17:40:33.582: E/AndroidRuntime(454): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
12-25 17:40:33.582: E/AndroidRuntime(454): ... 11 more 

답변

5
Caused by: java.lang.IllegalArgumentException: The handle attribute is must refer to an existing child. 

android:handle="@+id/handle" 
SlidingDrawer 속성에서

. 하지만 '핸들'이라는 ID를 가진 슬라이딩 서랍의 기존 아이는 없습니다. 핸들 속성에 대한

확인 documentation : 서랍의 손잡이를 나타내는 아이

식별자.

"@ [+] [package :] type : name"또는 "? [package :] [type :] name"형식의 테마 속성에 대한 참조 여야합니다. .

그래서 당신은 같은으로 XML 파일을 수정해야합니다 :

<SlidingDrawer 
     android:id="@+id/drawer" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:content="@+id/content" 
     android:handle="@+id/handle" > 

    <ImageView 
      android:id="@id/handle" 
      android:layout_width="88dip" 
      android:layout_height="44dip" /> 

</SlidingDrawer> 

는 도움이되기를 바랍니다.

4

SDK에서 SlidingDrawer 코드를 복사하여 프로젝트로 가져 오는 경우 비슷한 문제가 발생했습니다. 내 문제는 레이아웃 파일의 네임 스페이스입니다.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:dd="http://schemas.android.com/apk/res/com.example.actionbarslider" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 


<com.example.actionbarslider.MySlidingDrawer 
    android:id="@+id/drawer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    dd:content="@+id/mycontent" 
    dd:handle="@+id/handle" 
    dd:orientation="0" > 

    <ImageView 
     android:id="@id/handle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/exit_light" /> 

    <LinearLayout 
     android:id="@id/mycontent" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="20dp" 
      android:text="Hello" 
      android:textSize="20sp" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="20dp" 
      android:text="World" 
      android:textSize="20sp" /> 
    </LinearLayout>  

</com.example.actionbarslider.MySlidingDrawer> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Top Drawer"/> 
</LinearLayout> 

참고 : 나는 styleable 속성을 참조하기 위해 내 자신의 네임 스페이스를 만든 표준을 일치 "안드로이드"

내가 제대로 나는이 같은 언급했다 어떤 속성 식별하여 문제를 해결 네임 스페이스. "XMLNS : DD =". ... "

나는이처럼 attrs.xml이 파일에 대해 동일한 일을했다 :

<?xml version="1.0" encoding="utf-8"?> 

<declare-styleable name="MySlidingDrawer"> 

    <!-- Identifier for the child that represents the drawer's handle. --> 
    <attr name="handle" format="reference" /> 

    <!-- Identifier for the child that represents the drawer's content. --> 
    <attr name="content" format="reference" /> 

    <!-- Orientation of the SlidingDrawer. --> 
    <attr name="orientation" format="integer"/> 

    <!-- Extra offset for the handle at the bottom of the SlidingDrawer. --> 
    <attr name="bottomOffset" format="dimension" /> 

    <!-- Extra offset for the handle at the top of the SlidingDrawer. --> 
    <attr name="topOffset" format="dimension" /> 

    <!-- Indicates whether the drawer can be opened/closed by a single tap 
     on the handle. (If false, the user must drag or fling, or click 
     using the trackball, to open/close the drawer.) Default is true. --> 
    <attr name="allowSingleTap" format="boolean" /> 

    <!-- Indicates whether the drawer should be opened/closed with an animation when the user clicks the handle. Default is true. --> 
    <attr name="animateOnClick" format="boolean" /> 
</declare-styleable>  

이제 모든 작업.

+0

사용자 정의보기에서 obtainStyledAttributes를 호출하고 코드에서 속성을 설정해야합니까? 동일한 문제가 있지만 내 SlidingDrawer 뷰가 다른 조각이 아닌 하나의 조각에서 부풀려집니다. – speedynomads

+0

아니요, 리소스 파일에서 사용하고있는 특성의 네임 스페이스가 내 프로젝트의 네임 스페이스와 동일해야합니다. –

+0

아.덕분에 – speedynomads