【问题标题】:my onClickListener is not working我的 onClickListener 不工作
【发布时间】:2016-12-16 12:53:55
【问题描述】:

这是我的 MainActivity.java /* * 版权所有 (C) 2016 Android 开源项目 * * 根据 Apache 许可证 2.0 版(“许可证”)获得许可; * 除非遵守许可,否则您不得使用此文件。 * 您可以在以下网址获取许可证的副本 * * http://www.apache.org/licenses/LICENSE-2.0 * * 除非适用法律要求或书面同意,否则软件 *根据许可分发是在“原样”基础上分发的, * 不提供任何明示或暗示的保证或条件。 * 请参阅许可证以了解特定语言的管理权限和 * 许可证下的限制。 */ 包 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>

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

【问题讨论】:

    标签: android onclicklistener


    【解决方案1】:

    为什么你一次又一次地设置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);
         .
         .
         .
        }
    

    【讨论】:

    • 我没有足够的批次或声誉
    【解决方案2】:
    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 coe 就可以了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多