简体   繁体   中英

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. 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. If sombody see's my mistake please tell me, i have class in 5 hours :\\

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.

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. You need to put "text" instead "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");

Because You are sending string value tag as "text" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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