Проблема с вложенностью макетов - PullRequest
0 голосов
/ 19 июня 2011

У меня в xml следующая ситуация:

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


<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:gravity="center"
    android:background="@drawable/topbar"
    >
    <TableRow >
        <TextView android:minWidth="280sp" 
            android:id="@+id/header" 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:layout_gravity="center_vertical"
            android:textColor="@color/path_color"
            android:paddingLeft="10px"/>
    </TableRow>

</TableLayout>
<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

    <org.solid.explorer.HorizontalPager
            android:id="@+id/horizontal_pager"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >
    </org.solid.explorer.HorizontalPager>

    <RelativeLayout
        android:id="@+id/contextMenu"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone"
        >
            <org.solid.explorer.ContextOption
                android:layout_height="wrap_content"
                android:layout_width="wrap_content" 
                />
            <org.solid.explorer.ContextOption
                android:layout_height="wrap_content"
                android:layout_width="wrap_content" 
                />
            <org.solid.explorer.ContextOption
                android:layout_height="wrap_content"
                android:layout_width="wrap_content" 
                />
            <org.solid.explorer.ContextOption
                android:layout_height="wrap_content"
                android:layout_width="wrap_content" 
                />
            <org.solid.explorer.ContextOption
                android:layout_height="wrap_content"
                android:layout_width="wrap_content" 
                />

    </RelativeLayout>
    <LinearLayout
        android:id="@+id/drag_view"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone">
        <ImageView
            android:id="@+id/drag_icon"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />
        <TextView
            android:id="@+id/drag_label"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />
    </LinearLayout>
<!--        <ImageView
        android:id="@+id/drag_icon1"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        android:visibility="gone"
    /> -->
</RelativeLayout>
</LinearLayout>

Проблема в LinearLayout с идентификатором drag_view.

В программном коде я использую ImageView с идентификатором drag_icon1 (он перетаскивается пальцем). То все в порядке. Но я хочу добавить текст к этому значку, поэтому мне нужно перетащить не ImageView, а LinearLayout с ImageView и TextView внутри, я прав?

Но когда я переключаюсь на LinearLayout и комментирую старый ImageView (как в представленном xml), программа завершается сбоем в методе setContentView (вторая строка onCreate (), поэтому дальнейший код не имеет ничего общего с этой проблемой). WTF? Если drag_icon1 не закомментирован и используется как перетаскиваемое изображение, то все в порядке, setContentView работает (даже при наличии drag_view).

Вот трассировка стека:

E/AndroidRuntime(13517): FATAL EXCEPTION: main
E/AndroidRuntime(13517): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.solid.explorer/org.solid.explorer.Explorer}:java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
E/AndroidRuntime(13517):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
E/AndroidRuntime(13517):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
E/AndroidRuntime(13517):        at android.app.ActivityThread.access$1500(ActivityThread.java:132)
E/AndroidRuntime(13517):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
E/AndroidRuntime(13517):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(13517):        at android.os.Looper.loop(Looper.java:143)
E/AndroidRuntime(13517):        at android.app.ActivityThread.main(ActivityThread.java:4196)
E/AndroidRuntime(13517):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(13517):        at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(13517):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime(13517):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime(13517):        at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(13517): Caused by: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
E/AndroidRuntime(13517):        at org.solid.explorer.Explorer.onCreate(Explorer.java:126)
E/AndroidRuntime(13517):        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
E/AndroidRuntime(13517):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
E/AndroidRuntime(13517):        ... 11 more

Это говорит о том, что что-то не так с LayoutParams, но с drag_icon1 это работает без проблем .. Я понятия не имею, что не так .. Спасибо за любые предложения

1 Ответ

0 голосов
/ 19 июня 2011

Предполагая, что XML-разметка, которую вы разместили, является полным файлом, я вижу следующие проблемы:

  • Вам необходим один корневой элемент.
  • Вы закрываете LinearLayoutв нижней части документа, который я не вижу.

Однако, если это правда, я не уверен, как вы могли бы заставить его работать, раскомментировав ImageView.

...