5

So, lets say in my main activity, i have an array declared like this, that im not having any problems using inside any of main's methods:

public int currentPrices[] = {0,0,0,0,0,0,0,0,0};

Now, my buyDialog class is as follows:

package foosh.Frontier;


import android.app.Activity;
import android.os.Bundle;
import foosh.Frontier.*;

public class buyDialog extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Be sure to call the super class.
        super.onCreate(savedInstanceState);

        currentPrices[0]=999;

        //insert setup magic here
        setContentView(R.layout.buydialog);
}  }

Eclipse persistently says that currentPrices can't be resolved. How do I link a child activity to the parent activity's variables?

I may have done my intent wrong, as I'm quite new at constructing them. But here's that line, from main:

case R.id.buyButton1: 
            currentRes = masterRes.get(0);
            Intent intent = new Intent();
            intent.setClass(main.this, buyDialog.class);
            startActivity(intent);

2 Answers 2

10

The simple answer is you have to qualify it with the containing instance's name.

So if the instance is myinstance to access the currentPrices field, you would do this:

myinstance.currentPrices

However, you really should be using getters and setters and there are a lot more issues here regarding actually getting a handle on the instance of the activity. I would suggest looking into an intro Java book before you get your hands dirty with Android. Check out http://math.hws.edu/javanotes/ for a good online introduction.

Based on your tag, I assume you have some notion of global variables. There is no such thing in Java. In practice, you can have static variables that are globally accessible. For instance

public class MyClass{
  public static int MY_VALUE = 4;
}

You can access by writing

MyClass.MY_VALUE

However, with instance variables, that is, one's that aren't qualified by the static keyword, you have have an instance of the class that has been allocated via the new keyword.

For example

MyClass someInstance = new MyClass();
someInstance.currentPrices

The reason this is more complicated with Activities is because you don't have access to the instance of the activity class that is being used unless you do something really creative.

Does that make sense?

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply. Yes, I have a java book in the mail already, I realized I was getting in pretty deep pretty quickly :) ---- edit to your edit ---- Makes alot of sense. I had originally tried it "proper" with getters and setters but I still couldnt get the child to resolve back to the parent's methods. I'll tinker around with the New. Thanks.
@Eric, happy to help. It definitely can get confusing and Android can be a language all its own at times and can just confuse things further. Good luck!
The squiggly red line of doom is gone! Found a way to work the instance in. Thank you :)
2

If you ever need to do this without making a static variable, you can also pass the array through the Intent as an extra, like so:

intent.putExtra("currentPrices", currentPrices);
startActivity(intent);

Then, once you're in the new Activity, retrieve it like so:

int[] currentPrices = getIntent().getIntArrayExtra("currentPrices");

It looks like you're modifying the original from the dialog though, so short of using startActivityForResult() and handling it that way, Chris' method is much simpler.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.