我的onClickListener无法正常工作

时间:2016-12-16 12:53:55

标签: android onclicklistener

这是我的MainActivity.java     / *      *版权所有(C)2016 Android开源项目      *      *根据Apache许可证2.0版("许可证")获得许可;      *除非符合许可,否则您不得使用此文件。      *您可以在以下位置获取许可证副本      *      * http://www.apache.org/licenses/LICENSE-2.0      *      *除非适用法律要求或书面同意,否则软件      *根据许可证分发,发布在" AS IS"基础,      *不附带任何明示或暗示的保证或条件。      *有关管理权限的特定语言,请参阅许可证      *许可证下的限制。      * /      包com.example.android.miwok;

 import android.content.Intent;
 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);

    TextView numbers1=(TextView)findViewById(R.id.numbers);
    assert numbers1 != null;
    numbers1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent number_intent =new    Intent(MainActivity.this,NumbersActivity.class);
            startActivity(number_intent);
        }
    });

    setContentView(R.layout.activity_main);

    TextView family=(TextView)findViewById(R.id.family);
    assert family != null;
    family.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent family_intent=new   Intent(MainActivity.this,FamilyActivity.class);
             startActivity(family_intent);
        }
    });

    setContentView(R.layout.activity_main);

    TextView colors=(TextView)findViewById(R.id.colors);
    assert colors != null;
    colors.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent color_intent=new Intent(MainActivity.this,ColorsActivity.class);
            startActivity(color_intent);
        }
    });

    setContentView(R.layout.activity_main);

    TextView phrases=(TextView)findViewById(R.id.phrases);
    assert phrases != null;
    phrases.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent phrases_intent=new Intent(MainActivity.this,PhrasesActivity.class);
            startActivity(phrases_intent);
          }
        });
    }


}

和我的main_activity.xml

<?xml version="1.0" encoding="utf-8"?><!-- 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.
-->
 <LinearLayout 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:background="@color/tan_background"
 android:orientation="vertical"
 tools:context="com.example.android.miwok.MainActivity">

<TextView
    android:id="@+id/numbers"
    style="@style/CategoryStyle"
    android:background="@color/category_numbers"
    android:text="@string/category_numbers" />

<TextView
    android:id="@+id/family"
    style="@style/CategoryStyle"
    android:background="@color/category_family"
    android:text="@string/category_family" />

<TextView
    android:id="@+id/colors"
    style="@style/CategoryStyle"
    android:background="@color/category_colors"
    android:text="@string/category_colors" />

<TextView
    android:id="@+id/phrases"
    style="@style/CategoryStyle"
    android:background="@color/category_phrases"
    android:text="@string/category_phrases" />

</LinearLayout>

当我运行这个应用程序时没有错误没有警告但是当我点击我的文本视图时它不会进行除短语文本视图之外的任何其他活动,它适用于短语textview

2 个答案:

答案 0 :(得分:0)

为什么要一次又一次地设置setContentView(R.layout.activity_main);

只需在super.onCreate(savedInstanceState);

下面设置一次
 @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);
     .
     .
     .
    }

答案 1 :(得分:0)

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

import com.indianic.signaturestylo.R;


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

        TextView numbers1=(TextView)findViewById(R.id.numbers);
        assert numbers1 != null;
        numbers1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent number_intent =new    Intent(MainActivity.this,NumbersActivity.class);
                startActivity(number_intent);
            }
        });


        TextView family=(TextView)findViewById(R.id.family);
        assert family != null;
        family.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent family_intent=new   Intent(MainActivity.this,FamilyActivity.class);
                startActivity(family_intent);
            }
        });


        TextView colors=(TextView)findViewById(R.id.colors);
        assert colors != null;
        colors.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent color_intent=new Intent(MainActivity.this,ColorsActivity.class);
                startActivity(color_intent);
            }
        });


        TextView phrases=(TextView)findViewById(R.id.phrases);
        assert phrases != null;
        phrases.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent phrases_intent=new Intent(MainActivity.this,PhrasesActivity.class);
                startActivity(phrases_intent);
            }
        });
    }


}

用这个改变你的java cos它会正常工作。

相关问题