Im trying to Contol LEDs using my android device and I'am using Basic4android for the app. I got things working but everytime I press a button to turn an LED on/off.. I got this error saying "android.os.NetworkOnMainThreadException" ... This is my code:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim request As HttpRequest
Dim HttpClient1 As HttpClient
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim Button1 As Button
Dim Button2 As Button
Dim Button3 As Button
Dim Button4 As Button
Dim Button5 As Button
Dim Button6 As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("main")
HttpClient1.Initialize("HttpClient1")
End Sub
Sub HttpClient1_ResponseSuccess (Response As HttpResponse, TaskId As Int)
Dim resultString As String
resultString = Response.GetString("UTF8")
End Sub
Sub HttpClient1_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
Log("Error connecting: " & Reason &" "& StatusCode)
If Response <> Null Then
Log(Response.GetString("UTF8"))
Response.Release
End If
End Sub
Sub Button6_Click
request.InitializeGet("http://192.168.0.8/?BlueOFF")
HttpClient1.Execute(request, 1)
End Sub
Sub Button5_Click
request.InitializeGet("http://192.168.0.8/?BlueON")
HttpClient1.Execute(request, 1)
End Sub
Sub Button4_Click
request.InitializeGet("http://192.168.0.8/?GreenOFF")
HttpClient1.Execute(request, 1)
End Sub
Sub Button3_Click
request.InitializeGet("http://192.168.0.8/?GreenON")
HttpClient1.Execute(request, 1)
End Sub
Sub Button2_Click
request.InitializeGet("http://192.168.0.8/?RedOFF")
HttpClient1.Execute(request, 1)
End Sub
Sub Button1_Click
request.InitializeGet("http://192.168.0.8/?RedON")
HttpClient1.Execute(request, 1)
End Sub
I've researched this issue and it says that I'am doing a network operation on the main thread... I'm new to basic4android.. any tips on how to do the network operations an a separate thread? any help is really appreciated.. thank you :D
best regards,
Caldwell D.
There are two solutions. The simplest would be to disable the strict mode on Android version greater API 9:
Sub DisableStrictMode
Dim jo As JavaObject
jo.InitializeStatic("android.os.Build.VERSION")
If jo.GetField("SDK_INT") > 9 Then
Dim policy As JavaObject
policy = policy.InitializeNewInstance("android.os.StrictMode.ThreadPolicy.Builder", Null)
policy = policy.RunMethodJO("permitAll", Null).RunMethodJO("build", Null)
Dim sm As JavaObject
sm.InitializeStatic("android.os.StrictMode").RunMethod("setThreadPolicy", Array(policy))
End If
End Sub
Better solution would be to replace the Response.GetString() calls (which are deprecated) with the Asynchronous Response.GetAsynchronously() calls.
Related
I have a form to enter all necessary data. With the wizard I created a button that saves the data as a new record, but this button clears the form.
I want this button to do exactly that, but keep some of the entered values in the form, because those values will stay the same for a certain amount of records.
Currently my button runs this code generated by the button creation wizard:
Private Sub submit_btn_Click()
On Error GoTo submit_btn_Click_Err
On Error Resume Next
DoCmd.GoToRecord , "", acNewRec
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
End If
submit_btn_Click_Exit:
Exit Sub
submit_btn_Click_Err:
MsgBox Error$
Resume submit_btn_Click_Exit
End Sub
When the button is clicked I want to clear all values in the form, except for the date and a group field. Can I easily do this in this code or is there a way to do this via the default value property of these fields?
Try the following:
Go to the properties of your form and put the following code in the BeforeUpdate event:
Private Sub Form_BeforeUpdate(Cancel As Integer)
entry_date.Tag = CLng(entry_date.Value)
acc_value.Tag = acc_value.Value
End Sub
In the Current event of the form, put the following code:
Private Sub Form_Current()
If Me.NewRecord Then
entry_date = CDate(entry_date.Tag)
acc_value = acc_value.Tag
End If
End Sub
That is all you need to do. If you have any questions please let me know
I have the following VBA code to enable a textbox everytime a checkbox is clicked.
Private Sub CheckName1_Click()
If CheckName1.Value = True Then
TextName1.Enabled = False
TextName1.SpecialEffect = fmSpecialEffectFlat
Else
TextName1.Enabled = True
TextName1.SpecialEffect = fmSpecialEffectSunken
End If
End Sub
The problem is my document will probably have 30 of these by the time I am done (CheckName1, CheckName2, CheckRent1, CheckRent2, etc). I am already having trouble with Word lagging, so I'd like to create a subroutine that will call this sub instead of copy/pasting it again and again.
I haven't spent a lot of time with functions and the like- and even less with VBA. I just know the basics and they always make my head spin. So, this is what I have.
I just don't know how to pass the textbox parameter, or if I even have the checkbox parameter right.
And if this is close, do I still create a call for each checkbox?
Private Sub CheckName1_Click()
Call NA
End Sub
Private Sub NA(checkbox, textbox)
If checkbox.value = True Then
textbox.Enabled = False
textbox.SpecialEffect = fmSpecialEffectFlat
Else
textbox.Enabled = True
textbox.SpecialEffect = fmSpecialEffectSunken
End Sub
Thanks in advance for any help!
Loop on all controls
Based on this example: http://www.ozgrid.com/VBA/control-loop.htm
Dim cCont As Control
For Each cCont In Me.Controls
If TypeName(cCont) = "TextBox" Then
'DO STUFF HERE
End If
Next cCont
I'm making a server checkpoint in my new version for this app. and I'm encountering this error
when my app tries to open my main form.
An error occurred creating the form. See Exception.InnerException for
details. The error is: ActiveX control
'6bf52a52-394a-11d3-b153-00c04f79faa6' cannot be instantiated because
the current thread is not in a single-threaded apartment.
I'm not sure what this is, because it's something like a usual form except this form is used as Splash screen. When I remove the splash screen the form opens normally with all plugins and modules.
Here's a part of the code I used
Public Class example_form
Public Function servercheck() As Boolean
Dim objUrl As New System.Uri("http://google.com")
Dim objWebReq As System.Net.WebRequest
objWebReq = System.Net.WebRequest.Create(objUrl)
Dim objresp As System.Net.WebResponse
Try
objresp = objWebReq.GetResponse
objresp.Close()
objresp = Nothing
Return True
Catch ex As Exception
objresp = Nothing
objWebReq = Nothing
Return False
End Try
End Function
Private Sub Form4_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
BackgroundWorker1.RunWorkerAsync()
Control.CheckForIllegalCrossThreadCalls = False
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
If servercheck() = True Then
Form1.Show()
Me.Hide()
BackgroundWorker1.CancelAsync()
Else : PictureBox1.Image = My.Resources._12383u9
MsgBox("some text here", MsgBoxStyle.Critical)
End
End If
End Sub
End Class
Now you have the code. The error is on the background worker when it tries to open the form. (at the end of the code)
Ok i found what is the error. For those who have this error, do not add the form.show() command in the backgroundworker in work mode. the form should be load when backgroundworker will finish. so the correct code is like this
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
If servercheck() = True Then
Me.Hide()
BackgroundWorker1.CancelAsync()
Else : PictureBox1.Image = My.Resources._12383u9
MsgBox("Some text here", MsgBoxStyle.Critical)
End
End If
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Form1.Show()
End Sub
I am writing a code in MS Access VBA, which is as follows:
Private Sub Form_Load()
MsgBox "loggedIn = " + CStr(loggedIn)
If (loggedIn = 1) Then
Else
Exit Sub
End If
End Sub
I want to decide whether to load the form or not based on loggedIn variable. If loggedIn variable is 1, form shall be loaded. If the same is not 1, form shall not be loaded.
Issue that I am facing is that, whatever I do in ELSE part of the code above, I am not able to stop the form from loading.
How can I achieve this?
Pls comment if any additional information is required.
Thank you.
If the loggedIn value is available at form open, you could cancel the form open event.
Private Sub Form_Open(Cancel As Integer)
Cancel = Not (loggedIn = 1)
End Sub
If the value of loggedIn is not available until the form's load event, you can close the form.
Private Sub Form_Load()
If loggedIn <> 1 Then
DoCmd.Close acForm, Me.Name
End If
End Sub
Private Sub OccurrenceName_AfterUpdate()
If OccurrenceName.Value = "Other" Then
Dim strTechID As String
Dim strOccurrenceCt As String
Dim strOccurrenceDate As String
strTechID = Me.Parent.tbxTechID.Value
strOccurrenceCt = Forms![frmEmployeeOccurrenceInput]![tbxOccurrence].Value
strOccurrenceDate = Me.OccurrenceDate.Value
Dim strOpenArgs As String
strOpenArgs = strTechID & "|" & strOccurrenceCt & "|" & strOccurrenceDate
DoCmd.OpenForm "frmOtherOccurrence", , , , , , strOpenArgs
Else
Me.OccurrenceAmt = Me.OccurrenceName.Column(1)
Me.Type = Me.OccurrenceName.Column(2)
End If
End Sub
Every time it runs I get "The Open Form action was canceled" with an error code of 2501. The line it gets caught on is the DoCmd.OpenForm call. Debugging give NO additional information.
Here is where the OpenArgs is passed to:
Private Sub Form_Load()
Dim aryOA As Variant
aryOA = Split(Me.OpenArgs, "|")
Me.lblTechID.Caption = aryOA(0)
Me.lblOccurrenceCt.Caption = aryOA(1)
Me.lblOccurrenceDate.Caption = aryOA(2)
End Sub
I don't know if this could be your problem, but you can't pass OpenArgs to an open form, and by open I mean it can't be even in edit mode, should be completely closed.
Otherwise, the form will open (change its status from edit mode to normal) but no OpenArgs will be passed, so OpenArgs will be null and an exception will be thrown.