简体   繁体   English

添加ImageView时应用程序崩溃

[英]App crashes when adding an ImageView

I'm trying to add an image to my app but it keeps crashing and I can't conclude why. 我正在尝试将图像添加到我的应用程序,但它一直崩溃,我不能总结为什么。

I have multiple images in my app, but adding this one image seems to be problematic. 我的应用程序中有多个图像,但添加这一个图像似乎有问题。 Here's my xml (EDIT: full xml (This has changed since the original for layout purposes)): 这是我的xml(编辑:完整的xml(为了布局目的,这已经改变了原因)):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/imageTextButton4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/imageTextButton3"
    android:layout_alignBottom="@+id/imageTextButton3"
    android:layout_alignLeft="@+id/imageTextButton2"
    android:layout_alignRight="@+id/imageTextButton2"
    android:drawableTop="@drawable/ic_launcher"
    android:text="Progress" />

<Button
    android:id="@+id/imageTextButton3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageTextButton1"
    android:layout_alignRight="@+id/imageTextButton1"
    android:layout_below="@+id/imageTextButton1"
    android:layout_marginTop="85dp"
    android:drawableTop="@drawable/ic_launcher"
    android:text="Meal Plans" />

<Button
    android:id="@+id/imageTextButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="22dp"
    android:drawableTop="@drawable/ic_launcher"
    android:text="Your Meals" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="42dp"
    android:src="@drawable/logo" />

<Button
    android:id="@+id/imageTextButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="16dp"
    android:drawableTop="@drawable/ic_launcher"
    android:text="Meal of the Day" />

</RelativeLayout>

And my java code (condensed for brevity): 和我的java代码(为了简洁而浓缩):

package com.example.c3347115app;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.content.Intent;

public class MainActivity extends Activity {

ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    image = (ImageView) findViewById(R.id.imageView1);

Logcat: logcat的:

05-10 01:55:30.718: W/dalvikvm(13140): threadid=1: thread exiting with uncaught exception (group=0x40a99228)
05-10 01:55:30.728: E/AndroidRuntime(13140): FATAL EXCEPTION: main
05-10 01:55:30.728: E/AndroidRuntime(13140): java.lang.RuntimeException: Unable to     start activity ComponentInfo{com.example.c3347115app/com.example.c3347115app.MainActivity}: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.ImageView
05-10 01:55:30.728: E/AndroidRuntime(13140):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
05-10 01:55:30.728: E/AndroidRuntime(13140):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
05-10 01:55:30.728: E/AndroidRuntime(13140):    at android.app.ActivityThread.access$600(ActivityThread.java:139)
05-10 01:55:30.728: E/AndroidRuntime(13140):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
05-10 01:55:30.728: E/AndroidRuntime(13140):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-10 01:55:30.728: E/AndroidRuntime(13140):    at android.os.Looper.loop(Looper.java:156)
05-10 01:55:30.728: E/AndroidRuntime(13140):    at android.app.ActivityThread.main(ActivityThread.java:4987)
05-10 01:55:30.728: E/AndroidRuntime(13140):    at java.lang.reflect.Method.invokeNative(Native Method)
05-10 01:55:30.728: E/AndroidRuntime(13140):    at java.lang.reflect.Method.invoke(Method.java:511)
05-10 01:55:30.728: E/AndroidRuntime(13140):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-10 01:55:30.728: E/AndroidRuntime(13140):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-10 01:55:30.728: E/AndroidRuntime(13140):    at dalvik.system.NativeStart.main(Native Method)
05-10 01:55:30.728: E/AndroidRuntime(13140): Caused by: java.lang.ClassCastException:      android.widget.Button cannot be cast to android.widget.ImageView
05-10 01:55:30.728: E/AndroidRuntime(13140):    at com.example.c3347115app.MainActivity.onCreate(MainActivity.java:22)
05-10 01:55:30.728: E/AndroidRuntime(13140):    at android.app.Activity.performCreate(Activity.java:4538)
05-10 01:55:30.728: E/AndroidRuntime(13140):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
05-10 01:55:30.728: E/AndroidRuntime(13140):    at        android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)

Can anyone figure out what the problem is? 谁能弄明白问题是什么? Thanks in advance. 提前致谢。

These two lines look problematic to me: 这两行看起来对我有问题:

android:layout_above="@+id/imageTextButton1"
android:layout_alignRight="@+id/imageTextButton2"

Doesn't the "@+id/" form generate a new id value? “@ + id /”表单不会生成新的id值吗? Perhaps you meant "@id/" 也许你的意思是“@ id /”

When crating ids in xml file (using @+id/) You do it just on first occurrence and later You use just @id/ 在xml文件中创建id时(使用@ + id /)你只是在第一次出现时才这样做,之后你只使用@ id /

So Your xml should look like this 所以你的xml应该是这样的

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/imageTextButton4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/imageTextButton3"
    android:layout_alignBottom="@id/imageTextButton3"
    android:layout_alignLeft="@+id/imageTextButton2"
    android:layout_alignRight="@id/imageTextButton2"
    android:drawableTop="@drawable/ic_launcher"
    android:text="Progress" />

<Button
    android:id="@id/imageTextButton3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageTextButton1"
    android:layout_alignRight="@id/imageTextButton1"
    android:layout_below="@id/imageTextButton1"
    android:layout_marginTop="85dp"
    android:drawableTop="@drawable/ic_launcher"
    android:text="Meal Plans" />

<Button
    android:id="@id/imageTextButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="22dp"
    android:drawableTop="@drawable/ic_launcher"
    android:text="Your Meals" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="42dp"
    android:src="@drawable/logo" />

<Button
    android:id="@id/imageTextButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="16dp"
    android:drawableTop="@drawable/ic_launcher"
    android:text="Meal of the Day" />

</RelativeLayout>

Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.ImageView 引起:java.lang.ClassCastException:android.widget.Button无法强制转换为android.widget.ImageView

You seem to have a Button in your layout with the same ID as your ImageView 您的布局中似乎有一个与ImageView具有相同ID的Button

I just found out what used to go wrong when i added an image. 我刚刚发现了添加图像时出错的地方。 The problem is, in my case, that ScrollView can't have more than one direct child. 在我的例子中,问题是ScrollView不能有多个直接子项。 So when i added an image side by side to the existing child, the compile shows no errors but the runtime application crashes on create. 因此,当我将图像并排添加到现有子项时,编译显示没有错误,但运行时应用程序在创建时崩溃。 The solution was to add a layout inside the scrollview as a unique direct child, inside which will go my usefull scrollview content layouts and views. 解决方案是在scrollview中添加一个布局作为一个独特的直接子项,其中将包含我有用的scrollview内容布局和视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM