簡體   English   中英

無法從進程全局例程訪問活動 object

[英]can't accesso to activity object from process global routine

我正在使用 b4a 11.20,我需要在服務模塊的 Main 中使用一個變量。 主要代碼

Sub Process_Globals
Public targetprice As String
End Sub

Sub Globals
Private btnStop As Button
Private btnStart As Button
Private price As EditText    
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("layMain")
If FirstTime Then
If File.Exists(File.DirInternal, "1.txt") Then
price.Text = File.ReadString(File.DirInternal, "1.txt")
targetprice= price.text
End If
End If
End Sub

Private Sub btnStart_Click
btnStart.Enabled = False
CallSubDelayed(MyService, "Start")
btnStop.Enabled = True
End Sub

Private Sub btnStop_Click
btnStop.Enabled = False
CallSubDelayed(MyService, "Stop")
btnStart.Enabled = True
End Sub

Private Sub Price_TextChanged (Old As String, New As String)
File.WriteString(File.DirInternal, "1.txt", price.Text)    
targetprice= price.text
End Sub

和服務模塊

Sub Process_Globals
Private nid As Int = 1
Private lock As PhoneWakeState
Private  timer1 As Timer
End Sub
Sub Service_Create
lock.PartialLock
timer1.Initialize("Timer1", 3000)
End Sub

Sub Service_Start (StartingIntent As Intent)
Service.StopAutomaticForeground
End Sub

Public Sub Start
Service.StartForeground(nid, CreateNotification("HotBit attiva"))
timer1.Enabled = True
End Sub

Public Sub Stop
timer1.Enabled = False
Service.StopForeground(nid)
End Sub

Sub CreateNotification (Body As String) As Notification
Dim notification As Notification
notification.Initialize2(notification.IMPORTANCE_HIGH)
notification.Icon = "icon"
notification.SetInfo("App attiva", Body, Main)
Return notification
End Sub

Sub Service_Destroy
    lock.ReleasePartialLock
End Sub

Sub timer1_tick
Dim j As HttpJob
j.Initialize("", Me)
j.Download("https://api.hotbit.io/api/v1/market.last?market=KIBA/USDT")
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Log(j.GetString)
Dim parser As JSONParser
parser.Initialize(j.GetString)
Dim root As Map = parser.NextObject
Dim result As String = root.Get("result")
Log(result)
End If
If result > Main.targetprice  Then
Dim n As Notification = CreateNotification($"Prezzo superiore a "$ + result)
n.Notify(nid)
End If
j.Release
End Sub

如您所見,我想使用變量targetpricemainservice module 我在main上的全局進程中將 Public targetprice 聲明Public targetprice as String ,並且每當我在editText(Price)上輸入任何值或使用其textchanged 事件時,我都在使用targetprice = Price.Text

因此,我現在可以從服務模塊調用If result > Main.targetprice Then ,但它什么也沒做,當我調試它時,它會在計時器觸發時自行關閉。 日志:

Logger connesso a:  samsung SM-G985F
--------- beginning of main
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
{"error":null,"result":"0.00002873","id":401651055}
0.00002873
myservice$ResumableSub_timer1_tickresume (java line: 285)
java.lang.NumberFormatException: For input string: "Prezzo superiore a"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at lm.hotbit.myservice$ResumableSub_timer1_tick.resume(myservice.java:285)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:207)
    at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1178)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:226)
    at android.os.Looper.loop(Looper.java:313)
    at android.app.ActivityThread.main(ActivityThread.java:8641)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1133)

謝謝

我不太了解 b4x 生態系統,但閱讀日志我認為變量不是問題。

java.lang.NumberFormatException:對於輸入字符串:“Prezzo Superiore a”

可能意味着您沒有正確構建字符串。

連接符號+應替換為& ,因此以下內容應解決此問題。

Dim n As Notification = CreateNotification($"Prezzo superiore a "$ & result)

TextChanged 事件應該是

File.WriteString(File.DirInternal, "1.txt", price.Text)  
targetprice =price.Text

並且由於您處理的是數字而不是字符串,因此必須將Public targetprice As String替換為Public targetprice As Double

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM