0

I have an OnCLick listener class in my Android app defined as below. I need to access an application-level variable, but the last line in this code generates this compile error: "The method getApplication() is undefined for the type OnClickListenerSelectPresetItem"

How can I access Application variables from this class?

public class OnClickListenerSelectPresetItem implements OnClickListener {
    private long glbMealId = ((MyApplication) this.getApplication()).getMealId();
Ian M
  • 567
  • 8
  • 33

3 Answers3

3

The error is pretty explicit. this is the listener object, not the Context executing the listener.

You should use ActivityExecutingListener.this.getApplication().

EDIT : If your listener is not an anonymous/inner class, you need to store the Context in the listener instance :

public class OnClickListenerSelectPresetItem implements OnClickListener {
  private long glbMealId;
  private Activity activity;

  public OnClickListenerSelectPresetItem(Activity activity) {
    this.activity = activity; // facultative, but you may need it in onClick() ...
    this.glbMealId = ((MyApplication) activity.getApplication()).getMealId();
  }

  ...
}
pdegand59
  • 12,776
  • 8
  • 51
  • 58
  • I just tried your exact code but the last line gives "The method getApplication() is undefined for the type Context" – Ian M May 27 '14 at 14:54
  • 1
    edited: getApplication() is only available from Activity, not from Context. – pdegand59 May 27 '14 at 14:57
  • So how can I access my Application variable from this class?? (since it is not an Activity class) – Ian M May 27 '14 at 15:00
  • Just pass the activity to the listener with `new OnClickListenerSelectPresetItem(this)` with `this` being an activity (or a Service since `getApplication()` is also available in the `Service`class). See the edited answer. – pdegand59 May 27 '14 at 15:02
  • This is what I've coded in the Activity: textViewItem.setOnClickListener(new OnClickListenerSelectPresetItem(this)); – Ian M May 27 '14 at 15:12
  • Then if you change Context to Activity in the listener, everything will work great. – pdegand59 May 27 '14 at 15:14
  • OK I'm now passing the Activity to the listener, which has got rid of the compile error - thanks to all. I now have an NPE with a getWritableDatabase() call which was previously working fine! Will start a separate thread so that I can post the code... – Ian M May 27 '14 at 15:45
0

The this operator in your case references OnClickListenerSelectPresetItem.

Here is an example of what you're trying to do, cut-pasted from one of my projects:

public class PageFragment extends Fragment {

    private boolean isVisible( View view )
    {
        return true; // fake 
    }

    private class OnLikeClickListener implements OnClickListener
    {
        private boolean isVisible = PageFragment.this.isVisible(); // <------
        @Override
        public void onClick(View v) 
        {
            // Do whatever

        }

    }
}
Vaiden
  • 15,728
  • 7
  • 61
  • 91
  • Sorry I'm not following this! Does it contain any clues as to how I can access my Application variable from my OnClickListener class? – Ian M May 27 '14 at 15:01
  • You're trying to access the enclosing class. I.E. the class holding your OnClickListenerSelectPresetItem. I gave an example of how to access the enclosing class. – Vaiden May 27 '14 at 15:02
  • What is your enclosing class? – Vaiden May 27 '14 at 15:03
  • Actually I don't want to access the enclosing class necessarily. I'm simply trying to access a variable defined in the MyApplication class (which has been created purely to host some "global" variables). – Ian M May 27 '14 at 15:05
  • Is this MyApplication extending the Android Application class, and also set in the Manifest.XML, or just a generic static class? – Vaiden May 27 '14 at 15:08
  • There is no enclosing class - this class is defined in OnClickListenerSelectPresetItem.java. There is an associated, separate Activity class which assigns this listener to its TextViews, but this Activity is an entirely separate class. – Ian M May 27 '14 at 15:08
  • Yes MyApplication extends the Android Application class, and contains only a couple of variable declarations and getters/setters for them – Ian M May 27 '14 at 15:10
  • Then you need a reference to an Activity class that has the getApplication() method. So pass it in the constructor. Just as degand59 suggested. – Vaiden May 27 '14 at 15:13
0

OK. Finally understood what your question is all about.

You wanna reference your implementation of the Application class. So either:

  1. Pass an Activity to the constructor of OnClickListenerSelectPresetItem as @pdegand59 suggested.

  2. Make MyApplication a singleton, for convenience (this is what I do because I don't like passing Activities): Android Application as Singleton

Community
  • 1
  • 1
Vaiden
  • 15,728
  • 7
  • 61
  • 91