在xml中移动imageView

时间:2014-03-17 09:45:13

标签: android

在我的应用中,我有这个布局:

<TextView
    android:id="@+id/info"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" 
    android:visibility="gone"/>

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_gravity="center"
    android:layout_marginTop="30dip"/>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_gravity="center"
    android:layout_marginTop="60dip"/>

以这种方式应用程序工作,但如果我想把imageview顶部应用程序崩溃 例如:

  <ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_gravity="center"
    android:layout_marginTop="60dip"/>


<TextView
    android:id="@+id/info"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" 
    android:visibility="gone"/>

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_gravity="center"
    android:layout_marginTop="30dip"/>

这是logcat:

03-17 10:42:39.919: W/ApplicationPackageManager(18362): getCSCPackageItemText()
03-17 10:42:40.130: D/AndroidRuntime(18362): Shutting down VM
03-17 10:42:40.130: W/dalvikvm(18362): threadid=1: thread exiting with uncaught exception (group=0x41844da0)
03-17 10:42:40.130: E/AndroidRuntime(18362): FATAL EXCEPTION: main
03-17 10:42:40.130: E/AndroidRuntime(18362): Process: com.example.localinuovo, PID: 18362
03-17 10:42:40.130: E/AndroidRuntime(18362): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.localinuovo/com.example.localinuovo.Second}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.ImageView
03-17 10:42:40.130: E/AndroidRuntime(18362):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
03-17 10:42:40.130: E/AndroidRuntime(18362):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
03-17 10:42:40.130: E/AndroidRuntime(18362):    at android.app.ActivityThread.access$900(ActivityThread.java:161)
03-17 10:42:40.130: E/AndroidRuntime(18362):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
03-17 10:42:40.130: E/AndroidRuntime(18362):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-17 10:42:40.130: E/AndroidRuntime(18362):    at android.os.Looper.loop(Looper.java:157)
03-17 10:42:40.130: E/AndroidRuntime(18362):    at android.app.ActivityThread.main(ActivityThread.java:5356)
03-17 10:42:40.130: E/AndroidRuntime(18362):    at java.lang.reflect.Method.invokeNative(Native Method)
03-17 10:42:40.130: E/AndroidRuntime(18362):    at java.lang.reflect.Method.invoke(Method.java:515)
03-17 10:42:40.130: E/AndroidRuntime(18362):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
03-17 10:42:40.130: E/AndroidRuntime(18362):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
03-17 10:42:40.130: E/AndroidRuntime(18362):    at dalvik.system.NativeStart.main(Native Method)
03-17 10:42:40.130: E/AndroidRuntime(18362): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.ImageView
03-17 10:42:40.130: E/AndroidRuntime(18362):    at com.example.localinuovo.Second.onCreate(Second.java:28)
03-17 10:42:40.130: E/AndroidRuntime(18362):    at android.app.Activity.performCreate(Activity.java:5426)
03-17 10:42:40.130: E/AndroidRuntime(18362):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
03-17 10:42:40.130: E/AndroidRuntime(18362):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
03-17 10:42:40.130: E/AndroidRuntime(18362):    ... 11 more

我不明白应用程序崩溃的原因。有谁可以帮助我?

2 个答案:

答案 0 :(得分:2)

根据崩溃日志。

ClassCastException: android.widget.TextView cannot be cast to android.widget.ImageView

您正在为图像视图投射文本视图..

如果在移动它之前它正在工作,那么请尝试清理并构建应用程序,以便重新生成.R文件。否则,请检查您使用的是正确的ID和类型。

E.g。

 (TextView)findViewById(R.id.info);

答案 1 :(得分:0)

我试过了两个,我的应用程序在两个视图中仍然正常工作......我希望你没有在基础布局(线性或相对)下使用它们。

“线性布局”下的第一个布局:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dip" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="60dip" />

</LinearLayout>

“线性布局”下的第二个布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="60dip" />

    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dip" />

</LinearLayout>