简体   繁体   English

Android:更改ImageView源使我的应用程序意外关闭

[英]Android: changing an ImageView source makes my application close unexpectedly

I just signed up in order to ask for help. 我刚刚报名以寻求帮助。 I'm doing an application which displays an image gallery. 我正在做一个显示图片库的应用程序。 When you click any of the images, it must open the image and show it. 当您单击任何图像时,它必须打开图像并显示它。

There are two activities: 'Galeria' and 'Imagen'. 有两个活动:“ Galeria”和“ Imagen”。 'Galeria' displays the image gallery and 'Imagen' displays the image clicked. “ Galeria”显示图像库,“ Imagen”显示单击的图像。 When an image is clicked, I create the second activity using an Intent as seen in the code below. 单击图像后,我使用Intent创建第二个活动,如下面的代码所示。

Galeria.java code: Galeria.java代码:

package com.ejemplo.galeria;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;

public class Galeria extends Activity {

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

    //Called when an image is clicked (I use ImageButtons)
    public void openImage(View view){
        Intent i = new Intent(this,Imagen.class);
        i.putExtra("IMAGEN", view.getId());
        startActivity(i);
    }
}

The method 'openImage()' is called when any of the ImageButton is pushed (Whatever button you press, the same method is called). 当按下任何ImageButton时,将调用方法'openImage()'(无论您按什么按钮,都将调用相同的方法)。 Here is the 'main.xml' code: 这是“ main.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Galeria" >

<HorizontalScrollView
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:id = "@+id/scrollview">
    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ImageButton
            android:id="@+id/imagen1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="fitXY"
            android:onClick="openImage"
            android:src="@drawable/imagen1"></ImageButton>
        <ImageButton
            android:id="@+id/imagen2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="fitXY"
            android:onClick="openImage"
            android:src="@drawable/imagen2"></ImageButton>
        <ImageButton
            android:id="@+id/imagen3"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="fitXY"
            android:onClick="openImage"
            android:src="@drawable/imagen3"></ImageButton>
        <ImageButton
            android:id="@+id/imagen4"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="fitXY"
            android:onClick="openImage"
            android:src="@drawable/imagen4"></ImageButton>
        <ImageButton
            android:id="@+id/imagen5"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="fitXY"
            android:onClick="openImage"
            android:src="@drawable/imagen5"></ImageButton>
        <ImageButton
            android:id="@+id/imagen6"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="fitXY"
            android:onClick="openImage"
            android:src="@drawable/petergriffin"></ImageButton>
         <ImageButton
            android:id="@+id/imagen7"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:scaleType="fitXY"
            android:onClick="openImage"
            android:src="@drawable/gato"></ImageButton>
    </LinearLayout>
</HorizontalScrollView>

</RelativeLayout>

So, this is the code of the 'Image' class: 因此,这是“ Image”类的代码:

package com.ejemplo.galeria;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

public class Imagen extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        Intent i = getIntent();
        int id = i.getIntExtra("IMAGEN", -1);
        if(id != -1){
            ImageView iv = (ImageView) findViewById(R.id.imageV1);
            iv.setImageResource(id); //Error
            setContentView(R.layout.imagen);
        }
    }
}

And this is the XML code of 'imagen.xml' file: 这是“ imagen.xml”文件的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ImageView
        android:id="@+id/imageV1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" ></ImageView>

</LinearLayout>

So, when I modify the source of the ImageView, my applications closes unexpectedly, and I have no idea why. 因此,当我修改ImageView的源代码时,我的应用程序意外关闭,我也不知道为什么。 I've been looking for help but I found nothing, so I wish anyone can help me, I'm getting mad. 我一直在寻求帮助,但一无所获,所以我希望有人能帮助我,我正在生气。

Thank you! 谢谢!

------------------------------------------------------------------------------------------ -------------------------------------------------- ----------------------------------------

------------------------------------------------------------------------------------------ -------------------------------------------------- ----------------------------------------

OK I FOUND THE ISSUE 好,我找到了这个问题

I changed the parameter in the method 'setImageResource(id)' for R.drawable.imagen1 and constructed the switch case statement. 我在R.drawable.imagen1的方法'setImageResource(id)'中更改了参数,并构造了switch case语句。

But it kept closing unexpectedly!!! 但是它一直意外关闭!!!

So, in the method 'onCreate()' of 'Imagen' class, I just moved the line 'setContentView(R.layout.imagen)' from the end of the method to the beginning. 因此,在“ Imagen”类的“ onCreate()”方法中,我只是将“ setContentView(R.layout.imagen)”行从该方法的末尾移到了开始。

PROBLEM SOLVED :) 问题解决了 :)

So here: 所以在这里:

Intent i = new Intent(this,Imagen.class);
i.putExtra("IMAGEN", view.getId());
startActivity(i);

you just put view id in intent, but after: 您只是将视图ID放入了意图,但之后:

int id = i.getIntExtra("IMAGEN", -1);
if(id != -1){
      ImageView iv = (ImageView) findViewById(R.id.imageV1);
      iv.setImageResource(id); //Error
      setContentView(R.layout.imagen);
}

you try assign this R.id to image resource, this is wrong, because setImageResource expecting something like R.drawable.my_image . 您尝试将此R.id分配给图像资源,这是错误的,因为setImageResource期望使用R.drawable.my_image类的R.drawable.my_image Please follow javadoc . 请遵循javadoc

 public void openImage(View view){
    Intent i = new Intent(this,Imagen.class);
    i.putExtra("IMAGEN", view.getId());
    startActivity(i);
}

The above view.getId() will return the id of the particular imagebutton. 上面的view.getId()将返回特定图像按钮的ID。 This is no way related to the drawable resource. 这与可绘制资源无关。 So when you setImage() in the other activity it will indeed through a error as you are trying to set a button to a image view. 因此,当您在其他活动中设置setImage()时,在尝试为图像视图设置按钮时,确实会出现错误。

Instead you can construct a switch case statement to send the corresponding drawable id to the other activity. 相反,您可以构造一个switch case语句,以将相应的drawable id发送到其他活动。

As others said - you are trying to set View id as ImageResource - those are different two things; 正如其他人所说的那样-您正尝试将View id设置为ImageResource-这是两件事。 so to fix your problem I would suggest something like this 所以要解决您的问题,我建议这样

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.imagen); // before findViewById
    Intent i = getIntent();
    int id = i.getIntExtra("IMAGEN", -1);
    if(id != -1){
        ImageView iv = (ImageView) findViewById(R.id.imageV1);
        switch (id) {
         case R.id.imagen1: // first one
           iv.setImageResource(R.drawable.imagen1);
         break;
         // repeat for others
        } // switch close

    } // if 
} // onCreate

anyway it seems to me like you would benefit from using ListView / GridView together w/ some ArrayAdapter 无论如何,在我看来,您可能会受益于同时使用一些ArrayAdapter的ListView / GridView

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

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