Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android

Varriables

How could I use this varriable in another class?

package com.example.help;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    public int counter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        counter = 0;
    }
}

5 Answers

What do you mean use the variable in another class? What exactly are you trying to do?

Like if I try to display this number in a different class. eg: Lets say I have a text view varriable in another class named mtextview and im trying to write the following line of code. mtextview.setText( StringOf.counter);

I would somehow need to import the counter varriable into the other class. How would I do this?

I guess making a public method and returning the variable would work? Never tried it before.

Since counter is a public variable, any other class can access it as long as that other class has a reference to the object of MainActivity. So you could do something like this

public class MainActivity extends Activity {

   public int counter;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      counter = 0;

      MyOtherClass myObject = new MyOtherClass();
      myObject.showCounterValue(this);   // You have to pass in the current instance of MainActivity so that myObject has a reference to the MainActivity object
   }
}

public class MyOtherClass {

   public void showCounterValue(MainActivity myMainActivity)
   {
      log.i("LOG", "the value of counter is " + myMainActivity.counter);   // You can access counter like this
   }

}

Public variables and methods can be accessed by any class. You just have to have a reference to the object of that class first.

Sorry I meant in another activity

You could use the putExtra method of the Intent class to pass data to your new activity when you start it.

public class MainActivity extends Activity {

   public int counter;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      counter = 0;
   }

   public void startNewActivity() {
      Intent intent = new Intent(MainActivity.this, MyOtherActivity.this);
      intent.putExtra("Counter Value", counter);   // Add the counter value as extra data to the intent for the new activity
      startActivity(intent);
   }

}

public class MyOtherActivity extends Activity {

   // Put your onCreate and other methods here

   public showCounterValue() {
      log.i("LOG", "the counter value is " + getIntExtra("Counter Value"));     // Get the counter value that was added with putExtra()
   }

}

It won't work for some reason

Isn't it supposed to say .class? Intent intent = new Intent(MainActivity.this, MyOtherActivity.class);

It still says: counter cannot be resolved to a variable

Yeah, you're right about the .class. It shouldn't be saying counter cannot be resolved to a variable. Post your code.

I'm on vacation right now I'll post it in a couple days

Main Activity

package com.example.help;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends Activity {

    public int counter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        counter = 0;
    }

        public void startNewActivity() {
              Intent intent = new Intent(MainActivity.this, SecondActivity.class);
              intent.putExtra("Counter Value", counter);   // Add the counter value as extra data to the intent for the new activity
              startActivity(intent);
    }
}

Second Activity

package com.example.help;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

public class SecondActivity extends Activity {

    public TextView mtextview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        mtextview = (TextView)findViewById(R.id.textview);
        mtextview.setText(counter);
    }

}

And I'm also not sure what the counter value is. Could you explain that to me?