SDK V23 無法運行

jike_5919684 Android基础知识-传感器... 最后由 qq_h4uoilns 于2016年11月25日回复

  • 3 回答
  • 4.4k 浏览

使用SDK V23開發撰寫時,

location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);

IDE 報錯訊息:

call requires permission which may be rejected by user : code should explicitly check to see if permission is available (with checkPermission) or explicitly handle a potential security exception


建議補上code

if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
 {

            // TODO: Consider calling

            //    public void requestPermissions(@NonNull String[] permissions, int requestCode)

            // here to request the missing permissions, and then overriding

            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,

            //                                          int[] grantResults)

            // to handle the case where the user grants the permission. See the documentation

            // for Activity#requestPermissions for more details.

            return;

        }

執行在手機,程序立刻掛了。


錯誤訊息

02-11 16:18:49.256 26870-26870/com.riitei.gps_03 E/AndroidRuntime: FATAL EXCEPTION: main

02-11 16:18:49.256 26870-26870/com.riitei.gps_03 E/AndroidRuntime: Process: com.riitei.gps_03, PID: 26870

02-11 16:18:49.256 26870-26870/com.riitei.gps_03 E/AndroidRuntime: java.lang.NoSuchMethodError: com.riitei.gps_03.MainActivity.checkSelfPermission

02-11 16:18:49.256 26870-26870/com.riitei.gps_03 E/AndroidRuntime:     at com.riitei.gps_03.MainActivity.onCreate(MainActivity.java:28)

02-11 16:18:49.256 26870-26870/com.riitei.gps_03 E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5248)

02-11 16:18:49.256 26870-26870/com.riitei.gps_03 E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)

02-11 16:18:49.256 26870-26870/com.riitei.gps_03 E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164)

02-11 16:18:49.256 26870-26870/com.riitei.gps_03 E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249)

02-11 16:18:49.256 26870-26870/com.riitei.gps_03 E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:141)

02-11 16:18:49.256 26870-26870/com.riitei.gps_03 E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)

02-11 16:18:49.256 26870-26870/com.riitei.gps_03 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)

02-11 16:18:49.256 26870-26870/com.riitei.gps_03 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136)

02-11 16:18:49.256 26870-26870/com.riitei.gps_03 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5113)

02-11 16:18:49.256 26870-26870/com.riitei.gps_03 E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)

02-11 16:18:49.256 26870-26870/com.riitei.gps_03 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)

02-11 16:18:49.256 26870-26870/com.riitei.gps_03 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

02-11 16:18:49.256 26870-26870/com.riitei.gps_03 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)

02-11 16:18:49.256 26870-26870/com.riitei.gps_03 E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

02-11 16:19:04.616 26870-26870/com.riitei.gps_03 I/Process: Sending signal. PID: 26870 SIG: 9


自己爬文章


http://my.oschina.net/u/990728/blog/549914

http://blog.csdn.net/u010313561/article/details/49902083

http://blog.csdn.net/ldhsecret/article/details/8063643

http://www.28im.com/android/a4736855.html

http://www.28im.com/android/a5148309.html

http://www.28im.com/android/a4948588.html

  • 极客学院_锋 2016年02月15日 回答 #1楼
  • 应该是在AndroidManifest中缺少相应的权限,你检查一下相应权限是否添加

  • 0 评论
  • jike_5919684 2016年02月17日 回答 #2楼
  • 請問你說只哪方面權限呢?
    個人天家了

    <!--1.允許使用網路權限-->

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

    <!--2.允許使用GPS權限-->

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

    <!--3.允許使用網路定位權限-->

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  • 0 评论