2013-02-07 16 views
13
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> 

第二畫布是圖像的變化會發生是這樣的setImageResource不工作

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




         } 


       } 

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

當我在手機上運行此代碼。發生錯誤。當我檢查logcat它說。我有錯誤setImageResource();

我該怎麼辦?我是一個biginner在android系統編程:(

在此先感謝

+0

你可以添加到您的問題logcat中的錯誤。 – NaviRamyle

+0

image.setImageResource(R.drawable.may2ndimage);應該管用。你清理了你的項目嗎?項目 - >清潔 –

+0

ImageView img1,img2和圖像,都在佈局卡? –

回答

19

更新時間@

Simon Schubertanswer看起來正確。

否則,您也可以通過從資源訪問getDrawable方法來完成。

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

沒有任何反應。 – Lestr1992

+0

好,很高興看到你找到了解決方案,你是如何解決它?是錯誤? –

+0

+1,爲我工作。謝謝! –

1

試試這個,

int id = getResources().getIdentifier("yourpackagename:drawable/" + StringGenerated, null, null); 

這將返回您要訪問...那麼你可以設置圖像的繪製的ID在通過執行以下

imageview.setImageResource(id); 
的ImageView的
+0

com.example.flip3d:可繪製/這是你的意思嗎? – Lestr1992

+0

編輯我的答案 – jlopez

+0

沒有發生:( – Lestr1992

4

您也可以撥打setImageDrawable

image.setImageDrawable(getResources().getDrawable(R.drawable.myfirstimage)); 
+0

沒有發生:( – Lestr1992

+0

仍呈現在logcat的錯誤? – DcodeChef

+0

是經常發生。 – Lestr1992

2
  1. 好吧,我看到了你的logcat的圖像捕捉。做得好。

  2. 檢查您的R.layout.cards中是否存在以下ID。

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

它看起來像你的形象之一,是不可訪問,。第55行究竟是什麼?

  1. 您的代碼應該可以正常工作。我想有一個錯誤引用您的R.清理項目並重新創建您R.

的Eclipse:項目清潔

Android的工作室:構建清理項目

+0

正確地注意到+ 1 – RobinHood

+0

給Logcat帶來異常?:) – Lestr1992

+0

轉到DDMS - > Logcat - >選擇錯誤行,「我在setImageResource();」中出現錯誤,然後按ctrl + c - >編輯你的答案,然後按ctrl + v ..或者輸入:) –

0

我和你有同樣的問題。其實我發現我的所有圖像都放在/ res/drawable中。我有幾張很大的圖片,所以我不得不將它們移動到/ res/drawable-hdpi或xhdpi。另一個訣竅是縮小它們。我希望這會幫助某人。歡呼聲

3

getResources().getDrawable已棄用。

您可以嘗試ContextCompat.getDrawable

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

0

試試這個代碼:

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

一號線將解決這個問題。