2013-02-07 5 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> 

la deuxième toile que changement d'image se produira est cesetImageResource ne fonctionne pas

  <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> 

fichier 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 mon message logcat.

lorsque j'exécute ce code sur mon téléphone. une erreur s'est produite. et quand je vérifie le logcat il dit. que j'ai une erreur dans setImageResource();

que dois-je faire? im un biginner dans la programmation Android :(

merci à l'avance

+0

Pouvez-vous ajouter à votre question l'erreur dans logcat. – NaviRamyle

+0

image.setImageResource (R.drawable.may2ndimage); devrait marcher. Avez-vous nettoyé votre projet? Projet -> Propre –

+0

Est-ce que ImageView img1, img2 et image, tous sont dans des cartes de mise en page? –

Répondre

19

Mis à jour @

Simon Schubertanswer semble correct.

Sinon, vous pouvez également le faire en accédant à la méthode getDrawable à partir de Ressources.

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

rien ne se passe – Lestr1992

+0

Ok agréable de voir que vous avez trouvé une solution, comment vous l'avez résolu? était l'erreur? –

+0

+1, Travailler pour moi, merci! –

1

Essayez ceci,

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

Ceci renvoie l'id du drawable que vous voulez accéder ... alors vous pouvez définir l'image dans le imageview en procédant comme suit

imageview.setImageResource(id); 
+0

com.example.flip3d: drawable/est-ce que vous voulez dire? – Lestr1992

+0

je modifier ma réponse – jlopez

+0

rien ne se passe :(:( – Lestr1992

4

Vous pouvez également appeler setImageDrawable:

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

rien ne se passe – Lestr1992

+0

il montre encore une erreur dans logcat? – DcodeChef

+0

oui toujours le cas. – Lestr1992

2
  1. Ok j'ai vu votre image logcat capture. Bien joué. Vérifiez si les identifiants suivants existent dans votre R.layout.cards.

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

Il ressemble à une de vos images n'est pas accessible. Qu'est-ce que la ligne 55?

  1. Votre code devrait fonctionner. Je crois qu'il ya une mauvaise référence dans votre R. Nettoyez votre projet et recréez votre R.

Eclipse: ProjetClean

Android Studio: Construirepropre projet

+0

remarqué correctement + 1 – RobinHood

+0

Donner Logcat avec exception? Comment? :) – Lestr1992

+0

Aller à DDMS -> Logcat -> sélectionnez la ligne d'erreur "que j'ai erreur dans setImageResource();" et appuyez sur ctrl + c -> modifier votre réponse et appuyez sur ctrl + v .. ou taper dehors. :) –

0

J'ai eu le même problème que vous. En fait, je découvre que toutes mes images ont été placées dans/res/drawable. J'ai eu quelques photos qui étaient très grandes, donc j'ai dû les déplacer vers/res/drawable-hdpi ou xhdpi.Une autre astuce consiste à les réduire. J'espère que cela aidera quelqu'un. Cheers

3

getResources().getDrawable est déprécié.

Vous pouvez ContextCompat.getDrawable:

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

0

Essayez ce code:

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

1ère ligne va résoudre le problème.

Questions connexes