【发布时间】:2012-08-06 16:21:44
【问题描述】:
今天发生在我身上最奇怪的事情,导致了数小时的沮丧和愤怒管理问题。
我有一个可以运行的 Android 幻灯片应用程序。但是,我对按钮(在相对布局中)不满意,并将它们移动到线性布局。但是,在运行我的应用程序时,按钮的行为被交换了(即按钮 1 完成了按钮 2 的工作,反之亦然)。我可以通过更改其中一个按钮的 id 来解决这个问题,而不是全部(这不起作用)。与此相关的是,当我切换方向时,我的应用程序开始崩溃。当我有相对布局时,它以前工作过。 (我正在使用 Handler 来更改幻灯片的 UI)
我无法解决这个问题,所以我恢复到我的相对布局,但我想知道这首先是怎么发生的。
有效的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:contentDescription="@string/imageView"
android:scaleType="centerInside"
android:src="@drawable/image1" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageButton
android:id="@+id/startStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:contentDescription="@string/startStop"
android:src="@drawable/ic_menu_slideshow" />
<ImageButton
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="false"
android:layout_toRightOf="@+id/startStop"
android:contentDescription="@string/next"
android:src="@drawable/ic_media_next" />
<ImageButton
android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="false"
android:layout_toLeftOf="@+id/startStop"
android:contentDescription="@string/previous"
android:src="@drawable/ic_media_previous" />
</RelativeLayout>
导致问题的代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:contentDescription="@string/imageView"
android:scaleType="centerInside"
android:src="@drawable/image1" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#30eee9e9" >
<ImageButton
android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/previous"
android:src="@drawable/ic_media_previous" />
<ImageButton
android:id="@+id/bla"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/startStop"
android:src="@drawable/ic_menu_slideshow" />
<ImageButton
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/next"
android:src="@drawable/ic_media_next" />
【问题讨论】:
-
你能在你处理按钮点击的地方发布你的布局xml和代码吗?
-
你一定使用过相对布局的 LeftOff 或 RightOff 功能(作为 PARENT),是这样吗?
-
我猜您的应用程序在方向更改后由于 ClassCast 或 NullPointer 异常而崩溃。你有不同的纵向和横向布局吗?如果是这样,您需要更新两个布局。但如果没有相关的 code 和 logcat 错误,我们只能猜测。
-
为了了解它崩溃的原因,您需要查看 logcat。转到 Windows--> Show View --> Logcat 并查找红色异常区域。如果您单击带有您的包名称的行,它将显示错误提示。也可能想发布错误日志。
-
顺便说一句,上面的评论有 +4 的原因是因为包括我自己在内的 4 位读者认为您应该发布更多信息。
标签: android xml multithreading layout