2017-01-15 6 views
0

Numbersだけが動作していますが、残りは動作していませんが、エラーはありません。 Numbers Textviewをクリックすると、それが動作して他のインテントに移動しますが、他のTextViewをクリックしても全く反応しません。AndroidインテントOnClickListenerは1つしか動作しません

すべてのアクティビティがマニフェストで宣言されています。

/* 
* Copyright (C) 2016 The Android Open Source Project 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*  http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 
package com.example.android.miwok; 

import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Set the content of the activity to use the activity_main.xml layout file 
     setContentView(R.layout.activity_main); 

     // Find the View that shows the phrases category 
     TextView colors = (TextView) findViewById(R.id.colors); 

// Set a click listener on that View 
     colors.setOnClickListener(new View.OnClickListener() { 
      // The code in this method will be executed when the phrases View is clicked on. 
      @Override 
      public void onClick(View view) { 
       Intent colorsIntent = new Intent(MainActivity.this, ColorsActivity.class); 
       startActivity(colorsIntent); 
      } 
     }); 




     // Set the content of the activity to use the activity_main.xml layout file 
     setContentView(R.layout.activity_main); 

     // Find the View that shows the phrases category 
     TextView family = (TextView) findViewById(R.id.family); 

// Set a click listener on that View 
     family.setOnClickListener(new View.OnClickListener() { 
      // The code in this method will be executed when the phrases View is clicked on. 
      @Override 
      public void onClick(View view) { 
       Intent familyIntent = new Intent(MainActivity.this, FamilyActivity.class); 
       startActivity(familyIntent); 
      } 
     }); 




     // Set the content of the activity to use the activity_main.xml layout file 
     setContentView(R.layout.activity_main); 

     // Find the View that shows the phrases category 
     TextView phrases = (TextView) findViewById(R.id.phrases); 

// Set a click listener on that View 
     phrases.setOnClickListener(new View.OnClickListener() { 
      // The code in this method will be executed when the phrases View is clicked on. 
      @Override 
      public void onClick(View view) { 
       Intent phrasesIntent = new Intent(MainActivity.this, PhrasesActivity.class); 
       startActivity(phrasesIntent); 
      } 
     }); 


     // Set the content of the activity to use the activity_main.xml layout file 
     setContentView(R.layout.activity_main); 

     // Find the View that shows the phrases category 
     TextView numbers = (TextView) findViewById(R.id.numbers); 

// Set a click listener on that View 
     numbers.setOnClickListener(new View.OnClickListener() { 
      // The code in this method will be executed when the phrases View is clicked on. 
      @Override 
      public void onClick(View view) { 
       Intent numbersIntent = new Intent(MainActivity.this, NumbersActivity.class); 
       startActivity(numbersIntent); 
      } 
     }); 



    } 


} 
+1

は、あなたのコードは本当にAndroidのオープンソースプロジェクトの一環ですか?あなたはたぶんコピーの権利通知に自分の名前を入れるべきです。 –

+1

'setcontentView()' ..を削除します。最初のものだけを残してください。 – rafsanahmad007

+0

自分のコードではありません.Googleコースの一部です。ライセンスを所有していません。しかし、ありがとうtho – Shalto

答えて

1

setContentView()を2回呼び出しています。 2回目には、すでにOnClickListenerを設定したオブジェクトとは異なる新しいオブジェクトViewが作成されます。実際に表示される新しいViewにはリスナーがありません。 2番目の通話を削除すると、あなたは行くのが良いでしょう。

p.s.コードが重複している場合は、単にコピー&ペーストするのではなく、メソッドを作成する必要があります。重複が異なる場所は、メソッドパラメータの良い候補です。この場合、このような何かがうまく動作します:

private void createOnClickListener(TextView view, Class<Activity> activityClass) { 
    view.setOnClickListener(new View.OnClickListener() { 
      // The code in this method will be executed when the phrases View is clicked on. 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent(MainActivity.this, activityClass); 
       startActivity(intent); 
      } 
     }); 
} 

を今、あなたは、単一の行で各リスナーを作成することができます。

createOnClickListener(colors, ColorsActivity.class); 
+0

私は同じことをしましたが、私はコースに従っており、彼らは具体的にそれをしないように頼んだ – Shalto

+0

ありがとう – Shalto

1

Uは

setContentView(R.layout.activity_main) 

あまりにも多くの時間を宣言しました。 super.onCreateの後で1回だけ宣言してください。

+0

ありがとう、今働いています – Shalto

1

setContentView()を1回だけ使用してください。最初のものを保ち、他のものを取り除くとすべてがうまくいくはずです。

+0

ありがとう今働いています – Shalto

0

このコードを削除するこの問題の原因となっているレイアウトを2回目に設定しています。

// Set the content of the activity to use the activity_main.xml layout file 
setContentView(R.layout.activity_main); 
関連する問題