为什么setImageResource方法不起作用?

26

这是第一个画布:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                android:background="@drawable/background_linen" >

  <ScrollView
          android:id="@+id/scrollView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" >

    <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

      <TableRow
              android:id="@+id/TableRow01"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >

        <TextView
                android:id="@+id/TextView03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" "
                android:textAppearance="?android:attr/textAppearanceLarge" />
      </TableRow>

      <TableRow
              android:id="@+id/tableRow1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >

        <ImageView
                android:id="@+id/img1"
                android:layout_width="wrap_content"
                android:layout_height="150dp"
                android:src="@drawable/girbaud1front" />

      </TableRow>

      <TableRow
              android:id="@+id/tableRow2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >

        <TextView
                android:id="@+id/TextView01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" "
                android:textAppearance="?android:attr/textAppearanceLarge" />
      </TableRow>

      <TableRow
              android:id="@+id/tableRow3"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >

        <ImageView
                android:id="@+id/img2"
                android:layout_width="320dp"
                android:layout_height="150dp"
                android:src="@drawable/girbaud4front" />

      </TableRow>

      <TableRow
              android:id="@+id/tableRow4"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >

        <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" "
                android:textAppearance="?android:attr/textAppearanceLarge" />
      </TableRow>

      <TableRow
              android:id="@+id/tableRow5"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >

        <ImageView
                android:id="@+id/img3"
                android:layout_width="320dp"
                android:layout_height="150dp"
                android:src="@drawable/girbaud2front" />

      </TableRow>

      <TableRow
              android:id="@+id/TableRow6"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >

        <TextView
                android:id="@+id/TextView02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" "
                android:textAppearance="?android:attr/textAppearanceLarge" />
      </TableRow>

      <TableRow
              android:id="@+id/TableRow7"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >

        <ImageView
                android:id="@+id/img4"
                android:layout_width="320dp"
                android:layout_height="150dp"
                android:src="@drawable/girbaud3front" />

      </TableRow>

      <TableRow
              android:id="@+id/TableRow02"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >

        <TextView
                android:id="@+id/TextView04"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" "
                android:textAppearance="?android:attr/textAppearanceLarge" />
      </TableRow>

    </TableLayout>
  </ScrollView>

</RelativeLayout> 

图片变化将发生在第二个画布上,该画布对应的文件为:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/Layout01"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

  <ImageView
          android:id="@+id/ImageView01"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_alignParentLeft="true"
          android:layout_alignParentRight="true"
          android:layout_alignParentTop="true"
          android:layout_marginBottom="10dp"
          android:layout_marginTop="10dp"
          android:layout_marginRight="10dp"
          android:layout_marginLeft="10dp"
          android:scaleType="fitXY"
          android:src="@drawable/girbaud1front" />

  <TextView
          android:id="@+id/txt1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_alignParentLeft="true"
          android:layout_marginBottom="62dp"
          android:layout_marginLeft="48dp"
          android:textAppearance="?android:attr/textAppearanceLarge"
          android:textColor="#000000" />

</RelativeLayout>

Java文件:

package com.example.flip3d;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;


public class Cards extends Activity {

  ImageView img1;
  ImageView img2;
  ImageView image;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cards);

    img1 =(ImageView) findViewById(R.id.img1);
    img2 =(ImageView) findViewById(R.id.img2);
    image =(ImageView) findViewById(R.id.ImageView01);

    img1.setOnClickListener(new View.OnClickListener() {

      public void onClick(View arg0) {

        image.setImageResource(R.drawable.myfirstimage);
        Intent startyou = new Intent("com.example.flip3d.FLIP3D");
        startActivity(startyou);
      }
    });

    img2.setOnClickListener(new View.OnClickListener() {

      public void onClick(View view) {
        image.setImageResource(R.drawable.may2ndimage);

        Toast.makeText(Cards.this, "Card Selected", Toast.LENGTH_SHORT).show();

        Intent startyou = new Intent("com.example.flip3d.FLIP3D");
        startActivity(startyou);
      }
    });

  }

}

我的logcat消息。

当我在手机上运行这段代码时,出现了一个错误。检查logcat时,它说我在setImageResource();处出现了错误。

我该怎么办?我是Android编程的初学者。

提前谢谢。


你能否在问题中添加logcat中的错误信息。 - NaviRamyle
image.setImageResource(R.drawable.may2ndimage); 应该可以工作。你清理过项目了吗?项目 -> 清理 - Simon Schubert
ImageView img1、img2 和 image,它们都在 Layout cards 中吗? - AnujMathur_07
是的,它们都在布局卡中。 - Lester1992
7个回答

28

更新于@

Simon Schubert回答看起来是正确的。

否则,您还可以通过从资源中访问getDrawable方法来实现。

Resources resources = getResources();
image.setImageDrawable(resources.getDrawable(R.drawable.myfirstimage));

好的,很高兴看到你找到了解决方案,你是怎么解决的?出了什么错吗? - Simon Schubert
2
对于 API 22 及以上版本,请使用 ContextCompat.getDrawable 或 ResourcesCompat.getDrawable。 - HendraWD

5
  1. Ok I saw your logcat image capture. Well done.

  2. Check if the following ids exists in your R.layout.cards.

    img1 = (ImageView) findViewById(R.id.img1);
    img2 = (ImageView) findViewById(R.id.img2);
    image = (ImageView) findViewById(R.id.ImageView01);
    

看起来你的其中一张图片无法访问。第55行是什么?

  1. 你的代码应该可以工作。我猜测你的R文件中有一个错误的引用。清理你的项目并重新创建R文件。

Eclipse:ProjectClean

Android Studio:BuildClean Project


如何提供带有异常的Logcat? :) - Lester1992
进入DDMS -> Logcat -> 选择出现错误的那一行 "that i having error in setImageResource();" 并按下ctrl + c -> 编辑您的答案并按下ctrl + v.. 或手动输入。 :) - Simon Schubert
有很多不同类型的异常存在:例如NullPointerException -> 某些内容未初始化或不存在。 - Simon Schubert
我尝试清理我的项目,但是什么也没有发生。 :( - Lester1992
image.setImageResource(R.drawable.may2ndimage); 这是第55行。 - Lester1992

5

getResources().getDrawable已经过时了。

您可以尝试使用ContextCompat.getDrawable:

image.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.myimage));


3
您还可以调用 setImageDrawable
image.setImageDrawable(getResources().getDrawable(R.drawable.myfirstimage));

请检查您的视图初始化是否正确。或者在此处展示您的视图XML和Logcat日志。 - DcodeChef

2

我有和你一样的问题。实际上,我发现我的所有图片都放在了/res/drawable目录下。我有几张非常大的图片,所以我不得不将它们移动到/res/drawable-hdpi或xhdpi目录下。另一个技巧是缩小它们的尺寸。希望这可以帮助到某些人。干杯!


0

尝试使用这段代码:

Resources resources = getResources();
image.setImageDrawable(resources.getDrawable(R.drawable.myfirstimage));

第一行代码将解决这个问题。


0

试试这个一行代码:

img.setImageDrawable(ResourcesCompat.getDrawable(getResources(),R.drawable.theResId, null));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接