0

I am trying to define global objects using the application class. I therefore define the following class.

public class MyApplication extends Application {

    private MyObject myObject=new MyObject();

    public MyObject getMyObject(){
        return this.myObject;
    }
}

Then, I use it in an activity, but I get an error (Cannot resolve method getApplication()):

public class AnActivity extends Activity {

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

    Button buttonMusic=(Button) findViewById(R.id.button5);
    buttonMusic.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            MyApplication myApplication = ((MyApplication)this.getApplication());
            Toast.makeText(MoodActivity.this, "playing music", Toast.LENGTH_SHORT).show();
        }
    });

    }
}

I have no clue why I get this error, as it for example works when calling the getApplication() in another activity.

I'm pretty new to Android and Java, so please excuse the ingenuity of the question.

UPDATE

Then I do MyObject myObject=myApplication.getMyObject(); and I don't get any compilation issue but the app dies as soon as I get in that activity.

As I understand it is not advised to use the Application class for such use, what would be a good alternative?

Robin
  • 605
  • 2
  • 8
  • 25

3 Answers3

5

You're getting this error because you call this.getApplication() inside the View.OnClickListener. Because of this, this now references the listener and not the activity.

To do what you need, just create a Context object outside of the listener in your activity's onCreate method and assign this to it. And, inside the listener, use context instead of this. Something like this :-

public class AnActivity extends Activity {
    Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mood);
    context = this;
    Button buttonMusic=(Button) findViewById(R.id.button5);
    buttonMusic.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            MyApplication myApplication = ((MyApplication)context.getApplication());//Changed "the" to "context"
            Toast.makeText(MoodActivity.this, "playing music", Toast.LENGTH_SHORT).show();
        }
    });

    }

}

Edit after question update :-

Instead of using the application class, use static objects to achieve global variables. static objects are independent of objects and can be referenced using the class that they belong to. For example, if you define a static variable in your MainActivity.class and name it testObject, then it can be accessed using the following code regardless of your current activity :-

YourObject object = MainActivity.testObject;
Rohan Stark
  • 2,346
  • 9
  • 16
1

Unless you have a specific reason for extending the Application class in Android you probably shouldn't. For reference, look at the note in the documentation for this: https://developer.android.com/reference/android/app/Application.html .

If you are trying to create an object that you can use in your Android app, simply do that as you would in Java:

public class MyObject {
    //Your stuff here
}

If there is a reason that you're specifically wanting to extend the Application class then perhaps there's more that people can do to help you if you explain what you're trying to do. I just don't necessarily see a need to go through all that complexity based on your example :)

  • My point is to have a global object that is shared between various activities. I define my object in the main activity and then do `MyObject myObject=myApplication.getMyObject();` and I do operations on my object. But this actually kills the app, this line makes the app bug. – Robin Jul 03 '17 at 01:26
1

Change this to AnActivity.this. Inside the code below the meaning of this changes from AnActivity to View.onClickListener as it is another object and inside those braces you are in the scope of the click listener class

new View.OnClickListener() {
        public void onClick(View v) {
            MyApplication myApplication = ((MyApplication)this.getApplication());
            Toast.makeText(MoodActivity.this, "playing music", Toast.LENGTH_SHORT).show();
        }
    }

So the code above should become

new View.OnClickListener() {
        public void onClick(View v) {
            MyApplication myApplication = ((MyApplication)AnActivity.this.getApplication());
            Toast.makeText(MoodActivity.this, "playing music", Toast.LENGTH_SHORT).show();
        }
    }

You can read a bit more about it here

Mibac
  • 8,990
  • 5
  • 33
  • 57