2

I have developed one android application.

This is my first activity:

public class ViewCartActivity extends Activity {

String mGrandTotal;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewcartactivity);

    ListView mLstView1 = (ListView) findViewById(R.id.listView1);
    TextView mTxtViewGrandTotal = (TextView) findViewById(R.id.mTxtViewGrandTotalValue);
    Button mBtnSubmit = (Button) findViewById(R.id.mBtnSubmit);

    ViewCartAdapter mViewCartAdpt = new ViewCartAdapter(
            ViewCartActivity.this);

    mLstView1.setAdapter(mViewCartAdpt);

    if (Constants.mItem_Detail.size() > 0) {
        Double mGTotal = Double.parseDouble(Constants.mItem_Detail.get(0)
                .get(SingleMenuItem.KEY_TOTAL));
        for (int i = 1; i < Constants.mItem_Detail.size(); i++) {
            mGTotal = mGTotal
                    + Double.parseDouble(Constants.mItem_Detail.get(i).get(
                            SingleMenuItem.KEY_TOTAL));
        }

        mGrandTotal = String.valueOf(mGTotal);
        mTxtViewGrandTotal.setText("$" + mGrandTotal);
    }

    mBtnSubmit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            String s= getIntent().getStringExtra(mGrandTotal);
             Intent i = new Intent(getApplicationContext(), CustomerLogin.class);
             i.putExtra("GrandTotal", s);
                startActivity(i);

        }
    });

This is my 2nd activity:

setContentView(R.layout.customer_login);

   Intent in = getIntent();
       String total = in.getStringExtra(mGrandTotal);
    TextView grandtotal = (TextView) findViewById(R.id.grand_total);
     grandtotal.setText("Welcome ,"+getIntent().getExtras().getString(total));

Here i have to run the app means am getting $45.32 on first activity. I have to pass these $45.32 to next activity means am getting null.

How can i clear these error.please help me how can i pass the value from 1st activity to next activity.

dakshbhatt21
  • 3,558
  • 3
  • 31
  • 40
user1897014
  • 143
  • 4
  • 14

4 Answers4

2

change getIntent().getExtras().getString(total) To:

getIntent().getStringExtra("GrandTotal"); 

Or

String total = in.getStringExtra("GrandTotal");
grandtotal.setText("Welcome ," + total);
Nermeen
  • 15,883
  • 5
  • 59
  • 72
  • i have changed the code.but now alos am getting same error only – user1897014 Dec 21 '12 at 09:34
  • can you post logcat and highlight which line gives the error? – Nermeen Dec 21 '12 at 09:36
  • this is my first activity:http://pastie.org/5560655 this is my 2nd activity:http://pastie.org/5560390 this is my 3rd activity:http://pastie.org/5560392 here i have to pass the mGrandTotal value is pass from 2nd activity to third activity.how can i do.am getting the null value – user1897014 Dec 21 '12 at 09:42
1
FirstActivity

public class ViewCartActivity extends Activity {

String mGrandTotal;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewcartactivity);

    ListView mLstView1 = (ListView) findViewById(R.id.listView1);
    TextView mTxtViewGrandTotal = (TextView) findViewById(R.id.mTxtViewGrandTotalValue);
    Button mBtnSubmit = (Button) findViewById(R.id.mBtnSubmit);

    ViewCartAdapter mViewCartAdpt = new ViewCartAdapter(
            ViewCartActivity.this);

    mLstView1.setAdapter(mViewCartAdpt);

    if (Constants.mItem_Detail.size() > 0) {
        Double mGTotal = Double.parseDouble(Constants.mItem_Detail.get(0)
                .get(SingleMenuItem.KEY_TOTAL));
        for (int i = 1; i < Constants.mItem_Detail.size(); i++) {
            mGTotal = mGTotal
                    + Double.parseDouble(Constants.mItem_Detail.get(i).get(
                            SingleMenuItem.KEY_TOTAL));
        }

        mGrandTotal = String.valueOf(mGTotal);
        mTxtViewGrandTotal.setText("$" + mGrandTotal);
    }

    mBtnSubmit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


             Intent i = new Intent(getApplicationContext(), CustomerLogin.class);
             i.putExtra("GrandTotal", mGrandTotal);
                startActivity(i);

        }
    });


SecondActivity

setContentView(R.layout.customer_login);

  Bundle b = getIntent().getExtras();
       String total = b.getString("GrandTotal");
    TextView grandtotal = (TextView) findViewById(R.id.grand_total);
     grandtotal.setText("Welcome ," + total );
User
  • 1,251
  • 1
  • 14
  • 19
0

Just change

grandtotal.setText("Welcome ,"+getIntent().getExtras().getString(total));

to

grandtotal.setText("Welcome ,"getIntent().getExtras().getString(GrandTotal));

in second activity.

And remove

String s= getIntent().getStringExtra(mGrandTotal);

It should be

i.putExtra("GrandTotal", mGrandTotal);

from first activity on button click event. Because mGrandTotal is local activity variable. So you don't need to get it from intent

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
0

Either store the value in shared preferences in one activity and get the value from same preference in another activity.

Storing in preferences

          SharedPreferences preferences=getSharedPreferences(preferencename, MODE_PRIVATE);
          Editor preferenceEditor=preferences.edit();

          preferenceEditor.putString(key,value);
          preferenceEditor.putInt(key,value);
          preferenceEditor.commit();

Getting from preferences

         preferences=getSharedPreferences(config_prop.getConfigValue("sharedprefs"), MODE_PRIVATE);
         String str=preferences.getString(key, defaultValue);

Also you can pass the values using intents.

Prateek
  • 12,014
  • 12
  • 60
  • 81