2017-06-07 9 views
1

ImはAndroidプログラミングの新機能で、独自の画像ビューでクリックした画像ビューごとに新しいアクティビティを開くimageviewsでアプリを構築しようとしています。これまでのところ、私は主な活動をしており、4つの画像ビューをそれぞれ制約の下に置いていました。すべて私はアプリを開いたときの意味で、画像のビューが配置されている必要があり、私はimageview1、次に2、3、次に4のいずれかをクリックすることができます配置されます。しかし、私はアプリを開いて、例えばimageview 2またはimageview 3をクリックしようとすると、imageview 4は動作しません。私は1で始まってから2と3と4に進み、最後に私は自由にナビゲートすることができます。これをどうすれば解決できますか?私は前に私の主な活動のJavaファイルを掲載しました。どんな助けもありがとう。あなたはすべてのビューのために同じ変数を使用しているImage Viewと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

2つの問題があります。最初の問題は、終了括弧が間違った場所にあることです。 2番目の問題は、同じ変数を使用していることです。ここでは、コードが修正されました:

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番目、次にいずれかをクリックしてみてください.2,3、または4番目の画像ビューを最初にクリックしてみてください: – Essentialz

+0

この 'ts =(ImageView)findViewById(R.id.ts2) 'これを' ts2 =(ImageView)findViewById(R.id.ts2) 'に変更してください。残りの部分についても同様に – 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