jjzjj

android:onClick xml 不执行

coder 2024-07-04 原文

我正在尝试制作一个简单的应用程序,其中包含一个布局和两个将更改布局文本的按钮。但是我找不到解决为什么 android:onClick 不执行的方法。在尝试了一个 OnClickListener 之后,它不起作用但就像他们在这里所做的那样:How exactly does the android:onClick XML attribute differ from setOnClickListener?

我已经阅读并遵循了 ANdroid SDK 网站上的教程:http://developer.android.com/reference/android/widget/Button.html

这是我的 XML 代码:

<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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/maintext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.00"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button1"  />

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button2"  />

这是我的 Java 代码:

public class MainActivity extends Activity implements OnClickListener{

    TextView label1;
int stage;
Button button1, button2;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    label1 = new TextView(this);
    TextView label1 = (TextView) findViewById(R.id.maintext);

    stage = 1;
    label1.setText("hello " + stage);
    Log.d("HiThereActivity", "THIS IS DEBUG OUTPUT TO LOGCAT"); //working

    button1 = new Button(this);
    button2 = new Button(this);

    button1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            button1_click(v);
            Log.d("button1 working", "THIS IS DEBUG OUTPUT TO LOGCAT"); //not appearing
        }
    });
    button2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            button2_click(v);
            Log.d("button2 working", "THIS IS DEBUG OUTPUT TO LOGCAT"); //not appearing
        }
    });
}

public void button1_click(View v) {

    stage = 2;
    label1.setText("hello " + stage);
    Log.d("b1click", "THIS IS DEBUG OUTPUT TO LOGCAT"); //not appearing

}

public void button2_click(View v) {

    stage = 3;
    label1.setText("hello " + stage);
    Log.d("b2click", "THIS IS DEBUG OUTPUT TO LOGCAT"); //not appearing
}

@Override
public void onClick(View v) {
    Log.d("wrong method", "THIS IS DEBUG OUTPUT TO LOGCAT"); // not appearing
}}

所以我收到的唯一 Logcat 消息是 OnCreate 方法中的消息。阶段变量始终保持在 1,并且在我单击按钮时不会改变。但是,按钮会为点击设置动画,因此必须是 OnClick 或标签本身不起作用。

模拟器运行图像:http://i.stack.imgur.com/ioUoD.png

提前致谢!

最佳答案

这是因为您点击的按钮实际上并没有引用那些在 XML 布局文件中定义的按钮。 有了这个声明:

button1 = new Button(this);

您创建了一个新按钮,但没有引用那些在 XML 布局中声明的按钮。

正确的方法是获取对在 XML 布局中声明的按钮的引用:

button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);

关于android:onClick xml 不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17219006/

有关android:onClick xml 不执行的更多相关文章

  1. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  2. ruby - Chef 执行非顺序配方 - 2

    我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul

  3. ruby - 为什么 Ruby 的 each 迭代器先执行? - 2

    我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试

  4. ruby - 检查是否通过 require 执行或导入了 Ruby 程序 - 2

    如何检查Ruby文件是否是通过“require”或“load”导入的,而不是简单地从命令行执行的?例如:foo.rb的内容:puts"Hello"bar.rb的内容require'foo'输出:$./foo.rbHello$./bar.rbHello基本上,我想调用bar.rb以不执行puts调用。 最佳答案 将foo.rb改为:if__FILE__==$0puts"Hello"end检查__FILE__-当前ruby​​文件的名称-与$0-正在运行的脚本的名称。 关于ruby-检查是否

  5. postman——集合——执行集合——测试脚本——pm对象简单示例02 - 2

    //1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json

  6. ruby-on-rails - rbenv:从 RVM 移动到 rbenv 后,在 Jenkins 执行 shell 中找不到命令 - 2

    我从Ubuntu服务器上的RVM转移到rbenv。当我使用RVM时,使用bundle没有问题。转移到rbenv后,我在Jenkins的执行shell中收到“找不到命令”错误。我内爆并删除了RVM,并从~/.bashrc'中删除了所有与RVM相关的行。使用后我仍然收到此错误:rvmimploderm~/.rvm-rfrm~/.rvmrcgeminstallbundlerecho'exportPATH="$HOME/.rbenv/bin:$PATH"'>>~/.bashrcecho'eval"$(rbenvinit-)"'>>~/.bashrc.~/.bashrcrbenvversions

  7. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  8. ruby - 如何使用 Selenium Webdriver 根据 div 的内容执行操作? - 2

    我有一个使用SeleniumWebdriver和Nokogiri的Ruby应用程序。我想选择一个类,然后对于那个类对应的每个div,我想根据div的内容执行一个Action。例如,我正在解析以下页面:https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=puppies这是一个搜索结果页面,我正在寻找描述中包含“Adoption”一词的第一个结果。因此机器人应该寻找带有className:"result"的div,对于每个检查它的.descriptiondiv是否包含单词“adoption

  9. ruby-on-rails - Rake 任务仅调用一次时执行两次 - 2

    我写了一个非常简单的rake任务来尝试找到这个问题的根源。namespace:foodotaskbar::environmentdoputs'RUNNING'endend当在控制台中执行rakefoo:bar时,输出为:RUNNINGRUNNING当我执行任何rake任务时会发生这种情况。有没有人遇到过这样的事情?编辑上面的rake任务就是写在那个.rake文件中的所有内容。这是当前正在使用的Rakefile。requireFile.expand_path('../config/application',__FILE__)OurApp::Application.load_tasks这里

  10. ruby-on-rails - 只有当不是 nil 时才执行映射? - 2

    如果names为nil,则以下中断。我怎样才能让这个map只有在它不是nil时才执行?self.topics=names.split(",").mapdo|n|Topic.where(name:n.strip).first_or_create!end 最佳答案 其他几个选项:选项1(在其上执行map时检查split的结果):names_list=names.try(:split,",")self.topics=names_list.mapdo|n|Topic.where(name:n.strip).first_or_create!e

随机推荐