Android应用程序在尝试检索和显示GPS坐标时崩溃

时间:2014-06-30 07:41:02

标签: android gps

我正在构建一个显示用户当前纬度和经度的简单应用程序,但是当它试图在模拟器上显示GPS坐标时以及当它试图在真实设备上检索坐标时它会崩溃,我&# 39;一直在寻找答案,仍然可以找到一个

public class Locator extends ActionBarActivity {
 TextView textLat;
 TextView textLong;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    textLat = (TextView)findViewById(R.id.textLat);
    textLong = (TextView)findViewById(R.id.textLong);
    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    LocationListener ll = new locatorlistener();
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
    setContentView(R.layout.activity_locator);
    if (savedInstanceState == null) 
    {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}
class locatorlistener implements LocationListener
{

    @Override
    public void onLocationChanged(Location location) {
        if(location != null)
        {
            double pLong = location.getLongitude();
            double pLat = location.getLatitude();
            textLat.setText(Double.toString(pLat));
            textLong.setText(Double.toString(pLong));
        }

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    Intent intent = getIntent();
    setContentView(R.layout.activity_locator);
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.locator, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_locator,
                container, false);
        return rootView;
    }
}

}

清单文件:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

logcat的:

06-30 05:01:49.079: D/dalvikvm(781): GC_FOR_ALLOC freed 268K, 11% free 3016K/3364K, paused 339ms, total 339ms
06-30 05:01:49.829: I/InputDispatcher(379): Window 'Window{b3f05bf8 u0 com.android.launcher/com.android.launcher2.Launcher}' spent 2082.7ms processing the last input event: MotionEvent(deviceId=0, source=0x00001002, action=0, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (428.0, 370.0)]), policyFlags=0x42000000
06-30 05:01:49.869: I/Choreographer(536): Skipped 720 frames!  The application may be doing too much work on its main thread.
06-30 05:01:49.889: I/InputDispatcher(379): Window 'Window{b3f05bf8 u0 com.android.launcher/com.android.launcher2.Launcher}' spent 2138.6ms processing the last input event: MotionEvent(deviceId=0, source=0x00001002, action=2, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (427.0, 370.0)]), policyFlags=0x42000000
06-30 05:01:49.889: I/InputDispatcher(379): Window 'Window{b3f05bf8 u0 com.android.launcher/com.android.launcher2.Launcher}' spent 2121.0ms processing the last input event: MotionEvent(deviceId=0, source=0x00001002, action=2, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (428.0, 371.0)]), policyFlags=0x42000000
06-30 05:01:49.909: I/InputDispatcher(379): Window 'Window{b3f05bf8 u0 com.android.launcher/com.android.launcher2.Launcher}' spent 2077.7ms processing the last input event: MotionEvent(deviceId=0, source=0x00001002, action=2, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (427.0, 372.0)]), policyFlags=0x42000000
06-30 05:01:49.909: I/InputDispatcher(379): Window 'Window{b3f05bf8 u0 com.android.launcher/com.android.launcher2.Launcher}' spent 2000.4ms processing the last input event: MotionEvent(deviceId=0, source=0x00001002, action=2, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (426.0, 374.0)]), policyFlags=0x42000000
06-30 05:01:50.779: D/dalvikvm(536): GC_FOR_ALLOC freed 2813K, 30% free 7068K/9992K, paused 140ms, total 154ms
06-30 05:01:50.989: E/memtrack(1104): Couldn't load memtrack module (No such file or directory)
06-30 05:01:51.009: E/android.os.Debug(1104): failed to load memtrack module: -2
06-30 05:01:51.259: D/dalvikvm(536): GC_FOR_ALLOC freed 72K, 17% free 8360K/9992K, paused 66ms, total 68ms
06-30 05:01:51.889: I/ActivityManager(379): Delay finish: com.android.settings/.widget.SettingsAppWidgetProvider
06-30 05:01:52.079: I/Choreographer(536): Skipped 547 frames!  The application may be doing too much work on its main thread.
06-30 05:01:52.429: D/dalvikvm(536): GC_FOR_ALLOC freed 1598K, 28% free 7248K/9992K, paused 78ms, total 83ms
06-30 05:01:52.579: I/Choreographer(536): Skipped 64 frames!  The application may be doing too much work on its main thread.
06-30 05:01:52.919: E/SoundPool(379): error loading /system/media/audio/ui/Effect_Tick.ogg
06-30 05:01:52.919: W/AudioService(379): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
06-30 05:01:52.929: E/SoundPool(379): error loading /system/media/audio/ui/Effect_Tick.ogg
06-30 05:01:52.929: W/AudioService(379): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
06-30 05:01:52.929: E/SoundPool(379): error loading /system/media/audio/ui/Effect_Tick.ogg
06-30 05:01:52.929: W/AudioService(379): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
06-30 05:01:52.929: E/SoundPool(379): error loading /system/media/audio/ui/Effect_Tick.ogg
06-30 05:01:52.929: W/AudioService(379): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
06-30 05:01:53.069: E/SoundPool(379): error loading /system/media/audio/ui/Effect_Tick.ogg
06-30 05:01:53.069: W/AudioService(379): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
06-30 05:01:53.069: E/SoundPool(379): error loading /system/media/audio/ui/KeypressStandard.ogg
06-30 05:01:53.099: W/AudioService(379): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
06-30 05:01:53.109: E/SoundPool(379): error loading /system/media/audio/ui/KeypressSpacebar.ogg
06-30 05:01:53.129: W/AudioService(379): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
06-30 05:01:53.129: E/SoundPool(379): error loading /system/media/audio/ui/KeypressDelete.ogg
06-30 05:01:53.189: W/AudioService(379): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
06-30 05:01:53.219: I/ActivityManager(379): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.trucktracker/.MainActivity} from pid 536
06-30 05:01:53.239: E/SoundPool(379): error loading /system/media/audio/ui/KeypressReturn.ogg
06-30 05:01:53.259: W/AudioService(379): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
06-30 05:01:53.299: E/SoundPool(379): error loading /system/media/audio/ui/KeypressInvalid.ogg
06-30 05:01:53.319: W/AudioService(379): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
06-30 05:01:53.319: W/AudioService(379): onLoadSoundEffects(), Error -1 while loading samples
06-30 05:01:53.599: D/gralloc(50): Registering a buffer in the process that created it. This may cause memory ordering problems.
06-30 05:01:53.599: E/libEGL(50): called unimplemented OpenGL ES API
06-30 05:01:53.609: E/libEGL(50): called unimplemented OpenGL ES API
06-30 05:01:53.609: E/libEGL(50): called unimplemented OpenGL ES API
06-30 05:01:53.609: E/libEGL(50): called unimplemented OpenGL ES API
06-30 05:01:53.609: E/SurfaceFlinger(50): glCheckFramebufferStatusOES error 186238714
06-30 05:01:53.609: E/SurfaceFlinger(50): got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot
06-30 05:01:53.609: E/libEGL(50): called unimplemented OpenGL ES API
06-30 05:01:53.609: E/libEGL(50): called unimplemented OpenGL ES API
06-30 05:01:53.669: W/WindowManager(379): Screenshot failure taking screenshot for (246x410) to layer 21005
06-30 05:01:53.839: I/Choreographer(536): Skipped 178 frames!  The application may be doing too much work on its main thread.
06-30 05:01:53.999: D/dalvikvm(536): GC_FOR_ALLOC freed 193K, 15% free 8518K/9992K, paused 63ms, total 69ms
06-30 05:01:54.109: D/dalvikvm(379): JIT unchain all for threadid=41
06-30 05:01:54.549: D/dalvikvm(379): GC_FOR_ALLOC freed 714K, 20% free 5737K/7140K, paused 684ms, total 685ms
06-30 05:01:54.619: W/ActivityManager(379): Activity pause timeout for ActivityRecord{b3f886b8 u0 com.android.launcher/com.android.launcher2.Launcher t1}
06-30 05:01:54.959: I/Choreographer(379): Skipped 60 frames!  The application may be doing too much work on its main thread.
06-30 05:01:55.019: I/ActivityManager(379): Start proc com.example.trucktracker for activity com.example.trucktracker/.MainActivity: pid=1120 uid=10052 gids={50052}
06-30 05:01:55.159: D/dalvikvm(1120): Not late-enabling CheckJNI (already on)
06-30 05:01:56.559: I/ARMAssembler(50): generated scanline__00000077:03010104_00008001_00000000 [ 89 ipp] (110 ins) at [0xb5e0d680:0xb5e0d838] in 10000000 ns
06-30 05:01:56.679: I/Choreographer(379): Skipped 45 frames!  The application may be doing too much work on its main thread.
06-30 05:01:56.899: I/Choreographer(379): Skipped 53 frames!  The application may be doing too much work on its main thread.
06-30 05:01:57.829: D/dalvikvm(1120): GC_FOR_ALLOC freed 64K, 5% free 3135K/3280K, paused 61ms, total 63ms
06-30 05:01:57.829: I/dalvikvm-heap(1120): Grow heap (frag case) to 3.766MB for 659360-byte allocation
06-30 05:01:57.919: D/dalvikvm(1120): GC_FOR_ALLOC freed 6K, 4% free 3772K/3924K, paused 77ms, total 77ms
06-30 05:01:58.069: D/com.example.trucktracker.MainActivity(1120): Settings Text.
06-30 05:01:58.479: D/com.example.trucktracker.MainActivity(1120): Showing menu.
06-30 05:01:58.619: D/gralloc_goldfish(1120): Emulator without GPU emulation detected.
06-30 05:01:58.779: I/ActivityManager(379): Displayed com.example.trucktracker/.MainActivity: +3s944ms
06-30 05:01:59.079: I/Choreographer(379): Skipped 39 frames!  The application may be doing too much work on its main thread.
06-30 05:01:59.619: D/dalvikvm(930): GC_FOR_ALLOC freed 324K, 13% free 2959K/3364K, paused 67ms, total 330ms
06-30 05:01:59.699: E/StrictMode(930): A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
06-30 05:01:59.699: E/StrictMode(930): java.lang.Throwable: Explicit termination method 'release' not called
06-30 05:01:59.699: E/StrictMode(930):  at dalvik.system.CloseGuard.open(CloseGuard.java:184)
06-30 05:01:59.699: E/StrictMode(930):  at android.drm.DrmManagerClient.<init>(DrmManagerClient.java:258)
06-30 05:01:59.699: E/StrictMode(930):  at com.google.android.mms.pdu.PduPersister.<init>(PduPersister.java:288)
06-30 05:01:59.699: E/StrictMode(930):  at com.google.android.mms.pdu.PduPersister.getPduPersister(PduPersister.java:296)
06-30 05:01:59.699: E/StrictMode(930):  at com.android.mms.transaction.TransactionService.onNewIntent(TransactionService.java:224)
06-30 05:01:59.699: E/StrictMode(930):  at com.android.mms.transaction.TransactionService$ServiceHandler.handleMessage(TransactionService.java:621)
06-30 05:01:59.699: E/StrictMode(930):  at android.os.Handler.dispatchMessage(Handler.java:102)
06-30 05:01:59.699: E/StrictMode(930):  at android.os.Looper.loop(Looper.java:136)
06-30 05:01:59.699: E/StrictMode(930):  at android.os.HandlerThread.run(HandlerThread.java:61)
06-30 05:01:59.859: I/ActivityManager(379): Resuming delayed broadcast
06-30 05:01:59.959: E/BluetoothAdapter(509): Bluetooth binder is null
06-30 05:02:00.279: D/com.example.trucktracker.MainActivity(1120): ClickedContinueButton
06-30 05:02:00.299: I/ActivityManager(379): START u0 {cmp=com.example.trucktracker/.Locator} from pid 1120
06-30 05:02:00.329: E/SoundPool(379): error loading /system/media/audio/ui/Effect_Tick.ogg
06-30 05:02:00.329: W/AudioService(379): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
06-30 05:02:00.339: E/SoundPool(379): error loading /system/media/audio/ui/Effect_Tick.ogg
06-30 05:02:00.339: W/AudioService(379): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
06-30 05:02:00.339: E/SoundPool(379): error loading /system/media/audio/ui/Effect_Tick.ogg
06-30 05:02:00.339: W/AudioService(379): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
06-30 05:02:00.339: E/SoundPool(379): error loading /system/media/audio/ui/Effect_Tick.ogg
06-30 05:02:00.339: W/AudioService(379): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
06-30 05:02:00.339: E/SoundPool(379): error loading /system/media/audio/ui/Effect_Tick.ogg
06-30 05:02:00.359: W/AudioService(379): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
06-30 05:02:00.359: E/SoundPool(379): error loading /system/media/audio/ui/KeypressStandard.ogg
06-30 05:02:00.359: W/AudioService(379): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
06-30 05:02:00.399: E/SoundPool(379): error loading /system/media/audio/ui/KeypressSpacebar.ogg
06-30 05:02:00.399: W/AudioService(379): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
06-30 05:02:00.399: E/SoundPool(379): error loading /system/media/audio/ui/KeypressDelete.ogg
06-30 05:02:00.409: W/AudioService(379): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
06-30 05:02:00.409: E/SoundPool(379): error loading /system/media/audio/ui/KeypressReturn.ogg
06-30 05:02:00.409: W/AudioService(379): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
06-30 05:02:00.409: E/SoundPool(379): error loading /system/media/audio/ui/KeypressInvalid.ogg
06-30 05:02:00.409: W/AudioService(379): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
06-30 05:02:00.409: W/AudioService(379): onLoadSoundEffects(), Error -1 while loading samples
06-30 05:02:00.599: I/Choreographer(1120): Skipped 56 frames!  The application may be doing too much work on its main thread.
06-30 05:02:01.909: I/Choreographer(1120): Skipped 33 frames!  The application may be doing too much work on its main thread.
06-30 05:02:02.519: D/AlertReceiver(957): onReceive: a=android.intent.action.PROVIDER_CHANGED Intent { act=android.intent.action.PROVIDER_CHANGED dat=content://com.android.calendar flg=0x10 cmp=com.android.calendar/.alerts.AlertReceiver }
06-30 05:02:02.649: I/ActivityManager(379): Delay finish: com.android.calendar/.alerts.AlertReceiver
06-30 05:02:02.769: D/AlertService(957): 0 Action = android.intent.action.PROVIDER_CHANGED
06-30 05:02:03.309: I/Choreographer(379): Skipped 31 frames!  The application may be doing too much work on its main thread.
06-30 05:02:03.349: I/ActivityManager(379): Displayed com.example.trucktracker/.Locator: +2s964ms
06-30 05:02:03.729: I/Choreographer(379): Skipped 68 frames!  The application may be doing too much work on its main thread.
06-30 05:02:04.129: I/Choreographer(536): Skipped 107 frames!  The application may be doing too much work on its main thread.
06-30 05:02:04.149: W/RecognitionManagerService(379): no available voice recognition services found for user 0
06-30 05:02:07.779: I/GlobalDismissManager(957): no sender configured
06-30 05:02:07.779: D/AlertService(957): Beginning updateAlertNotification
06-30 05:02:07.889: D/AlertService(957): No fired or scheduled alerts
06-30 05:02:08.019: D/AlertService(957): Scheduling next alarm with AlarmScheduler. sEventReminderReceived: null
06-30 05:02:08.099: D/AlarmScheduler(957): No events found starting within 1 week.
06-30 05:02:08.119: I/ActivityManager(379): Resuming delayed broadcast
06-30 05:02:08.239: I/ActivityManager(379): Delay finish: com.android.mms/.transaction.SmsReceiver
06-30 05:02:08.259: V/SmsReceiverService(930): onStart: #1 mResultCode: -1 = Activity.RESULT_OK
06-30 05:02:08.479: I/ActivityManager(379): Resuming delayed broadcast
06-30 05:02:08.569: I/ActivityManager(379): Delay finish: com.android.providers.calendar/.CalendarProviderBroadcastReceiver
06-30 05:02:08.699: I/ActivityManager(379): Resuming delayed broadcast
06-30 05:02:08.919: V/AlarmClock(983): AlarmInitReceiver android.intent.action.TIME_SET
06-30 05:02:09.209: I/AlarmClock(983): Displaying next alarm time: ''
06-30 05:02:09.279: V/AlarmClock(983): AlarmInitReceiver finished
06-30 05:02:09.409: I/ActivityManager(379): Delay finish: com.android.providers.calendar/.CalendarProviderBroadcastReceiver
06-30 05:02:09.529: I/ActivityManager(379): Resuming delayed broadcast
06-30 05:02:09.629: W/ContextImpl(1058): Calling a method in the system process without a qualified user: android.app.ContextImpl.startService:1479 android.content.ContextWrapper.startService:494 android.content.ContextWrapper.startService:494 com.android.keychain.KeyChainBroadcastReceiver.onReceive:12 android.app.ActivityThread.handleReceiver:2419 
06-30 05:02:09.669: I/ActivityManager(379): Delay finish: com.android.keychain/.KeyChainBroadcastReceiver
06-30 05:02:09.789: I/ActivityManager(379): Resuming delayed broadcast
06-30 05:02:55.079: D/LightsService(379): Excessive delay setting light: 55ms

3 个答案:

答案 0 :(得分:2)

错误:

您在设置内容布局之前尝试查找视图。

textLat = (TextView)findViewById(R.id.textLat);     <<<<<<<
textLong = (TextView)findViewById(R.id.textLong);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new locatorlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
setContentView(R.layout.activity_locator);   <<<<<<

正确:

setContentView(R.layout.activity_locator);
textLat = (TextView)findViewById(R.id.textLat);
textLong = (TextView)findViewById(R.id.textLong);
....
....

答案 1 :(得分:1)

改变这个:

super.onCreate(savedInstanceState);
textLat = (TextView)findViewById(R.id.textLat);
textLong = (TextView)findViewById(R.id.textLong);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new locatorlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
setContentView(R.layout.activity_locator);

为:

 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_locator);
        textLat = (TextView)findViewById(R.id.textLat);
        textLong = (TextView)findViewById(R.id.textLong);
        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new locatorlistener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
        setContentView(R.layout.activity_locator);

您正在调用textView的findViewById()而不初始化您的java文件的布局。这将给NPE。

答案 2 :(得分:1)

因此,要查看问题,我们需要查看堆栈跟踪。但我看到的是你在setContentView之前添加了你的textViews。这不起作用,将其更改为:

      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_locator);
      textLat = (TextView)findViewById(R.id.textLat);
      textLong = (TextView)findViewById(R.id.textLong);
      LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
      LocationListener ll = new locatorlistener();
      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);