-1

I have two classes. the first one is MainActivity and the other is Orders The MainActivity.java is as follows

 public class MainActivity extends Activity implements
            ProductListAdapterListener {
        private static final String TAG = MainActivity.class.getSimpleName();
        public String itms1,itms2,itms3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    @Override
        public void onAddToCartPressed(Product product) {
            if(x==0)
            {
                itms1=",";
            }
            x++;
            PayPalItem item = new PayPalItem(product.getName(), 1, product.getPrice(), Config.DEFAULT_CURRENCY,product.getSku());
            productsInCart.add(item);
                itms2 = item.getName();
            itms2=itms2.concat(", ");
            itms1=itms2.concat(itms1);
                Toast.makeText(getApplicationContext(),
                        item.getName() + " added to cart! the cart now has:" + itms1, Toast.LENGTH_LONG).show();
            }

    }

Here, I am trying to use the variable itms1 in another class Orders.java which is as follows:

 public class Orders extends MainActivity {
        Button email, text;
        public String msg=itms1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.orders);
            email = (Button) findViewById(R.id.email);
            text = (Button) findViewById(R.id.text);
         text.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    final String message = msg;

    //check whether the msg empty or not

                        final HttpClient httpclient = new DefaultHttpClient();
                        final HttpPost httppost = new HttpPost("http://ramsproject.16mb.com/sendorder.php");
                        Thread thread = new Thread(new Runnable(){
                            @Override
                            public void run() {

                                try {
                                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                                    nameValuePairs.add(new BasicNameValuePair("id", "01"));
                                    nameValuePairs.add(new BasicNameValuePair("message",message));
                                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                                    httpclient.execute(httppost);
                                    Toast.makeText(getBaseContext(), "Sent", Toast.LENGTH_SHORT).show();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                        thread.start();


                }


            });

        }
    }

Here, when I try to use variable itms1 in Orders.java, i get the value of itms1 as null. How do i use the value of that variable as it is as in MainActivity.java in another class?

Jagan P
  • 13
  • 1
  • 8
  • 2
    Possible duplicate of [What's the best way to share data between activities?](http://stackoverflow.com/questions/4878159/whats-the-best-way-to-share-data-between-activities) – Nir Alfasi Mar 10 '16 at 05:05
  • You are initialising the item1 only if Add To Cart is Pressed right ?.. – Sunil Sunny Mar 10 '16 at 05:09
  • do not extend MainActivity. make item1 variable public and static. and access in Order class as MainActivity.item1. Assign it to String msg inside onCreate Method. or as per you need. – vabhi vab Mar 10 '16 at 05:10

4 Answers4

0

In this line: public String itms1,itms2,itms3;

itms1 is declared but was never initialized.

The reason you are getting null is because your variable has not been initialized and only takes a value within the onAddToCartPressed() method.

VVN
  • 1,607
  • 2
  • 16
  • 25
fndg87
  • 331
  • 4
  • 13
0

You can declare the variable itms1 as public static by doing so you can access it by its class name (like MainActivity.itms1 ).

If you have still doubt comment below.

Hope this will helpful .

sahu
  • 1,188
  • 10
  • 19
0

You can have one Global class inside which you can store the variables which you want to access across activities as follows

public class Globals extends Application {String itms1;}

To access this String in another activity;

Globals g = (Globals) getApplication(); String msg = g.itms1;

Don't forget to insert this line in your AndroidMenifest.xml; android:name=".Globals"

Gvs13
  • 126
  • 12
0

Watch the use of that static variable...remember it will live in your memory and will be shared between different threads.

fndg87
  • 331
  • 4
  • 13