Android 在xml中移动imageView
在我的应用程序中,我有以下布局:Android 在xml中移动imageView,android,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"
<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"/>
我不明白为什么应用程序会崩溃。有人能帮我吗?根据崩溃日志
ClassCastException: android.widget.TextView cannot be cast to android.widget.ImageView
您正在将文本视图转换为图像视图
如果在您移动应用程序之前,它正在工作,请尝试清理并构建应用程序,以便重新生成.R文件。否则请检查您是否使用了正确的ID和类型
例如
我尝试了这两种方法,我的应用程序在两种视图中都运行良好……我希望你没有将它们放在基本布局下(线性或相对) “线性布局”下的第一个布局:
“线性布局”下的第二个布局:
clean and build your project我猜问题不在于您创建的布局,而在于您试图根据logcat输出将TextView强制转换为小部件的java代码。
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
ClassCastException: android.widget.TextView cannot be cast to android.widget.ImageView
(TextView)findViewById(R.id.info);
<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>