setImageResource不起作用

Les*_*992 22 android

this is the first canvas.


<?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> 
Run Code Online (Sandbox Code Playgroud)

图像更改将发生的第二个画布就是这个

            <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>
Run Code Online (Sandbox Code Playgroud)

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);
                        }
                    });




                      }


               }
Run Code Online (Sandbox Code Playgroud)

http://i820.photobucket.com/albums/zz130/lestah23/untitled-1_zps33fa9be5.png 我的logcat消息.

当我在手机上运行此代码时.发生错误.当我检查logcat它说.我有错误setImageResource();

我该怎么办?我是android编程中的一个大人物:(

提前致谢

Rob*_*ood 24

更新@

Simon Schubert 答案看起来正确.

否则,您也可以通过从Resources访问getDrawable方法来完成.

Resources resources = getResources();
image.setImageDrawable(resources.getDrawable(R.drawable.myfirstimage));
Run Code Online (Sandbox Code Playgroud)

  • 对 API 22 及更高版本使用 ContextCompat.getDrawable 或 ResourcesCompat.getDrawable (2认同)

Sim*_*ert 5

  1. 好的,我看到了你的logcat图像捕获.做得好.

  2. 检查您的帐户中是否存在以下ID R.layout.cards.

    img1 = (ImageView) findViewById(R.id.img1);
    img2 = (ImageView) findViewById(R.id.img2);
    image = (ImageView) findViewById(R.id.ImageView01);
    
    Run Code Online (Sandbox Code Playgroud)

看起来您的某个图像无法访问.第55行究竟是什么?

  1. 你的代码应该有效.我想你的R中有一个不好的参考.清理你的项目并重新创建你的R.

Eclipse:ProjectClean

Android Studio:BuildClean Project


dag*_*ope 5

getResources().getDrawable 不推荐使用。

您可以尝试ContextCompat.getDrawable

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