0

我有兩個圖像在我的主要活動中,我想顯示每個圖像點擊新的活動。當我點擊第一張圖片時,它會開始第二個活動(Image2.class),而不是開始第一個活動(Image1.class)。這是爲什麼發生?我想在點擊image1時顯示Image1.class活動。同樣Image2.class活動當我點擊image2。onclickListeners不能用於兩種不同ImageView

public class MainActivity extends Activity { 

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

     ImageView image1 = (ImageView) findViewById(R.id.imageView1); 

     ImageView image2 = (ImageView) findViewById(R.id.imageView2); 

     image1.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v1) { 
       // TODO Auto-generated method stub 

       Intent intent1 = new Intent(MainActivity.this,Image1.class); 
       intent1.putExtra("image1Desc", "Text Description for IMAGE1!!!!!!!!!"); 
       startActivity(intent1); 
       finish(); 
      } 
     }); 

     image2.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v2) { 
       // TODO Auto-generated method stub 
       Intent intent2 = new Intent(MainActivity.this, Image2.class); 
       intent2.putExtra("image2Desc", "Text Description for IMAGE2!!!!!!!"); 
       startActivity(intent2); 
       finish(); 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

主要活動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=".MainActivity" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/a" /> 

     <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/b" /> 

</RelativeLayout> 

Image1.class活動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=".Image1" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/image1text" /> 

    <ImageView 
     android:id="@+id/imageViewZoom1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="28dp" 
     android:src="@drawable/a" /> 

</RelativeLayout> 

Image2.class活動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=".Image2" > 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/image2text" /> 

    <ImageView 
     android:id="@+id/imageViewZoom2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="28dp" 
     android:src="@drawable/b" /> 

</RelativeLayout> 

AndroidManifest XML

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.imageanimation" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.imageanimation.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.example.imageanimation.Image1" 
      android:label="@string/title_activity_image1" > 
     </activity> 

     <activity 
      android:name="com.example.imageanimation.Image2" 
      android:label="@string/title_activity_image2" > 
     </activity> 
    </application> 

</manifest> 
+0

代碼似乎確定..檢查,如果你把正確的圖像在XML – stinepike 2013-04-28 12:45:01

+0

安置自己的XML還,你可能有混淆的ImageView – Pragnani 2013-04-28 12:45:05

+0

的ID會顯示您的XML? – 2013-04-28 12:50:31

回答

0

我發現這個問題很簡單。

MainActivity XML的imageView2與ImageView1不可見重疊。我重新調整大小後,問題就解決了。

在主要活動中XML

<ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="200dp" 
     android:layout_height="100dp" 
     android:layout_alignLeft="@+id/imageView1" 
     android:layout_below="@+id/imageView1" 
     android:layout_marginTop="19dp" 
     android:src="@drawable/b" />