简体   繁体   English

Android,onClick按钮

[英]Android, onClick Button

Although it seems like easy question I can't make it work. 尽管这似乎是一个简单的问题,但我无法解决。 (There are similar questions on StackOverFlow but doesn't seem to work for me, using xml onClick attribute). (在StackOverFlow上也有类似的问题,但使用xml onClick属性似乎对我不起作用)。

I want to change a text when a button is clicked (basics...) 我想在单击按钮时更改文本(基本...)

I'm using the following code. 我正在使用以下代码。

package com.example.mathieu.pangolin;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {

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

    public void doSomething(View v) {
        TextView myText = (TextView) this.findViewById(R.id.worldMood);
        myText.setText("The word is happy");
    }
}

and the activity_main.xml 和activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView
        android:name="@+id/worldMood"
        android:text="Grumpy World!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button android:name="@+id/worldButton"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:onClick="doSomething"
        android:text="Make the World Happy" />

</RelativeLayout>

I'm getting an error message, but can't figure out the solution. 我收到一条错误消息,但找不到解决方案。 Kind of a NullPointerException on myText ? myText上是否存在NullPointerException类型? What is wrong that I can't find my text by its Id? 我无法通过其ID查找文本,这是什么问题?

You have to use android:id to specify id of an attribute not android:name . 您必须使用android:id来指定属性的ID而不是android:name So change the following line, 因此,更改以下行,

android:name="@+id/worldMood"

to

android:id="@+id/worldMood"

try this. 尝试这个。

public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            TextView myText = (TextView)findViewById(R.id.worldMood);
        }

        public void doSomething(View v) {

            myText.setText("The word is happy");
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM