有人可以提供有关我的问题的见解吗?
我的应用程序在禁用GPS时有效,但在启用GPS时启动FC。在冷启动时,它提供打开GPS的选项。完成后,按后退箭头按钮返回到应用程序活动(虽然状态栏中看不到GPS图标,但是如何在此处显示?)
当GPS仍处于打开状态时,按后退按钮或使用菜单选项退出应用程序。 现在,当我尝试启动应用程序时,它就是FC。 FC消息后面会出现闪烁的GPS图标,并在我确认FC时消失。
以下是代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getLocation();
setupWebView();
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
private void setupWebView() {
final String centerURL = "javascript:centerAt("
+ mostRecentLocation.getLatitude() + ","
+ mostRecentLocation.getLongitude() + ")";
webView = (WebView) findViewById(R.id.webview);
// Get rid of white line on the right
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.getSettings().setJavaScriptEnabled(true);
// Wait for the page to load then send the location information
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
webView.loadUrl(centerURL);
}
});
webView.loadUrl(MAP_URL);
}
/**
* The Location Manager manages location providers. This code searches for
* the best provider of data (GPS, WiFi/cell phone tower lookup, some other
* mechanism) and finds the last known location.
**/
private void getLocation() {
locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
createGpsDisabledAlert();
}
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
provider = locationManager.getBestProvider(criteria, true);
// In order to make sure the device is getting location, request
// updates. Look to tweaking the arguments to this for a smooth ride
locationManager.requestLocationUpdates(provider, 1, 0, this);
mostRecentLocation = locationManager.getLastKnownLocation(provider);
}
private void createGpsDisabledAlert() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS is disabled! Would you like to enable it?")
.setCancelable(false)
.setPositiveButton("Enable GPS",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
showGpsOptions();
}
});
builder.setNegativeButton("Use Wifi or 3g for location",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
private void showGpsOptions() {
Intent gpsOptionsIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(gpsOptionsIntent);
}
// Menu setup
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, R.string.close).setIcon(
android.R.drawable.ic_menu_close_clear_cancel);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
super.onStop();
super.onDestroy();
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onResume() {
locationManager.requestLocationUpdates(provider, 1, 0, this);
super.onResume();
}
@Override
protected void onPause() {
locationManager.removeUpdates(this);
super.onPause();
}
/** Sets the mostRecentLocation object to the current location of the device **/
public void onLocationChanged(Location location) {
mostRecentLocation = location;
final String centerURL = "javascript:centerAt("
+ mostRecentLocation.getLatitude() + ","
+ mostRecentLocation.getLongitude() + ")";
// This line is good to update the map with the current location
webView.loadUrl(centerURL);
}
这是logcat 来自日食:
11-04 11:45:10.774: INFO/ActivityManager(52): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.weuni.android.mups/.ViewCampusActivity }
11-04 11:45:10.925: INFO/ActivityManager(52): Start proc com.weuni.android.mups for activity com.weuni.android.mups/.ViewCampusActivity: pid=225 uid=10030 gids={3003, 1015}
11-04 11:45:10.934: DEBUG/AndroidRuntime(197): Shutting down VM
11-04 11:45:10.944: DEBUG/dalvikvm(197): DestroyJavaVM waiting for non-daemon threads to exit
11-04 11:45:10.954: DEBUG/dalvikvm(197): DestroyJavaVM shutting VM down
11-04 11:45:10.954: DEBUG/dalvikvm(197): HeapWorker thread shutting down
11-04 11:45:10.954: DEBUG/dalvikvm(197): HeapWorker thread has shut down
11-04 11:45:10.954: DEBUG/jdwp(197): JDWP shutting down net...
11-04 11:45:10.964: INFO/jdwp(197): adbd disconnected
11-04 11:45:10.974: DEBUG/dalvikvm(197): VM cleaning up
11-04 11:45:11.055: ERROR/AndroidRuntime(197): ERROR: thread attach failed
11-04 11:45:11.185: DEBUG/dalvikvm(197): LinearAlloc 0x0 used 636388 of 5242880 (12%)
11-04 11:45:11.485: DEBUG/ddm-heap(209): Got feature list request
11-04 11:45:11.555: DEBUG/NetworkLocationService(52): onCreate
11-04 11:45:11.555: DEBUG/ddm-heap(225): Got feature list request
11-04 11:45:11.595: DEBUG/LocationManager(52): Constructor: service = com.android.server.LocationManagerService@43ceb720
11-04 11:45:11.625: ERROR/ActivityThread(52): Failed to find provider info for com.google.settings
11-04 11:45:11.625: ERROR/ActivityThread(52): Failed to find provider info for com.google.settings
11-04 11:45:11.934: INFO/ARMAssembler(52): generated scanline__00000177:03515104_00000001_00000000 [ 73 ipp] (95 ins) at [0x45ef00:0x45f07c] in 1428001 ns
11-04 11:45:12.004: INFO/ARMAssembler(52): generated scanline__00000077:03545404_00000004_00000000 [ 47 ipp] (67 ins) at [0x33bf28:0x33c034] in 772000 ns
11-04 11:45:12.185: WARN/WindowManager(52): No window to dispatch pointer action 0
11-04 11:45:12.195: WARN/WindowManager(52): No window to dispatch pointer action 1
11-04 11:45:12.724: DEBUG/LocationManager(225): Constructor: service = android.location.ILocationManager$Stub$Proxy@43d14648
11-04 11:45:12.794: DEBUG/GpsLocationProvider(52): setMinTime 1
11-04 11:45:12.794: DEBUG/GpsLocationProvider(52): startNavigating
11-04 11:45:12.834: DEBUG/AndroidRuntime(225): Shutting down VM
11-04 11:45:12.834: WARN/dalvikvm(225): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
11-04 11:45:12.834: ERROR/AndroidRuntime(225): Uncaught handler: thread main exiting due to uncaught exception
11-04 11:45:12.894: ERROR/AndroidRuntime(225): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.weuni.android.mups/com.weuni.android.mups.ViewCampusActivity}: java.lang.NullPointerException
11-04 11:45:12.894: ERROR/AndroidRuntime(225): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
11-04 11:45:12.894: ERROR/AndroidRuntime(225): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
11-04 11:45:12.894: ERROR/AndroidRuntime(225): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
11-04 11:45:12.894: ERROR/AndroidRuntime(225): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
11-04 11:45:12.894: ERROR/AndroidRuntime(225): at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 11:45:12.894: ERROR/AndroidRuntime(225): at android.os.Looper.loop(Looper.java:123)
11-04 11:45:12.894: ERROR/AndroidRuntime(225): at android.app.ActivityThread.main(ActivityThread.java:4363)
11-04 11:45:12.894: ERROR/AndroidRuntime(225): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 11:45:12.894: ERROR/AndroidRuntime(225): at java.lang.reflect.Method.invoke(Method.java:521)
11-04 11:45:12.894: ERROR/AndroidRuntime(225): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-04 11:45:12.894: ERROR/AndroidRuntime(225): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-04 11:45:12.894: ERROR/AndroidRuntime(225): at dalvik.system.NativeStart.main(Native Method)
11-04 11:45:12.894: ERROR/AndroidRuntime(225): Caused by: java.lang.NullPointerException
11-04 11:45:12.894: ERROR/AndroidRuntime(225): at com.weuni.android.mups.ViewCampusActivity.setupWebView(ViewCampusActivity.java:41)
11-04 11:45:12.894: ERROR/AndroidRuntime(225): at com.weuni.android.mups.ViewCampusActivity.onCreate(ViewCampusActivity.java:35)
11-04 11:45:12.894: ERROR/AndroidRuntime(225): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-04 11:45:12.894: ERROR/AndroidRuntime(225): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
11-04 11:45:12.894: ERROR/AndroidRuntime(225): ... 11 more
11-04 11:45:12.934: INFO/Process(52): Sending signal. PID: 225 SIG: 3
11-04 11:45:12.944: INFO/dalvikvm(225): threadid=7: reacting to signal 3
11-04 11:45:12.944: ERROR/dalvikvm(225): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
11-04 11:45:13.165: DEBUG/ddm-heap(144): Got feature list request
11-04 11:45:13.265: DEBUG/ddm-heap(158): Got feature list request
11-04 11:45:13.315: INFO/ARMAssembler(52): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x4bdcb0:0x4bdd6c] in 591000 ns
11-04 11:45:13.345: INFO/ARMAssembler(52): generated scanline__00000177:03515104_00001001_00000000 [ 91 ipp] (114 ins) at [0x4bdd70:0x4bdf38] in 752000 ns
11-04 11:45:13.375: DEBUG/ddm-heap(182): Got feature list request
11-04 11:45:13.385: DEBUG/ddm-heap(198): Got feature list request
11-04 11:45:14.265: DEBUG/NetworkLocationProvider(52): updateNetworkState(): Updating network state to 2
11-04 11:45:14.294: DEBUG/NetworkLocationProvider(52): onCellLocationChanged [0,0]
11-04 11:45:14.305: DEBUG/NetworkLocationProvider(52): onDataConnectionStateChanged 3
11-04 11:45:14.844: INFO/ActivityManager(52): Start proc com.svox.pico for broadcast com.svox.pico/.VoiceDataInstallerReceiver: pid=242 uid=10019 gids={}
11-04 11:45:14.995: DEBUG/ddm-heap(242): Got feature list request
11-04 11:45:15.624: DEBUG/MediaScanner(158): opendir /system/media/ failed, errno: 2
11-04 11:45:15.634: DEBUG/MediaScanner(158): prescan time: 5425ms
11-04 11:45:15.634: DEBUG/MediaScanner(158): scan time: 18ms
11-04 11:45:15.644: DEBUG/MediaScanner(158): postscan time: 2ms
11-04 11:45:15.644: DEBUG/MediaScanner(158): total time: 5445ms
11-04 11:45:15.664: DEBUG/MediaScannerService(158): done scanning volume internal
11-04 11:45:15.674: DEBUG/MediaScannerService(158): start scanning volume external
11-04 11:45:15.694: VERBOSE/MediaProvider(158): /sdcard volume ID: 283779103
11-04 11:45:15.835: VERBOSE/MediaProvider(158): Attached volume: external
11-04 11:45:15.995: VERBOSE/MediaScanner(158): pruneDeadThumbnailFiles... android.database.sqlite.SQLiteCursor@43bb5fd0
11-04 11:45:16.005: VERBOSE/MediaScanner(158): /pruneDeadThumbnailFiles... android.database.sqlite.SQLiteCursor@43bb5fd0
11-04 11:45:16.036: DEBUG/MediaScanner(158): prescan time: 123ms
11-04 11:45:16.036: DEBUG/MediaScanner(158): scan time: 4ms
11-04 11:45:16.036: DEBUG/MediaScanner(158): postscan time: 52ms
11-04 11:45:16.036: DEBUG/MediaScanner(158): total time: 179ms
11-04 11:45:16.065: DEBUG/MediaScannerService(158): done scanning volume external
11-04 11:45:19.655: INFO/Process(225): Sending signal. PID: 225 SIG: 9
11-04 11:45:19.685: INFO/ActivityManager(52): Process com.weuni.android.mups (pid 225) has died.
11-04 11:45:19.685: WARN/GpsLocationProvider(52): Unneeded remove listener for uid 1000
11-04 11:45:19.685: DEBUG/GpsLocationProvider(52): stopNavigating
11-04 11:45:19.795: INFO/UsageStats(52): Unexpected resume of com.android.launcher while already resumed in com.weuni.android.mups
11-04 11:45:20.085: WARN/InputManagerService(52): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@43d2d030
11-04 11:45:24.585: WARN/ResourceType(52): Resources don't contain package for resource number 0x7f0700e5
11-04 11:45:24.595: WARN/ResourceType(52): Resources don't contain package for resource number 0x7f020031
11-04 11:45:24.595: WARN/ResourceType(52): Resources don't contain package for resource number 0x7f020030
11-04 11:45:24.595: WARN/ResourceType(52): Resources don't contain package for resource number 0x7f050000
11-04 11:45:24.605: WARN/ResourceType(52): Resources don't contain package for resource number 0x7f060001
11-04 11:45:24.625: WARN/ResourceType(52): Resources don't contain package for resource number 0x7f060000
答案 0 :(得分:1)
11-04 11:45:12.894: ERROR/AndroidRuntime(225): Caused by: java.lang.NullPointerException 11-04 11:45:12.894: ERROR/AndroidRuntime(225): at com.weuni.android.mups.ViewCampusActivity.setupWebView(ViewCampusActivity.java:41)
看起来你正试图在你的活动的setupWebView()方法中访问一个空对象,特别是第41行。因为我们没有所有的代码而且我不能准确计算这些行,所以我愿意在尝试致电mostRecentLocation
和mostRecentLocation.getLatitude()
时,您的mostRecentLocation.getLongitude
对象为空。在尝试调用这些方法之前检查它是否为空。 GPS接收器有时可能很慢并且可能需要一段时间来实际获取位置。
评论回复:
问题是您在实际接收位置之前调用setupWebView()。如果mostRecentLocation
为空,那么在收到位置之前,您无法真正做到这一点。
您可能需要重新考虑一下您的应用是如何运作的。如果mostRecentLocation
为空,则可能会显示错误或对话框,表明您正在等待某个位置。然后,当实际收到某个位置时,系统会调用onLocationChanged
,并在WebView
处设置新位置。