• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Understanding context?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys! Nice to meet you all!

When programming in android, how do I call a class(method) from any other class without running into context issues all the time?

Thanks.

 
author
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by context problems?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean the "problem" of having to have a context to pass?

Activity and Service both extend Context (indirectly), and ContentProvider has a getContext() method, so in a big chunk of the code we write, the Context is a given. Any other class we write--at least any class that's going to do anything requiring a context--is probably created and/or used by one of those components, so it's easy to give that class a Context.
 
G. Plann
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:You mean the "problem" of having to have a context to pass?

Activity and Service both extend Context (indirectly), and ContentProvider has a getContext() method, so in a big chunk of the code we write, the Context is a given. Any other class we write--at least any class that's going to do anything requiring a context--is probably created and/or used by one of those components, so it's easy to give that class a Context.



Thanks for your reply. How do I use getContext to call an external class from any other class? Any examples or further information to help me understand Context would be great. I come from a VB background and this concept is still not sinking in completely.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

G. Plann wrote:How do I use getContext to call an external class from any other class?



I don't understand what you're asking here. What do you mean by "external class"? External to what?

And what do you mean "call a class"? We don't call classes, we call methods.

Any examples or further information to help me understand Context would be great. I come from a VB background and this concept is still not sinking in completely.



In that case, some of your confusion may have little or nothing to do with Android and Context in particular but may be more about Java in general. It's hard to provide more concrete help without an example of what's giving you trouble though.
 
G. Plann
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe I'm doing something wrong then because in eclipse I don't see a way to create a method, only classes. I would like to be able to organize my code better like I do in VB by separating the code into methods that are called from my main activity. I've tried to create classes for just this purpose but I'm always running into these context issues and I don't really understand context or the syntax to use it. Other than this context issue I get along with java just fine. Any help would be appreciated as I don't have any specific problem to solve at the moment but I am starting a new app and I was hoping to get a better grasp on this before I get into it. Thanks for your time.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

G. Plann wrote:Maybe I'm doing something wrong then because in eclipse I don't see a way to create a method, only classes.



Yeah, you definitely need to learn some Java basics, preferably without Eclipse or any other IDE.

Eclipse may or may not have a UI feature to create a method for you. It's been a while since I used it. However, the normal way to create a method is just to type the code.



There, I just created a class.



Now I added a method, called main, to my class.

I would like to be able to organize my code better like I do in VB by separating the code into methods that are called from my main activity.



Yes, both in Java in general and in Android in particular, we break up the responsibilities into classes and methods. The interaction of classes in an Android app is somewhat different than in a "normal" Java app (by which I mean the kind I'm used to. ), and I'm sure it's different than in a typical VB app.

I've tried to create classes for just this purpose but I'm always running into these context issues and I don't really understand context or the syntax to use it. Other than this context issue I get along with java just fine.



Don't take this the wrong way but if you don't understand the "syntax to use [Contexts]", then you don't really get along just fine with Java. If you don't understand how Android uses Contexts in general, or where you should get one when it's not blatantly obvious (such as when "this" IS-A Context), then that's an Android issue.

On the Java side, you should know that it's an object that is needed a lot by Android apps. You should know whether your current class IS-A Context, and how to find that out. You should know that if it IS-A Context, you should just use this, and what that means. You should know that if it's not a Context, but there's a getContext() method available, then you don't need to worry about how it works or where the Context comes from, but that it's sufficient wherever you need to pass a Context. If any of that doesn't make sense to you, then you need to brush up on some Java fundamentals.

Again, not saying this to insult you, just to try to get us both a realistic idea of just how much of the basics of Java you really have a grasp on.

 
G. Plann
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm used to methods and classes being separated in the UI. To always HAVE TO place a public method in a class is a concept that I am just getting used to. You're not going to insult me if I fully understood this I wouldn't be in here asking questions. I do appreciate your courtesy though.

I appreciate the code examples. I do understand the syntax for classes and methods. I also have used THIS for context and tried to use getContext with some degree of success. I guess my issues must be with Android and the way it works with java as you mentioned is a bit different. I am used to OOP where you name it and call to it from anywhere if it is a global. Sometimes java doesn't even feel like an OOP to me with all this context stuff to contend with all the time.

If I name it myMethod and it's in the myClass class, I am used to just saying myClass.myMethod(params) and in java it seems like everytime I try and do this it complains about context.

I was hoping for some clarification on the rules of using context. Apparently it is as complicated as I thought.

It's funny I can write php with my eyes closed but this stuff drives me mad!

All my code ends up in one place because of this and I get this thousand line programs to hunt through. Where in other OOP languages I would just create one method and call to it over and over from anywhere. Nice and neat and organized.

Thanks for your help. I guess there isn't an easy answer.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

G. Plann wrote:I'm used to methods and classes being separated in the UI.


I am not sure what that means... but it seems like you are depending on your IDE too much before you actually understand what is going on. My suggestion (as has already been said) would be to drop the IDE for a bit until you understand the Java language a bit better.


...I do understand the syntax for classes and methods. I also have used THIS for context and tried to use getContext ... I guess my issues must be with Android and the way it works with java as you mentioned is a bit different. I am used to OOP where you name it and call to it from anywhere if it is a global. Sometimes java doesn't even feel like an OOP to me with all this context stuff ... If I name it myMethod and it's in the myClass class, I am used to just saying myClass.myMethod(params) and in java it seems like everytime I try and do this it complains about context.


I gather from this that you don't have a solid grasp on OOP in general or Java to be specific. The last sentence where you talk about myClass.myMethod() causing a complaint about context: this is WAY different than the context stuff you have to think about on Android. The Android Context stuff brought up here is a red herring mentioned because you are in the Android forum and there is a basic assumption about your level of comfort with Java. The confusion most people have here with your question is that you were not specific enough. The error message actually is complaining about the static context. The static part being the most important key word. I understand you probably didn't recognize it as such, because you don't know the language, but whenever you get error messages it is important to tell us the entire and specific message, rather than a generalization.

Anywho, back to the problem: this is really about basic Java. You should stop writing Android applications for the moment, and go to writing normal desktop Java until you understand the language a bit. Oracle has some good Tutorials on Java that make a good starting point. Don't get too hung up on the GUI sections - you won't be using them on Android (which has its own GUI system). But there is a lot there that you can and should pick up. Learning Android and Java at the same time is too difficult. I think it is better to isolate the language first, and the environment second. Android is also pretty hard to write without an IDE doing some of the work for you. So you will have a better go of it with Java without an IDE first. Then pick up the IDE when you are comfortable with the language semantics and compiling steps. Then add Android last.

As a guideline for learning, challenge yourself to this: write your applications so there is only one static method - the public static void main(String[] args) method required to launch the application. Do not make any other static methods for now. The reason is they become a crutch to help you avoid learning or thinking about Objects, and will hurt you when you do transition to Android. I think you will understand more when you learn about instances, and the difference between Object/instance methods and static methods.

I was hoping for some clarification on the rules of using context. Apparently it is as complicated as I thought.


It actually isn't all that difficult when we understand what context you mean. The rules of static vs. instance are straight forward. You just have to understand what an instance is (and by knowing that, what the 'static context' is.) The part that got hung up was not being clear what you meant by context.

All my code ends up in one place because of this and I get this thousand line programs to hunt through.


Well there is another problem. Why do you have thousands lines of code? You should write a small chunk (5-10 lines if you can. Only longer if absolutely necessary), compile, run, test. If it works move to the next chunk of code. So whenever you have a problem it should be pretty clear where the problem is (those few lines since the last time you compiled and ran).
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Luke wrote:

G. Plann wrote:If I name it myMethod and it's in the myClass class, I am used to just saying myClass.myMethod(params) and in java it seems like everytime I try and do this it complains about context.


I gather from this that you don't have a solid grasp on OOP in general or Java to be specific. The last sentence where you talk about myClass.myMethod() causing a complaint about context: this is WAY different than the context stuff you have to think about on Android. The Android Context stuff brought up here is a red herring mentioned because you are in the Android forum and there is a basic assumption about your level of comfort with Java.



Ohhhh! Yeah, that makes more sense. G. Plann, sorry to have led you on a bit of a goose chase. Re-reading it now, I do believe that Steve is correct. And what you describe as the kind of OOP you're used to is in fact not very OO at all.

Well-spotted Steve.
 
G. Plann
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both for your insight. I will carefully read over what you said and do some more research.
 
reply
    Bookmark Topic Watch Topic
  • New Topic