2017-06-07 85 views
1

Im新的android編程,我正嘗試構建一個應用程序與圖像打開一個新的活動爲每個圖像視圖點擊與他們自己的圖像視圖。到目前爲止,我已經完成了我的主要活動,並將其上的4個圖像視圖全部放在彼此之下並受到約束。在我打開應用程序的意義上,所有工作都很好,圖像視圖展示了它們應該如何,我可以點擊imageview1,然後是2,然後是3,然後是4,然後是其中任何一個。但如果我打開應用程序,並嘗試點擊imageview 2第一或imageview 3例如,或imageview 4然後它不起作用。我必須從1開始,然後繼續2,然後3和4,最後我可以自由導航。我怎樣才能解決這個問題?我之前已經發布了我的主要活動java文件。任何幫助,將不勝感激。問題與圖像視圖和onClickListener

package com.ntq.ntqapp; 


import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.ImageView; 



public class MainActivity1 extends AppCompatActivity { 
private ImageView ts; 


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


    ts = (ImageView)findViewById(R.id.ts1); 
    ts.setOnClickListener(new View.OnClickListener(){ 
     public void onClick(View view){ 
      Intent myIntent = new Intent(MainActivity1.this, imageView1.class); 
      startActivity(myIntent); 


    ts = (ImageView)findViewById(R.id.ts2); 
    ts.setOnClickListener(new View.OnClickListener(){ 
     public void onClick(View view) { 
      Intent myIntent = new Intent(MainActivity1.this, imageView2.class); 
      startActivity(myIntent); 

    ts = (ImageView)findViewById(R.id.ts3); 
    ts.setOnClickListener(new View.OnClickListener(){ 
      public void onClick(View view) { 
      Intent myIntent = new Intent(MainActivity1.this, imageView3.class); 
      startActivity(myIntent); 

    ts = (ImageView) findViewById(R.id.ts4); 
    ts.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View view) { 
       Intent myIntent = new Intent(view.getContext(), imageView4.class); 
      startActivity(myIntent); 


     } 

     } 
     ); 
     }} 
     ); 
     }});}});}} 

回答

1

你有兩個問題。第一個問題是你的結束括號位於錯誤的位置。第二個問題是你正在使用相同的變量。下面是校正的代碼:

package com.ntq.ntqapp; 


import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.ImageView; 



public class MainActivity1 extends AppCompatActivity { 
private ImageView ts1; 
    private ImageView ts2; 
    private ImageView ts3; 
    private ImageView ts4; 


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

     ts1 = (ImageView) findViewById(R.id.ts1); 
     ts1.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Intent myIntent = new Intent(MainActivity1.this, 
              imageView1.class); 
       startActivity(myIntent); 
      } 
     }); 

     ts2 = (ImageView) findViewById(R.id.ts2); 
     ts2.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Intent myIntent = new Intent(MainActivity1.this, 
              imageView2.class); 
       startActivity(myIntent); 
      } 
     }); 
     ts3 = (ImageView) findViewById(R.id.ts3); 
     ts3.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Intent myIntent = new Intent(MainActivity1.this, 
              imageView3.class); 
       startActivity(myIntent); 
      } 
     }); 
     ts4 = (ImageView) findViewById(R.id.ts4); 
     ts4.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Intent myIntent = new Intent(view.getContext(), 
              imageView4.class); 
       startActivity(myIntent); 
      } 
     }); 
    } 
+0

兄弟...我愛你。這是那些肯定與括號現在它應該如何工作。非常感謝 :) – Essentialz

1

您正在使用相同的變量對所有的觀點:private ImageView ts
使用4個不同的變量(TS1,TS2),他們會解決您的序列問題!

+0

是的,我試圖用TS1,TS2,TS3,TS4之前,但它反應是相同的。我可以點擊第一圖像視圖然後第二然後第三然後第四然後其中任何一個,但如果嘗試點擊2,3或4 imageview首先我不能:S – Essentialz

+0

你有這個'ts =(ImageView)findViewById(R.id.ts2) '將其更改爲'ts2 =(ImageView)findViewById(R.id.ts2)'.. simialrly其餘以及 – Zakir

2

您對所有視圖都使用相同的變量名稱ts。

更改爲ts1,ts2,ts3,ts4。

關閉圓括號還有一些問題。

您需要每次關閉onClick方法,內部類和外部方法的括號。

ts1 = (ImageView) findViewById(R.id.ts1); 
ts.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View view) { 
     Intent myIntent = new Intent(MainActivity1.this, imageView1.class); 
     startActivity(myIntent); 
    } //you are missing this 
}); //and this 

// Notice how I change the variable name to ts2 here... 
ts2 = (ImageView)findViewById(R.id.ts2); 
//rinse and repeat... 
+0

這將工作,我想但我試過蒂姆答案上面,它現在的作品。謝謝你的幫助:) – Essentialz