Android Question moving on to classes but I'm stuck with addview

sorex

Expert
Licensed User
Longtime User
I'm changing some code module to a class as it seems more flexible and easier to use with some activity views.

But can we use addview inside classes aswell?

I'm stuck at this...

B4X:
btnMenu.Initialize("menubutton")
Main.AddView(btnMenu,0,0,0,0)

the last line gives an "Unknown member : addview" error, it only list the global stuff.

I can declare an activity variable but I don't know how to link my main activity to it.

I'm also not able to access a view in my Main activity, altho I can bypass it by storing the position/size in a variable in the main activity tho so it's less important.

I went throught the classes tutorial thread but it doesn't reveal much on this topic.
 

thedesolatesoul

Expert
Licensed User
Longtime User
I can declare an activity variable but I don't know how to link my main activity to it.

B4X:
Sub Class_Globals
Private myActivity as Activity
End Class_Globals

Sub Initialize(paramActivity as Activity)
  myActivity = paramActivity 'Now we have the activity in a local variable
End Sub
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
Thanks TDS, that is solved.

Now I have another issue :)

I have 2 classes lets name them class1 & class2.

My class1 contains a map.

When I'm creating my button set in class2 I call a sub in class1 but when I read out mymap.size in that sub (both in class1) it crashes with a nullpointer exeption.

It's like it can't see my map.

It works fine when calling the same sub from in the main activity.

Is this a scoping problem or something else?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I think it is caused by going in & out of the classes due to limitations.

in Class2 I download a file but the job handler only seems to work when put in the main activity.

in the job complete I call a sub in class2 again which calls one in class1 too.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
According to HttpUtils2 documentation the JobDone sub should be raised in classes as well. (Infact I have used it in my Weatherfeed class).
This would allow you to completely encapsulate the fetching of data so I would recommend putting job done in class2.

When you were reading mymap.size from class1, was class1 declared in class2 or was it declared in main?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
intellisense says that the second parameter is the module containing the sub defined as parameter 1

Main.dljob.Initialize("update","Main")


I've attached an example of the problem.
 

Attachments

  • classesstuff.zip
    2.9 KB · Views: 130
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Ok, I am not entirely sure what you are doing here, so I will try to explain what I think is wrong.

Main:
myclass1 and f are both Global variables but only initialized in FirstTime. They should always be initialized.
Anyway, as for this example it doesnt matter.

Class1:
OK

functions:
If you want, you can declare dljob within the Class_Globals of functions so you never have to refer to Main, and functions becomes independent of Main.
This was if you do dljob.Initialize("update",Me)
The JobDone will be raised in functions instead of Main.

Now in getData,
you declare another instance of class1 called myclass2. Remember that this instance is difference from the myclass1 you declared in Main. These two have no link. So if you did myclass2.Initialize after declaring it, you would not get a NullPointerException as that would initialize the map in myclass2.

Now depending on your program structure, we need to think whether we need an instance of Class1 inside functions, OR, we have an instance of it in Main and we pass it to functions in the same way we passed the activity.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
in the meanwhile I copied the download stuff to the functions class.

using

B4X:
dljob.Initialize("update","functions")

doesn't seem to work and give this error > java.lang.RuntimeException: java.lang.NoSuchFieldException: processBA

Edit in this case ME works right (in the main activity this gives back appname.com)

B4X:
dljob.Initialize("update",me)

is correct, thanks.

now it reaches the null pointer exeption again :)
 
Last edited:
Upvote 0

sorex

Expert
Licensed User
Longtime User
hmm, with the initialize called it works!

but I don't get it, the initialize just stores a number that has nothing to do with that map.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
but I have it initialized in the main activity already.

even more weird... when I change it to

B4X:
Sub Initialize(testval)
Log("testval:" & testval)
mymap.Initialize
If mytimer.IsInitialized=False Then
    mytimer.Initialize("timerupdate",1000)
    mytimer.Enabled=False
Else
    Log("skipping timer init!!!!!!!!!!!!!!!!!")
End If
End Sub

I don't see the "skipping" appearing in the log, although the init is called twice.

I see that the map size grew in the ADD routine which looked fine, but in my timer the size suddenly becomes 1 holding the last item only.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
See, what you define as Class1 is just the Class definition. It has no existence (for e.g. a code module is always there). But the class cannot do anything until you instantiate it.
A new class instance is created everytime you Initialize it.
In your case you have two class instances, one in main and the other in functions. They are both independent objects.
So I never expect to see the skipping message above as everytime you call Initialize, the class and all its containing objects like the timer and map will be created again.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I ditched the declaration and init in the main activity so there's only one left, all seems fine now.

Thanks again for the detailed explanations and example code.
 
Upvote 0
Top