سؤال

When i run app it give message unfortunately loca has stopped and close app

Java coding

package com.example.loca;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements LocationListener {

    protected LocationManager locationManager;
    protected LocationListener locationListener;
    protected Context context;

    TextView txtLat;
    String lat, place1, currentlocation,  btn_text;
    String provider;
    protected String latitude,longitude;
    protected boolean gps_enabled,network_enabled;



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


        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

        Toast.makeText(getApplicationContext(), currentlocation  , Toast.LENGTH_LONG).show();




    }



    @Override
    public void onLocationChanged(Location location) {

        Toast.makeText(getApplicationContext(), currentlocation  , Toast.LENGTH_LONG).show();
        // TODO Auto-generated method stub

    }



    @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 provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }



}

AND LOGCAT is

    05-14 01:20:39.210: D/AndroidRuntime(1177): Shutting down VM
05-14 01:20:39.210: W/dalvikvm(1177): threadid=1: thread exiting with uncaught exception (group=0xb3aebb90)
05-14 01:20:39.230: E/AndroidRuntime(1177): FATAL EXCEPTION: main
05-14 01:20:39.230: E/AndroidRuntime(1177): Process: com.example.loca, PID: 1177
05-14 01:20:39.230: E/AndroidRuntime(1177): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.loca/com.example.loca.MainActivity}: java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.
05-14 01:20:39.230: E/AndroidRuntime(1177):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
05-14 01:20:39.230: E/AndroidRuntime(1177):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
05-14 01:20:39.230: E/AndroidRuntime(1177):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
05-14 01:20:39.230: E/AndroidRuntime(1177):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
05-14 01:20:39.230: E/AndroidRuntime(1177):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-14 01:20:39.230: E/AndroidRuntime(1177):     at android.os.Looper.loop(Looper.java:137)
05-14 01:20:39.230: E/AndroidRuntime(1177):     at android.app.ActivityThread.main(ActivityThread.java:4998)
05-14 01:20:39.230: E/AndroidRuntime(1177):     at java.lang.reflect.Method.invokeNative(Native Method)
05-14 01:20:39.230: E/AndroidRuntime(1177):     at java.lang.reflect.Method.invoke(Method.java:515)
05-14 01:20:39.230: E/AndroidRuntime(1177):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
05-14 01:20:39.230: E/AndroidRuntime(1177):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
05-14 01:20:39.230: E/AndroidRuntime(1177):     at dalvik.system.NativeStart.main(Native Method)
05-14 01:20:39.230: E/AndroidRuntime(1177): Caused by: java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.
05-14 01:20:39.230: E/AndroidRuntime(1177):     at android.os.Parcel.readException(Parcel.java:1461)
05-14 01:20:39.230: E/AndroidRuntime(1177):     at android.os.Parcel.readException(Parcel.java:1415)
05-14 01:20:39.230: E/AndroidRuntime(1177):     at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:540)
05-14 01:20:39.230: E/AndroidRuntime(1177):     at android.location.LocationManager.requestLocationUpdates(LocationManager.java:860)
05-14 01:20:39.230: E/AndroidRuntime(1177):     at android.location.LocationManager.requestLocationUpdates(LocationManager.java:454)
05-14 01:20:39.230: E/AndroidRuntime(1177):     at com.example.loca.MainActivity.onCreate(MainActivity.java:34)
05-14 01:20:39.230: E/AndroidRuntime(1177):     at android.app.Activity.performCreate(Activity.java:5243)
05-14 01:20:39.230: E/AndroidRuntime(1177):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-14 01:20:39.230: E/AndroidRuntime(1177):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
05-14 01:20:39.230: E/AndroidRuntime(1177):     ... 11 more
05-14 01:20:47.040: I/Process(1177): Sending signal. PID: 1177 SIG: 9
هل كانت مفيدة؟

المحلول

You need to request the FINE_LOCATION permission, just like the exception says.

نصائح أخرى

Your logcat clearly said

Caused by: java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission. 05-14 01:20:39.230: E/AndroidRuntime(1177):

You need to add below permission in your manifest.xml file

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

The log says it all :

 location provider requires ACCESS_FINE_LOCATION permission.

Add this permission in AndroidManifest.xml and your app will work fine.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top