krompa
Lost but trying
Profile
Posts: 257
Reg: Jun 14, 2013
Bristol
8,820
05/26/15 09:02 AM (8 years ago)

Android Custom plugin - Constructor errors

I'm creating a custom plugin. The plugin worked fine as a standalone app - but when I add the code to an existing BT app, I am running into problems. I am constructing items such as TextView, TableRow + other items as follows: TextView tv = new TextView(this); Eclipse is not liking the lack of context in "this". Searching the web I can see I should alter the code to: TableRow row = new TableRow(context.this); But I can't work out what "context" is in a BT 3.0 app. Any ideas? NB. The working code in the standalone app started with public class MainActivity extends Activity { but this was changed in the BT app to public class myCustomPlugin extends BT_fragment{
 
mysps
Code is Art
Profile
Posts: 2082
Reg: May 14, 2011
Palma
33,320
like
05/26/15 09:54 AM (8 years ago)
I believe (this) is (getactivity)) .. or something of that nature. I'm not in front of my computer but I'm sure the Android experts will chime in soon!
 
Dusko
Veteran developer
Profile
Posts: 998
Reg: Oct 13, 2012
Beograd
22,680
like
05/26/15 10:52 AM (8 years ago)
>But I can't work out what "context" is in a BT 3.0 app. You are in for a funny ride. Most of the code you find on GitHub is for activities, while BT 3.0 for Android is based on fragments. "this" is a set of variables for one module, or class or package, or app... For an activity, "this" will serve just fine, it is giving you all the variables that you defined after the definition of the class. In BT 3.0 there is one app, and one class on which everything is based, and everything else is a fragment on top of that class. So, using "this" in BT will refer to that class, and not to the variables that you defined in the class section. So, how to use it? First you have to have this: import android.content.Context; then define public static Context context; in your class. Then in the beginning of your onCreateView method put the following: context = getActivity().getApplicationContext(); Instead of "this" use context and 90% of time you are good.
 
mysps
Code is Art
Profile
Posts: 2082
Reg: May 14, 2011
Palma
33,320
like
05/26/15 10:54 AM (8 years ago)
Perfect Dusko ;)
 
Niraj
buzztouch Evangelist
Profile
Posts: 2943
Reg: Jul 11, 2012
Cerritos
37,930
like
05/26/15 11:54 AM (8 years ago)
Thanks, Dusko, keep teaching me! :-) -- Niraj
 
Dusko
Veteran developer
Profile
Posts: 998
Reg: Oct 13, 2012
Beograd
22,680
like
05/26/15 11:58 AM (8 years ago)
>Thanks, Dusko, keep teaching me! :-) You are welcome! If there is anything else you would like to know about Android programming, just let me know!
 
krompa
Lost but trying
Profile
Posts: 257
Reg: Jun 14, 2013
Bristol
8,820
like
05/26/15 01:32 PM (8 years ago)
Thanks Dusko I'll give it a try when I'm back home and report back - but it sounds gd to me.
 
krompa
Lost but trying
Profile
Posts: 257
Reg: Jun 14, 2013
Bristol
8,820
like
05/26/15 05:00 PM (8 years ago)
Yes Dusko - that worked perfectly. Thank you.
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
05/26/15 07:09 PM (8 years ago)
When it comes to Android, Dusko is *the* BT Source. I always learn something new and useful. Not just 'what', but 'why', which is infinitely more important. Cheers! -- Smug
 
LA
Aspiring developer
Profile
Posts: 3278
Reg: Aug 16, 2012
Jerseyville, IL
42,880
like
05/27/15 04:36 AM (8 years ago)
Yeah you don't need to put "this" so just declare the variable without using "this". LA
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.