简体   繁体   English

为什么这段代码不能改变Android上的文字?

[英]Why doesn't this code to change text on Android work?

This code to change text when the user presses a button doesn't work. 当用户按下按钮时,此代码更改文本不起作用。 Why? 为什么?

package com.cookbook.simple_activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class activity extends Activity {
    private TextView txt = (TextView) findViewById(R.id.hello_text);
    Button startButton = (Button) findViewById(R.id.trigger);
   @Override
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple);


   startButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            txt.setText(R.string.pressthisbutton);
        }
    });
 }
}

Change it to 将其更改为

    public class activity extends Activity {
    private TextView txt;
    Button startButton;
   @Override
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple);
        txt = (TextView) findViewById(R.id.hello_text);
        startButton = (Button) findViewById(R.id.trigger);

You need to initialize your Views after inflating your layout with setContentView() . 在使用setContentView()layout进行充气后,需要初始化Views Since your Views exist in your layout , they will return null if you haven't inflated your layout first. 由于您的Views你的存在layout ,他们将返回null ,如果你还没有充气的layout第一。 You can declare them before your setContentView() but you can't initialize them until after. 您可以在setContentView()之前声明它们,但直到之后才能初始化它们。

Also, since you are trying to access txt inside your listener it must either be final or declared as a member variable as above. 此外,由于您尝试在listener访问txt因此它必须是final或者如上所述声明为成员变量。

This was a rather easy one to spot but they aren't always. 这是一个相当容易发现但它们并非总是如此。 When you post a question try to describe what isn't working and how. 当您发布问题时,请尝试描述不起作用的内容以及如何操作。 Here it would be a NPE I'm guessing when you try to set the listener on your Button so it crashes. 这是一个NPE我猜你试图在你的Button上设置listener ,所以它崩溃了。 When it does crash, please provide the logcat so it is easier for us to spot the problem. 当它崩溃时,请提供logcat,以便我们更容易发现问题。

Edit 编辑

After seeing your xml, you are using the same string value for your initial text as you are for the text when you click the Button so of course it doesn't change. 在看到你的xml之后,当你单击Button时,你使用与初始文本相同的string值,因此它当然不会改变。 I'm not sure what you're after here but the code is running as it should as it is written. 我不确定你在这之后是什么,但代码正在运行,因为它应该在编写时运行。

try this code .... 试试这个代码......

you need to declare the 你需要申报

 TextView txt = (TextView) findViewById(R.id.hello_text); 

 Button startButton = (Button) findViewById(R.id.trigger);

after the create method otherwise it doesnot recognize id android Activity lifecycle starts with the 在create方法之后,否则它不会识别id android Activity生命周期开始于

onCreate() method onCreate()方法

package com.cookbook.simple_activity;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    public class activity extends Activity {
       @Override
       public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_simple);

        TextView txt = (TextView) findViewById(R.id.hello_text);
        Button startButton = (Button) findViewById(R.id.trigger);

       startButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                txt.setText(R.string.pressthisbutton);
            }
        });
     }
    }

The problem is your layout is android_simple 问题是你的布局是android_simple

setContentView(R.layout.activity_simple);

but you are setting contentView as activity_simple 但是您将contentView设置为activity_simple

EDIT: 1 Do this 编辑:1这样

Initiallization of you textView and button in onCreate because this setContentView(R.layout.activity_simple); 你在onCreate中初始化了textView和button,因为这个setContentView(R.layout.activity_simple); says where to find the view but you set the view before telling the layout file 说找到视图的位置,但在告诉布局文件之前设置视图

TextView txt = null;
Button startButton = null;

 @Override
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple);
    txt = (TextView) findViewById(R.id.hello_text);
    startButton = (Button) findViewById(R.id.trigger);

   startButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            txt.setText(R.string.pressthisbutton);
        }
    });
 }

尝试这个:

txt.setText(getResources().getString(R.string.pressthisbutton));

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

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