user3247335
user3247335

Reputation: 163

Using A variable accorss different classes. Java Android

I have two class files. One which creates and stores a value in a string (PINString). The second class file uses the variable to show in a textView. as shown below: Currently the calue of the variable PINString isnt being passed through

First

  {


        //
        int randomPIN = (int)(Math.random()*9000)+1000;

        //
        String PINString = String.valueOf(randomPIN);

         Intent i = new Intent(getBaseContext(),verification.class);
     i.putExtra("PINString", PINString);//transfer string
     startActivity(i);

    }

Second

           public void onClick(View view) 

           {

            String PINString;

            Bundle bundle = getIntent().getExtras();
            PINString = bundle.getString("SMSDemo.PINString");



               TextView textView2 = (TextView) findViewById(R.id.textView2);

               textView2.setText(PINString);

Upvotes: 0

Views: 53

Answers (2)

Raghunandan
Raghunandan

Reputation: 133560

You have

 i.putExtra("PINString", PINString);
 // key is PINString

The keys must match. Use PINString while you get the string also

PINString = bundle.getString("PINString");

Also follow java naming conventions while naming vairables

Upvotes: 1

Phantômaxx
Phantômaxx

Reputation: 38098

You pass your variable as "PinString":

i.putExtra("PINString", PINString);//transfer string

So, instead of this:

PINString = bundle.getString("SMSDemo.PINString");

Use

PINString = bundle.getString("PINString");

Upvotes: 2

Related Questions