简体   繁体   English

通过单击按钮[2个活动]更改Textview的文本

[英]Change text of a Textview by clicking Button [2 activities]

I have two activities in my project (ChooseLevel and ShowTraining). 我的项目中有两个活动(ChooseLevel和ShowTraining)。 In the first activity i have a Button, and in the second activity i have a Textview. 在第一个活动中,我有一个Button,在第二个活动中,我有一个Textview。

My problem is that when i try to click the button (and start the second activity with an intent), i want to change the text of the second activity's textview, but my app crashes. 我的问题是,当我尝试单击按钮(并以意图启动第二个活动)时,我想更改第二个活动的textview的文本,但是我的应用程序崩溃了。 I have already searched some posts here on stackoverflow but the problem still remains. 我已经搜索了一些关于stackoverflow的帖子,但是问题仍然存在。

This is the first class: 这是头等舱:

public class ChooseLevel extends AppCompatActivity {
Button firstlevel;
TextView tv;


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

    firstlevel = (Button)findViewById(R.id.firstlevelBtn);
    tv  = (TextView)findViewById(R.id.titleTextTraining); //this is the textview of the second activity

   firstlevel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //start the second activity   
            Intent showTrainings = new Intent(ChooseLevel.this, ShowTrainings.class);
            startActivity(showTrainings);
            tv.setText("Some text here"); //change the text of the textview

        }
    });
}

The XML files of these two activities are ok. 这两个活动的XML文件都可以。 How can i solve? 我该如何解决? Thanks to everyone 谢谢大家

Simply, you cant do what you are trying to do with findViewById(). 简而言之,您无法使用findViewById()做您想做的事情。

findViewById() looks for the requested view on the content of the calling view. findViewById()在调用视图的内容上查找请求的视图。

So if you call it from the activity it will look for the view on the contentView you set with setContentView(), thats why its returning null. 因此,如果您从活动中调用它,它将在您使用setContentView()设置的contentView上查找视图,这就是其返回null的原因。

You should pass the text to the second activity via Intent extra, then before you set the contentView of the second activity you can call findViewById() and change its text. 您应该通过Intent extra将文本传递给第二个活动,然后在设置第二个活动的contentView之前,可以调用findViewById()并更改其文本。

To pass the text via intent 通过意图传递文本

Intent showTrainings = new Intent(ChooseLevel.this, ShowTrainings.class);
showTraining.putStringExtra("extrakey","YourTextHere");

then on the other side 然后在另一边

String text = getIntent().getStringExtra("extrakey");

Hope this helps. 希望这可以帮助。

tv  = (TextView)findViewById(R.id.titleTextTraining); 

You can't get TextView in Activity 2 when you are in Activity 1. 您在Activity 1中无法在Activity 2中获得TextView

You should skip to Activity 2 and change TextView in it. 您应该跳到Activity 2并在其中更改TextView

One way you can setText to your second Activity is by passing your value from first Activity to the second Activity, in this case you can use the intent.putExtra() method. 将Text设置为第二个Activity的一种方法是将值从第一个Activity传递给第二个Activity,在这种情况下,您可以使用intent.putExtra()方法。 So how do you do this? 那你怎么做呢?

Well, in the first class, ie the class from where you are passing the data, 好吧,在第一类中,即您从中传递数据的类中,

    public class ChooseLevel extends AppCompatActivity {
Button firstlevel;
TextView tv;


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

    firstlevel = (Button)findViewById(R.id.firstlevelBtn);
    //tv  = (TextView)findViewById(R.id.titleTextTraining); //this is the textview of the second activity

   firstlevel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //start the second activity   
            Intent showTrainings = new Intent(ChooseLevel.this, ShowTrainings.class);
            showTrainings.putExtra("TEXTID", "THE TEXT YOU WANT TO PASS");
            startActivity(showTrainings);


        }
    });
}

And in the showTrainings class, you can receive this value like: showTrainings类中,您可以收到以下值:

    String receive;

if (savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            if(extras == null) {
                receive= null;
            } else {
                receive= extras.getString("TEXTID");
            }
        } else {
            receive= (String) savedInstanceState.getSerializable("TEXTID");
        }

And then you can set the Received text to TextView like: 然后您可以将Received文本设置为TextView例如:

  tv  = (TextView)findViewById(R.id.titleTextTraining);
 tv.setText(receive);

Do this: 做这个:

ChooseLevel.java ChooseLevel.java

public class ChooseLevel extends Activity {
    Button firstlevel;
    TextView tv;


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

        firstlevel = (Button)findViewById(R.id.firstlevelBtn);

        firstlevel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //start the second activity   
                Intent showTrainings = new Intent(ChooseLevel.this, ShowTrainings.class);
                showTrainings.putExtra("textValue", "Some Text");
                startActivity(showTrainings);
            }
        });
    }
}

ShowTrainings.java ShowTrainings.java

public class ShowTrainings extends Activity {
    Button firstlevel;
    TextView tv;


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

        tv  = (TextView)findViewById(R.id.titleTextTraining);
        String str = getIntent().getStringExtra("textValue");
        if(str != null) {
            tv.setText(str);
        }
    }
}

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

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