cn1中有一个位置管理器,对我的项目来说效果很好。但我需要每隔几分钟使用jobscheduler更新位置值。我已经使用本机接口完成了jobScheduler,并且在其方法之一中需要gps代码(这里是onStartJob()方法)。所以我不想再次在本机原生android中编码位置管理器(gps)。因此我直接在本机接口中使用了cn1位置提供程序,它给出了错误。我可以在原生界面smhow中混合使用cn1吗?
public class MyJobService extends JobService {
@Override
public boolean onStartJob(JobParameters jobParameters) {
if (Display.isInitialized()) {//updated
checkGPS();
}
Toast.makeText(this,
"Live track running with jobScheduler",
Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onStopJob(JobParameters jobParameters) {
Toast.makeText(this,
"Live track running with jobScheduler stops",
Toast.LENGTH_SHORT).show();
return false;
}
//using cn1 location manager...
public void checkGPS() {
LocationManager.getLocationManager().setLocationListener(new LocationListener() {
@Override
public void locationUpdated(Location location) {
loc = location;
latitude = location.getLatitude();
longitude = location.getLongitude();
System.out.println("locationUpdated latitudeeee " + latitude);
System.out.println("locationUpdated longitudeeee " + longitude);
}
@Override
public void providerStateChanged(int newState) {
System.out.println("newState " + newState);
}
});
}
}
错误日志:
09-11 10:48:14.980 15455-15455/? E/JobService: Error while executing job: 1
09-11 10:48:14.990 15455-15455/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.capitaleyenepal.nokia, PID: 15455
java.lang.RuntimeException: java.lang.NullPointerException: Attempt to invoke virtual method 'com.codename1.location.LocationManager com.codename1.impl.CodenameOneImplementation.getLocationManager()' on a null object reference
at android.app.job.JobService$JobHandler.handleMessage(JobService.java:130)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7409)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.codename1.location.LocationManager com.codename1.impl.CodenameOneImplementation.getLocationManager()' on a null object reference
at com.codename1.ui.Display.getLocationManager(Display.java:3091)
at com.codename1.location.LocationManager.getLocationManager(LocationManager.java:70)
at com.capitaleyenepal.nokia.MyJobService.checkGPS(MyJobService.java:56)
at com.capitaleyenepal.nokia.MyJobService.onStartJob(MyJobService.java:30)
at android.app.job.JobService$JobHandler.handleMessage(JobService.java:126)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7409)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Update1:如果我删除了具有cn1代码的checkGPS()方法,则jobScheduler可以正常工作。
@Override
public boolean onStartJob(JobParameters jobParameters) {
if (Display.isInitialized()) {
checkGPS();
}
return false;
}
调试时出错(app crashes)
10-18 11:33:36.608 2580-2726/? E/ActivityManager: ANR in com.capitaleyenepal.nokia
PID: 15661
Reason: executing service com.capitaleyenepal.nokia/.MyJobService
Load: 0.0 / 0.0 / 0.0
CPU usage from 97817ms to 0ms ago:
5.6% 2580/system_server: 3.6% user + 2% kernel / faults: 8228 minor 44 major
3.2% 1102/mmcqd/0: 0% user + 3.2% kernel
2.1% 3681/com.samsung.android.providers.context: 1.2% user + 0.8% kernel / faults: 2529 minor 397 major
0.9% 1262/mc_fastcall: 0% user + 0.9% kernel
0.5% 22199/com.sec.spp.push:RemoteDlcProcess: 0.3% user + 0.2% kernel / faults: 1906 minor 1027 major
0.9% 3136/com.android.systemui: 0.7% user + 0.1% kernel / faults: 11113 minor 7 major
0.7% 24072/adbd: 0.2% user + 0.5% kernel / faults: 1053 minor
0.5% 16674/logcat: 0.2% user + 0.3% kernel / faults: 17 minor
0.3% 7253/com.android.bluetooth: 0.2% user + 0.1% kernel / faults: 1791 minor
0.3% 4423/com.google.android.gms.persistent: 0.2% user + 0% kernel / faults: 1875 minor
0.3% 2241/jbd2/dm-0-8: 0% user + 0.3% kernel
0.2% 2115/servicemanager: 0.1% user + 0.1% kernel
0.2% 7549/com.samsung.android.sm.provider: 0.1% user + 0% kernel / faults: 768 minor
0.1% 2117/surfaceflinger: 0% user + 0.1% kernel / faults: 127 minor
0.1% 2268/argosd: 0.1% user + 0% kernel
0.1% 3114/com.android.phone: 0.1% user + 0% kernel / faults: 333 minor
0% 2284/fingerprintd: 0% user + 0% kernel / faults: 1234 minor
0.1% 15594/kworker/0:3: 0% user + 0.1% kernel
0.1% 15661/com.capitaleyenepal.nokia: 0.1% user + 0% kernel / faults: 2 minor
0.1% 16390/com.google.android.gms: 0.1% user + 0% kernel / faults: 3539 minor 2 major
0.1% 15207/kworker/1:1: 0% user + 0.1% kernel
0.1% 16358/com.google.android.gms.persistent: 0% user + 0% kernel / faults: 1797 minor
0% 2277/sdp_cryptod: 0% user + 0% kernel / faults: 74 minor
0.1% 3403/android.process.acore: 0% user + 0% kernel / faults: 182 minor
0.1% 12561/cfinteractive0: 0% user + 0.1% kernel
0.1% 7/rcu_preempt: 0% user + 0.1% kernel
0.1% 7424/com.samsung.android.sm: 0% user + 0% kernel / faults: 541 minor
0% 15160/kworker/2:1: 0% user + 0% kernel
0% 7618/com.opera.max.oem: 0% user + 0% kernel / faults: 43 minor
0% 16656/kworker/3:0: 0% user + 0% kernel
0% 2114/lmkd: 0% user + 0% kernel
0% 9858/com.sec.spp.push: 0% user + 0% kernel / faults: 15 minor
0% 1453/dhd_dpc: 0% user + 0% kernel
0% 10/migration/0: 0% user + 0% kernel
0% 13/migration/1: 0% user + 0% kernel
0% 23/migration/3: 0% user + 0% kernel
0% 2086/kworker/0:1H: 0% user + 0% kernel
0% 13720/com.facebook.katana: 0% user + 0% kernel / faults: 426 minor 1 major
0% 16211/com.samsung.cmh:CMH: 0% user + 0% kernel / faults: 38 minor
0% 25159/com.google.android.gms: 0% user + 0% kernel / faults: 2183 minor
0% 18/migration/2: 0% user + 0% kernel
0% 28/migration/4: 0% user + 0% kernel
0% 616/ion_noncontig_h: 0% user + 0% kernel
0% 686/kswapd0: 0% user + 0% kernel
0% 2113/healthd: 0% user + 0% kernel
0% 12378/wpa_supplicant: 0% user + 0% kernel / faults: 1 minor
0% 16576/com.sec.android.daemonapp: 0% user + 0% kernel / faults: 254 minor
0% 17723/com.sec.android.inputmethod: 0% user + 0% kernel / faults: 2743 minor
0% 1//init: 0% user + 0% kernel / faults: 46 minor
0% 30/kworker/4:0: 0% user + 0% kernel
0% 33/migration/5: 0% user + 0% kernel
0% 38/migration/6: 0% user + 0% kernel
0% 1475/ueventd: 0% user + 0% kernel
0% 2122/tee_scheduler: 0% user + 0% kernel
0% 2252/edmaudit: 0% user + 0% kernel
0% 2257/rild: 0% user + 0% kernel / faults: 35 minor
0% 2323/rild: 0% user + 0% kernel / faults: 53 minor
0% 16370/com.google.process.gapps: 0% user + 0% kernel / faults: 31 minor
0% 8/rcu_sched: 0% user + 0% kernel
0% 14/ksoftirqd/1: 0% user + 0% kernel
0% 43/migration/7: 0% user + 0% kernel
0% 870/hwrng: 0% user + 0% kernel
0% 1451/dhd_watchdog_th: 0% user + 0% kernel
0% 2087/kworker/2:1H: 0% user + 0% kernel
0% 2116/mcDriverDaemon: 0% user + 0% kernel / faults: 656 minor
0% 2254/netd: 0% user + 0% kernel / faults: 17 minor
0% 2278/diagexe: 0% user + 0% kernel
0% 2913/tlc_s
checkGps()方法
public void checkGPS() {
LocationManager.getLocationManager().setLocationListener(new LocationListener() {
@Override
public void locationUpdated(Location location) {
loc = location;
latitude = location.getLatitude();
longitude = location.getLongitude();
System.out.println("locationUpdated latitudeeee " + latitude);
System.out.println("locationUpdated longitudeeee " + longitude);
}
@Override
public void providerStateChanged(int newState) {
System.out.println("newState " + newState);
}
});
}
答案 0 :(得分:1)
这种情况正在发生,因为Codename One并未运行。您需要确保使用以下代码初始化Display
类:
if(Display.isInitialized()) {
...
}
如果不是,你可以尝试调用init
并传递你的上下文对象,如果可能的话,我会反对它,因为有一些细微差别。如果您选择遵循该路径,我建议您查看AndroidImplementation.java
文件。