Gps位置为空

时间:2017-04-18 19:45:51

标签: android gps osmdroid android-gps

我正在尝试创建一个应用程序,在TextView中给我一个位置但结果如果我启用精确位置是null

public class Gpslocation extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gpslocation);

        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Location position = locationManager.getLastKnownLocation( LocationManager.GPS_PROVIDER);

       //LocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,this);

        String message_gps = "gps position : "+position;
        TextView textView = (TextView) findViewById(R.id.txtgpscoord);
        textView.setText(message_gps);

    }
}

如果我在手机上使用应用程序“假位置”设置假坐标,它会给我一个结果,但不是没有它。但是我的Gps适用于其他应用程序,所以问题显然来自我的代码。

如果我将此行取消注释://LocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,this);应用程序崩溃...

我在清单中添加了以下权限:

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  <uses-feature android:name="android.hardware.location.network" />
    <uses-feature android:name="android.hardware.location.gps" />
    <uses-feature android:name="android.hardware.wifi" />

非常感谢您的帮助。

编辑:logcat是:

04-19 15:25:47.388 19663-19663/? I/art: Late-enabling -Xcheck:jni
04-19 15:25:47.414 19663-19663/? E/art: Failed to send JDWP packet APNM to debugger (-1 of 57): Broken pipe
04-19 15:25:47.414 19663-19670/? E/art: Failed sending reply to debugger: Broken pipe
04-19 15:25:47.414 19663-19670/? I/art: Debugger is no longer active
04-19 15:25:47.515 19663-19663/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a53 --instruction-set-features=default --dex-file=/data/app/test.testgpspos-2/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@test.testgpspos-2@split_lib_dependencies_apk.apk@classes.dex) because non-0 exit status
04-19 15:25:47.770 19663-19663/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a53 --instruction-set-features=default --dex-file=/data/app/test.testgpspos-2/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@test.testgpspos-2@split_lib_slice_0_apk.apk@classes.dex) because non-0 exit status
04-19 15:25:47.841 19663-19663/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a53 --instruction-set-features=default --dex-file=/data/app/test.testgpspos-2/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@test.testgpspos-2@split_lib_slice_1_apk.apk@classes.dex) because non-0 exit status
04-19 15:25:47.902 19663-19663/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a53 --instruction-set-features=default --dex-file=/data/app/test.testgpspos-2/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@test.testgpspos-2@split_lib_slice_2_apk.apk@classes.dex) because non-0 exit status
04-19 15:25:47.964 19663-19663/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a53 --instruction-set-features=default --dex-file=/data/app/test.testgpspos-2/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@test.testgpspos-2@split_lib_slice_3_apk.apk@classes.dex) because non-0 exit status
04-19 15:25:48.029 19663-19663/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a53 --instruction-set-features=default --dex-file=/data/app/test.testgpspos-2/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@test.testgpspos-2@split_lib_slice_4_apk.apk@classes.dex) because non-0 exit status
04-19 15:25:48.099 19663-19663/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a53 --instruction-set-features=default --dex-file=/data/app/test.testgpspos-2/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@test.testgpspos-2@split_lib_slice_5_apk.apk@classes.dex) because non-0 exit status
04-19 15:25:48.161 19663-19663/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a53 --instruction-set-features=default --dex-file=/data/app/test.testgpspos-2/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@test.testgpspos-2@split_lib_slice_6_apk.apk@classes.dex) because non-0 exit status
04-19 15:25:48.225 19663-19663/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a53 --instruction-set-features=default --dex-file=/data/app/test.testgpspos-2/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@test.testgpspos-2@split_lib_slice_7_apk.apk@classes.dex) because non-0 exit status
04-19 15:25:48.288 19663-19663/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a53 --instruction-set-features=default --dex-file=/data/app/test.testgpspos-2/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@test.testgpspos-2@split_lib_slice_8_apk.apk@classes.dex) because non-0 exit status
04-19 15:25:48.350 19663-19663/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a53 --instruction-set-features=default --dex-file=/data/app/test.testgpspos-2/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@test.testgpspos-2@split_lib_slice_9_apk.apk@classes.dex) because non-0 exit status
04-19 15:25:48.352 19663-19663/? W/System: ClassLoader referenced unknown path: /data/app/test.testgpspos-2/lib/arm
04-19 15:25:48.353 19663-19663/? I/srstest: makeApplication sAppContextandroid.app.Application@f0af218
04-19 15:25:48.353 19663-19663/? D/ActivityThread: installProvider: context.getPackageName()=test.testgpspos
04-19 15:25:48.355 19663-19663/? I/InstantRun: starting instant run server: is main process
04-19 15:25:48.359 19663-19663/? D/ActivityThread: BIND_APPLICATION handled : 0 / AppBindData{appInfo=ApplicationInfo{82bd256 test.testgpspos}}
04-19 15:25:48.359 19663-19663/? V/ActivityThread: Handling launch of ActivityRecord{942f2d7 token=android.os.BinderProxy@7506ad {test.testgpspos/test.testgpspos.MainActivity}} startsNotResumed=false
04-19 15:25:48.423 19663-19663/test.testgpspos V/ActivityThread: ActivityRecord{942f2d7 token=android.os.BinderProxy@7506ad {test.testgpspos/test.testgpspos.MainActivity}}: app=android.app.Application@f0af218, appName=test.testgpspos, pkg=test.testgpspos, comp={test.testgpspos/test.testgpspos.MainActivity}, dir=/data/app/test.testgpspos-2/base.apk
04-19 15:25:48.424 19663-19663/test.testgpspos D/PackageManager: Query text for package: test.testgpsposresid: 2131099681
04-19 15:25:48.425 19663-19663/test.testgpspos D/fiona: ApplicationPackageManager line1356 text=testgpspos
04-19 15:25:48.425 19663-19663/test.testgpspos D/fiona: ApplicationPackageManager line1358=test.testgpspos
04-19 15:25:48.425 19663-19663/test.testgpspos D/fiona: ApplicationPackageManager line1359=READY,ABSENT
04-19 15:25:48.461 19663-19663/test.testgpspos W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
04-19 15:25:48.504 19663-19663/test.testgpspos E/MultiWindowProxy: getServiceInstance failed!
04-19 15:25:48.535 19663-19672/test.testgpspos I/System: FinalizerDaemon: finalize objects = 1
04-19 15:25:48.661 19663-19663/test.testgpspos V/ActivityThread: Performing resume of ActivityRecord{942f2d7 token=android.os.BinderProxy@7506ad {test.testgpspos/test.testgpspos.MainActivity}}
04-19 15:25:48.673 19663-19663/test.testgpspos D/ActivityThread: ACT-AM_ON_RESUME_CALLED ActivityRecord{942f2d7 token=android.os.BinderProxy@7506ad {test.testgpspos/test.testgpspos.MainActivity}}
04-19 15:25:48.673 19663-19663/test.testgpspos V/ActivityThread: Resume ActivityRecord{942f2d7 token=android.os.BinderProxy@7506ad {test.testgpspos/test.testgpspos.MainActivity}} started activity: false, hideForNow: false, finished: false
04-19 15:25:48.673 19663-19663/test.testgpspos V/PhoneWindow: DecorView setVisiblity: visibility = 4 ,Parent =null, this =com.android.internal.policy.PhoneWindow$DecorView{a8860a9 I.E...... R.....ID 0,0-0,0}
04-19 15:25:48.676 19663-19663/test.testgpspos D/WindowClient: Add to mViews: com.android.internal.policy.PhoneWindow$DecorView{a8860a9 I.E...... R.....ID 0,0-0,0}, this = android.view.WindowManagerGlobal@ca312f2
04-19 15:25:48.677 19663-19663/test.testgpspos D/OpenGLRenderer: Dumper init 4 threads <0xabf70280>
04-19 15:25:48.678 19663-19663/test.testgpspos D/OpenGLRenderer: <test.testgpspos> is running.
04-19 15:25:48.679 19663-19721/test.testgpspos D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: false
04-19 15:25:48.680 19663-19721/test.testgpspos D/OpenGLRenderer: CanvasContext() 0xb3145800
04-19 15:25:48.683 19663-19663/test.testgpspos D/ViewRootImpl: hardware acceleration is enabled, this = ViewRoot{a60fd43 test.testgpspos/test.testgpspos.MainActivity,ident = 0}
04-19 15:25:48.687 19663-19663/test.testgpspos V/ActivityThread: Resuming ActivityRecord{942f2d7 token=android.os.BinderProxy@7506ad {test.testgpspos/test.testgpspos.MainActivity}} with isForward=true
04-19 15:25:48.687 19663-19663/test.testgpspos V/PhoneWindow: DecorView setVisiblity: visibility = 0 ,Parent =ViewRoot{a60fd43 test.testgpspos/test.testgpspos.MainActivity,ident = 0}, this =com.android.internal.policy.PhoneWindow$DecorView{a8860a9 V.E...... R.....ID 0,0-0,0}
04-19 15:25:48.687 19663-19663/test.testgpspos V/ActivityThread: Scheduling idle handler for ActivityRecord{942f2d7 token=android.os.BinderProxy@7506ad {test.testgpspos/test.testgpspos.MainActivity}}
04-19 15:25:48.687 19663-19663/test.testgpspos D/ActivityThread: ACT-LAUNCH_ACTIVITY handled : 0 / ActivityRecord{942f2d7 token=android.os.BinderProxy@7506ad {test.testgpspos/test.testgpspos.MainActivity}}
04-19 15:25:48.731 19663-19721/test.testgpspos D/OpenGLRenderer: CanvasContext() 0xb3145800 initialize window=0xae406608, title=test.testgpspos/test.testgpspos.MainActivity
04-19 15:25:48.731 19663-19663/test.testgpspos D/Surface: Surface::allocateBuffers(this=0xae406600)
04-19 15:25:48.737 19663-19721/test.testgpspos I/OpenGLRenderer: Initialized EGL, version 1.4
04-19 15:25:48.739 19663-19721/test.testgpspos D/OpenGLRenderer: Created EGL context (0xae411480)
04-19 15:25:48.741 19663-19721/test.testgpspos I/OpenGLRenderer: Get enable program binary service property (1)
04-19 15:25:48.741 19663-19721/test.testgpspos I/OpenGLRenderer: Initializing program atlas...
04-19 15:25:48.742 19663-19721/test.testgpspos D/ProgramBinary/Service: BpProgramBinaryService.getFileDescriptor
04-19 15:25:48.742 19663-19721/test.testgpspos D/ProgramBinary/Service: BpProgramBinaryService.getProgramMapLen
04-19 15:25:48.743 19663-19721/test.testgpspos D/ProgramBinary/Service: BpProgramBinaryService.getProgramMapArray
04-19 15:25:48.743 19663-19721/test.testgpspos D/ProgramBinary/Service: BpProgramBinaryService.getProgramBinaryLen
04-19 15:25:48.743 19663-19721/test.testgpspos I/OpenGLRenderer: Program binary detail: Binary length is 171416, program map length is 152.
04-19 15:25:48.744 19663-19721/test.testgpspos I/OpenGLRenderer: Succeeded to mmap program binaries. File descriptor is 46, and path is /dev/ashmem.
04-19 15:25:48.744 19663-19721/test.testgpspos I/OpenGLRenderer: No need to use file discriptor anymore, close fd(46).
04-19 15:25:48.744 19663-19721/test.testgpspos D/OpenGLRenderer: Initializing program cache from 0xb6d27eac, size = 4
04-19 15:25:48.747 19663-19721/test.testgpspos D/OpenGLRenderer: -- init (key = 0x0000000000000000)
04-19 15:25:48.748 19663-19721/test.testgpspos D/OpenGLRenderer: -- init (key = 0x0000000800000003)
04-19 15:25:48.750 19663-19721/test.testgpspos D/OpenGLRenderer: -- init (key = 0x0000001000500040)
04-19 15:25:48.751 19663-19721/test.testgpspos D/OpenGLRenderer: -- init (key = 0x0000003800000000)
04-19 15:25:48.751 19663-19721/test.testgpspos D/Surface: Surface::connect(this=0xae406600,api=1)
04-19 15:25:48.752 19663-19721/test.testgpspos W/libEGL: [ANDROID_RECORDABLE] format: 1
04-19 15:25:48.753 19663-19721/test.testgpspos D/mali_winsys: new_window_surface returns 0x3000
04-19 15:25:48.777 19663-19663/test.testgpspos W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
04-19 15:25:48.790 19663-19721/test.testgpspos D/OpenGLRenderer: CacheTexture 1 upload: x, y, width height = 0, 0, 111, 166
04-19 15:25:48.798 19663-19721/test.testgpspos I/[MALI][Gralloc]: [+]r_hnd(0x9fa78500), client(48), share_fd(47)
04-19 15:25:48.798 19663-19721/test.testgpspos D/GraphicBuffer: register, handle(0x9fa78500) (w:720 h:1280 s:720 f:0x1 u:0x000b00)
04-19 15:25:48.800 19663-19721/test.testgpspos D/OpenGLRenderer: ProgramCache save to disk, size = 4
04-19 15:25:48.804 19663-19663/test.testgpspos V/InputMethodManager: onWindowFocus: null softInputMode=288 first=true flags=#81810100
04-19 15:25:48.804 19663-19663/test.testgpspos V/InputMethodManager: START INPUT: com.android.internal.policy.PhoneWindow$DecorView{a8860a9 V.E...... R.....ID 0,0-720,1280} ic=null tba=android.view.inputmethod.EditorInfo@212ce97 controlFlags=#104
04-19 15:25:48.817 19663-19721/test.testgpspos I/[MALI][Gralloc]: [+]r_hnd(0x9fa78640), client(48), share_fd(50)
04-19 15:25:48.817 19663-19721/test.testgpspos D/GraphicBuffer: register, handle(0x9fa78640) (w:720 h:1280 s:720 f:0x1 u:0x000b00)
04-19 15:25:56.667 19663-19663/test.testgpspos V/SettingsInterface: invalidate [system]: current 4 != cached 0
04-19 15:25:56.668 19663-19663/test.testgpspos D/ActivityThread: holder:android.app.IActivityManager$ContentProviderHolder@9635e69, holder.provider:android.content.ContentProviderProxy@44960ee
04-19 15:25:56.670 19663-19721/test.testgpspos I/[MALI][Gralloc]: [+]r_hnd(0x9fa78780), client(48), share_fd(51)
04-19 15:25:56.670 19663-19721/test.testgpspos D/GraphicBuffer: register, handle(0x9fa78780) (w:720 h:1280 s:720 f:0x1 u:0x000b00)
04-19 15:25:56.682 19663-19663/test.testgpspos D/ActivityThread: ACT-AM_ON_PAUSE_CALLED ActivityRecord{942f2d7 token=android.os.BinderProxy@7506ad {test.testgpspos/test.testgpspos.MainActivity}}
04-19 15:25:56.688 19663-19663/test.testgpspos D/ActivityThread: ACT-PAUSE_ACTIVITY handled : 1 / android.os.BinderProxy@7506ad
04-19 15:25:56.690 19663-19663/test.testgpspos V/ActivityThread: Handling launch of ActivityRecord{ce0041c token=android.os.BinderProxy@64a7825 {test.testgpspos/test.testgpspos.Gpslocation}} startsNotResumed=false
04-19 15:25:56.705 19663-19663/test.testgpspos V/ActivityThread: ActivityRecord{ce0041c token=android.os.BinderProxy@64a7825 {test.testgpspos/test.testgpspos.Gpslocation}}: app=android.app.Application@f0af218, appName=test.testgpspos, pkg=test.testgpspos, comp={test.testgpspos/test.testgpspos.Gpslocation}, dir=/data/app/test.testgpspos-2/base.apk
04-19 15:25:56.706 19663-19663/test.testgpspos D/PackageManager: Query text for package: test.testgpsposresid: 2131099681
04-19 15:25:56.706 19663-19663/test.testgpspos D/fiona: ApplicationPackageManager line1288 text=testgpspos
04-19 15:25:56.710 19663-19663/test.testgpspos E/MultiWindowProxy: getServiceInstance failed!
04-19 15:25:56.726 19663-19663/test.testgpspos V/ActivityThread: Performing resume of ActivityRecord{ce0041c token=android.os.BinderProxy@64a7825 {test.testgpspos/test.testgpspos.Gpslocation}}
04-19 15:25:56.727 19663-19663/test.testgpspos D/ActivityThread: ACT-AM_ON_RESUME_CALLED ActivityRecord{ce0041c token=android.os.BinderProxy@64a7825 {test.testgpspos/test.testgpspos.Gpslocation}}
04-19 15:25:56.727 19663-19663/test.testgpspos V/ActivityThread: Resume ActivityRecord{ce0041c token=android.os.BinderProxy@64a7825 {test.testgpspos/test.testgpspos.Gpslocation}} started activity: false, hideForNow: false, finished: false
04-19 15:25:56.727 19663-19663/test.testgpspos V/PhoneWindow: DecorView setVisiblity: visibility = 4 ,Parent =null, this =com.android.internal.policy.PhoneWindow$DecorView{3f74608 I.E...... R.....ID 0,0-0,0}
04-19 15:25:56.727 19663-19663/test.testgpspos D/WindowClient: Add to mViews: com.android.internal.policy.PhoneWindow$DecorView{3f74608 I.E...... R.....ID 0,0-0,0}, this = android.view.WindowManagerGlobal@ca312f2
04-19 15:25:56.728 19663-19721/test.testgpspos D/OpenGLRenderer: CanvasContext() 0xae85b000
04-19 15:25:56.729 19663-19663/test.testgpspos D/ViewRootImpl: hardware acceleration is enabled, this = ViewRoot{cd9ff50 test.testgpspos/test.testgpspos.Gpslocation,ident = 1}
04-19 15:25:56.732 19663-19663/test.testgpspos V/ActivityThread: Resuming ActivityRecord{ce0041c token=android.os.BinderProxy@64a7825 {test.testgpspos/test.testgpspos.Gpslocation}} with isForward=true
04-19 15:25:56.732 19663-19663/test.testgpspos V/PhoneWindow: DecorView setVisiblity: visibility = 0 ,Parent =ViewRoot{cd9ff50 test.testgpspos/test.testgpspos.Gpslocation,ident = 1}, this =com.android.internal.policy.PhoneWindow$DecorView{3f74608 V.E...... R.....ID 0,0-0,0}
04-19 15:25:56.732 19663-19663/test.testgpspos V/ActivityThread: Scheduling idle handler for ActivityRecord{ce0041c token=android.os.BinderProxy@64a7825 {test.testgpspos/test.testgpspos.Gpslocation}}
04-19 15:25:56.732 19663-19663/test.testgpspos D/ActivityThread: ACT-LAUNCH_ACTIVITY handled : 0 / ActivityRecord{ce0041c token=android.os.BinderProxy@64a7825 {test.testgpspos/test.testgpspos.Gpslocation}}
04-19 15:25:56.756 19663-19663/test.testgpspos D/Surface: Surface::allocateBuffers(this=0xa81b6e00)
04-19 15:25:56.758 19663-19721/test.testgpspos D/OpenGLRenderer: CanvasContext() 0xae85b000 initialize window=0xa81b6e08, title=test.testgpspos/test.testgpspos.Gpslocation
04-19 15:25:56.758 19663-19721/test.testgpspos D/Surface: Surface::connect(this=0xa81b6e00,api=1)
04-19 15:25:56.759 19663-19721/test.testgpspos W/libEGL: [ANDROID_RECORDABLE] format: 1
04-19 15:25:56.759 19663-19721/test.testgpspos D/mali_winsys: new_window_surface returns 0x3000
04-19 15:25:56.769 19663-19721/test.testgpspos D/OpenGLRenderer: CacheTexture 1 upload: x, y, width height = 0, 43, 113, 174
04-19 15:25:56.773 19663-19721/test.testgpspos I/[MALI][Gralloc]: [+]r_hnd(0x9fa78c80), client(48), share_fd(54)
04-19 15:25:56.773 19663-19721/test.testgpspos D/GraphicBuffer: register, handle(0x9fa78c80) (w:720 h:1280 s:720 f:0x1 u:0x000b00)
04-19 15:25:56.783 19663-19663/test.testgpspos V/InputMethodManager: onWindowFocus: null softInputMode=288 first=true flags=#81810100
04-19 15:25:56.783 19663-19663/test.testgpspos V/InputMethodManager: START INPUT: com.android.internal.policy.PhoneWindow$DecorView{3f74608 V.E...... R.....ID 0,0-720,1280} ic=null tba=android.view.inputmethod.EditorInfo@825481 controlFlags=#104
04-19 15:25:56.797 19663-19721/test.testgpspos I/[MALI][Gralloc]: [+]r_hnd(0x9fa78dc0), client(48), share_fd(49)
04-19 15:25:56.797 19663-19721/test.testgpspos D/GraphicBuffer: register, handle(0x9fa78dc0) (w:720 h:1280 s:720 f:0x1 u:0x000b00)
04-19 15:25:56.803 19663-19721/test.testgpspos D/Surface: Surface::disconnect(this=0xae406600,api=1)
04-19 15:25:56.804 19663-19721/test.testgpspos D/GraphicBuffer: unregister, handle(0x9fa78500) (w:720 h:1280 s:720 f:0x1 u:0x000b00)
04-19 15:25:56.805 19663-19721/test.testgpspos I/[MALI][Gralloc]: [-]r_hnd(0x9fa78500), client(48), share_fd(47)
04-19 15:25:56.805 19663-19721/test.testgpspos D/GraphicBuffer: unregister, handle(0x9fa78640) (w:720 h:1280 s:720 f:0x1 u:0x000b00)
04-19 15:25:56.806 19663-19721/test.testgpspos I/[MALI][Gralloc]: [-]r_hnd(0x9fa78640), client(48), share_fd(50)
04-19 15:25:56.806 19663-19721/test.testgpspos D/GraphicBuffer: unregister, handle(0x9fa78780) (w:720 h:1280 s:720 f:0x1 u:0x000b00)
04-19 15:25:56.807 19663-19721/test.testgpspos I/[MALI][Gralloc]: [-]r_hnd(0x9fa78780), client(48), share_fd(51)
04-19 15:25:56.807 19663-19721/test.testgpspos D/Surface: Surface::disconnect(this=0xae406600,api=1)
04-19 15:25:56.808 19663-19721/test.testgpspos D/OpenGLRenderer: endAllStagingAnimators on 0xaa66dc00 (RippleDrawable @12d1bb) with handle 0xb4675c20
04-19 15:25:56.974 19663-19663/test.testgpspos V/ActivityThread: Finishing stop of ActivityRecord{942f2d7 token=android.os.BinderProxy@7506ad {test.testgpspos/test.testgpspos.MainActivity}}: show=false win=com.android.internal.policy.PhoneWindow@a037926
04-19 15:25:56.975 19663-19663/test.testgpspos V/PhoneWindow: DecorView setVisiblity: visibility = 4 ,Parent =ViewRoot{a60fd43 test.testgpspos/test.testgpspos.MainActivity,ident = 0}, this =com.android.internal.policy.PhoneWindow$DecorView{a8860a9 I.E...... R......D 0,0-720,1280}
04-19 15:25:56.975 19663-19663/test.testgpspos D/ActivityThread: ACT-STOP_ACTIVITY_HIDE handled : 0 / android.os.BinderProxy@7506ad

编辑#2:如果我为requestLocationUpdates添加以下代码:

        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            return;
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) this);
        Location position = locationManager.getLastKnownLocation( LocationManager.GPS_PROVIDER);

我得到以下logcat:

4-19 23:04:35.971 16384-16384/test.testgpspos E/MultiWindowProxy: getServiceInstance failed!
04-19 23:04:35.986 16384-16384/test.testgpspos D/AndroidRuntime: Shutting down VM
04-19 23:04:35.987 16384-16384/test.testgpspos E/AndroidRuntime: FATAL EXCEPTION: main
                                                                 Process: test.testgpspos, PID: 16384
                                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{test.testgpspos/test.testgpspos.Gpslocation}: java.lang.ClassCastException: test.testgpspos.Gpslocation cannot be cast to android.location.LocationListener
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2572)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654)
                                                                     at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                     at android.os.Looper.loop(Looper.java:207)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5728)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
                                                                  Caused by: java.lang.ClassCastException: test.testgpspos.Gpslocation cannot be cast to android.location.LocationListener
                                                                     at test.testgpspos.Gpslocation.onCreate(Gpslocation.java:40)
                                                                     at android.app.Activity.performCreate(Activity.java:6309)
                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2519)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654) 
                                                                     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488) 
                                                                     at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                     at android.os.Looper.loop(Looper.java:207) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5728) 
                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 
04-19 23:04:36.005 16384-16393/test.testgpspos I/System: FinalizerDaemon: finalize objects = 1

可能我的语法错误了......

1 个答案:

答案 0 :(得分:0)

我认为问题出现在这条道路上:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) this);

正如您在日志中看到的那样,您无法将Gpslocation类强制转换为LocationListener。 Gpslocation类需要实现LocationListener,因此解决方案是将类声明更改为:

public class Gpslocation extends AppCompatActivity implements LocationListener {