简体   繁体   English

从活动A到活动B的值返回null

[英]Value from Activity A to Activity B returning null

I am trying to get information from Activity B, into Activity A, the problem is that the value that i am getting is returning null for some reason. 我正在尝试将信息从Activity B获取到Activity A,问题是由于某种原因我获取的值返回null My code see's how many times you click on the Button and returns the value into my first Activity , at least thats what it is suppose to do. 我的代码看到的是您单击Button多少次并将该值返回到我的第一个Activity ,至少这就是它应该要做的。 If sombody see's my mistake please tell me, i have class in 5 hours :\\ 如果有人看到我的错误,请告诉我,我5个小时内上课了:\\

returned.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Intent intent = new Intent(Next.this, Hw3Activity.class);
                intent.putExtra("text", counted.getText().toString());
                startActivity(intent);
/*Next is the current activity, Counted is the name of my text box*/
            }
        });
        click.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                counter++;
            }

This is the Activity i want the information transferred to. 这是我希望信息转移到的Activity

Button change;
    TextView text;
    int number;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        change = (Button) findViewById(R.id.change);
        text = (TextView) findViewById(R.id.count);

        String s1 = getIntent().getStringExtra("Textview01");
        text.setText("You clicked the button " + s1 + " times.");

        change.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), Next.class);
                startActivityForResult(intent, 0);
/*this button is for going to the 2nd activity,not my problem currently*/
            }
        });
    }

}

here's what you are doing wrong: 这是您做错的事情:

String s1 = getIntent().getStringExtra("Textview01");

i believe it should be: 我认为应该是:

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

In the Activity where you want the info, there is an error. 在您需要信息的Activity中,有一个错误。 You need to put "text" instead "Textview01" . 您需要输入"text"而不是"Textview01"

You can use the following with error control. 您可以将以下内容用于错误控制。

Bundle extras = getIntent().getExtras();
if( extras != null){
   String text = extras.getString("text");
}

try String s1 = getIntent().getStringExtra("text"); 试试String s1 = getIntent().getStringExtra("text");

Because You are sending string value tag as "text" . 因为您正在将字符串值标签发送为"text"

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

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