2

标题说明了一切,每次我更改布局视图时,我的应用程序都会崩溃,我需要按钮位于最顶部,imageView1 位于最底部,iv_photo 位于两者之间。所以我尝试将我的 xml 按钮移到底部,然后我的应用程序崩溃了,我没有更改代码或任何东西,我所做的只是移动代码。这是我的代码

不会崩溃

<RelativeLayout 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"
    tools:context=".DashboardActivity" >

    <ImageView
        android:id="@+id/iv_photo"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp" />

    <Button
        android:id="@+id/btn_crop"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignLeft="@+id/iv_photo"
        android:layout_alignTop="@+id/iv_photo"
        android:text="@string/Nothing" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="105dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:scaleType="fitXY"
        android:src="@drawable/whitestrip" />

</RelativeLayout>

会崩溃

<RelativeLayout 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"
    tools:context=".DashboardActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="105dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:scaleType="fitXY"
        android:src="@drawable/whitestrip" />

    <ImageView
        android:id="@+id/iv_photo"
        android:layout_width="125dp"
        android:layout_height="125dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp" />

    <Button
        android:id="@+id/btn_crop"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignLeft="@+id/iv_photo"
        android:layout_alignTop="@+id/iv_photo"
        android:text="@string/Nothing" />

</RelativeLayout>

这是我在 LogCat 中遇到的错误

05-01 13:49:15.632: E/AndroidRuntime(32148): FATAL EXCEPTION: main
05-01 13:49:15.632: E/AndroidRuntime(32148): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.atmebeta/com.example.atmebeta.DashboardActivity}: java.lang.ClassCastException: android.widget.ImageView
05-01 13:49:15.632: E/AndroidRuntime(32148):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
05-01 13:49:15.632: E/AndroidRuntime(32148):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
05-01 13:49:15.632: E/AndroidRuntime(32148):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-01 13:49:15.632: E/AndroidRuntime(32148):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-01 13:49:15.632: E/AndroidRuntime(32148):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 13:49:15.632: E/AndroidRuntime(32148):    at android.os.Looper.loop(Looper.java:123)
05-01 13:49:15.632: E/AndroidRuntime(32148):    at android.app.ActivityThread.main(ActivityThread.java:3687)
05-01 13:49:15.632: E/AndroidRuntime(32148):    at java.lang.reflect.Method.invokeNative(Native Method)
05-01 13:49:15.632: E/AndroidRuntime(32148):    at java.lang.reflect.Method.invoke(Method.java:507)
05-01 13:49:15.632: E/AndroidRuntime(32148):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
05-01 13:49:15.632: E/AndroidRuntime(32148):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-01 13:49:15.632: E/AndroidRuntime(32148):    at dalvik.system.NativeStart.main(Native Method)
05-01 13:49:15.632: E/AndroidRuntime(32148): Caused by: java.lang.ClassCastException: android.widget.ImageView
05-01 13:49:15.632: E/AndroidRuntime(32148):    at com.example.atmebeta.DashboardActivity.onCreate(DashboardActivity.java:106)
05-01 13:49:15.632: E/AndroidRuntime(32148):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-01 13:49:15.632: E/AndroidRuntime(32148):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
4

1 回答 1

8

我以前见过这样的东西。

尝试运行一个干净的构建,然后再次运行您的应用程序。

在窗口栏上转到“项目”->“清洁”

清理所有解决方案,然后一切都很好。

于 2013-05-01T17:53:05.123 回答