0

During development, there was a similar problem and also because of the fact that I am a novice, do not know how to handle it, thanks in advance to those who help! I tried to change something to rewrite over and all else fails, I can not understand at what point I did something wrong

Activity

public class FormActivity extends AppCompatActivity {
    private LocationManager locationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.form);
        ///=================================
        /////Автоматическийм расчет веремени
        EditText et1 = (EditText) findViewById(R.id.formtime);
        EditText et2 = (EditText) findViewById(R.id.formdate);
        SimpleDateFormat df1 = new SimpleDateFormat("HH:mm:ss");
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd ");
        Calendar c = Calendar.getInstance();
        String e1 = df1.format(c.getTime());
        String e2 = df2.format(c.getTime());
        et1.setText(e1);
        et2.setText(e2);
        /////Конец расчета времени
    }

    public void GeoClick(View v){
        String svcName = Context.LOCATION_SERVICE;
        locationManager = (LocationManager)getSystemService(svcName);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setSpeedRequired(false);
        criteria.setCostAllowed(true);

        String provider = locationManager.getBestProvider(criteria, true);
        Location l = locationManager.getLastKnownLocation(provider);
        updateWithNewLocation(l);
        locationManager.requestLocationUpdates(provider, 5000, 10, locationListener);
    }

    private void updateWithNewLocation(Location location) {
        TextView myLocationText;
        myLocationText = (TextView)findViewById(R.id.myLocationText);

        String latLongString = "No location found";
        String addressString = "No address found";

        if (location != null) {
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            latLongString = "Lat:" + lat + "\nLong:" + lng;

            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
            Geocoder gc = new Geocoder(this, Locale.getDefault());

            try {
                List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
                StringBuilder sb = new StringBuilder();
                if (addresses.size() > 0) {
                    Address address = addresses.get(0);

                    for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
                        sb.append(address.getAddressLine(i)).append("\n");

                    sb.append(address.getLocality()).append("\n");
                    sb.append(address.getPostalCode()).append("\n");
                    sb.append(address.getCountryName());
                }
                addressString = sb.toString();
            } catch (IOException e) {}
        }
        myLocationText.setText("Your Current Position is:\n" +
                latLongString + "\n\n" + addressString);
    }

    private final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {updateWithNewLocation(location);}
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status,
                                    Bundle extras) {}
    };
}

Error

 {FATAL EXCEPTION: main                                                                  java.lang.IllegalStateException: Could not execute method of the activity
 at android.view.View$1.onClick(View.java:3591)
 at android.view.View.performClick(View.java:4084)
 at android.view.View$PerformClick.run(View.java:16966)
 at android.os.Handler.handleCallback(Handler.java:615)
 at android.os.Handler.dispatchMessage(Handler.java:92)
 at android.os.Looper.loop(Looper.java:137)
 at android.app.ActivityThread.main(ActivityThread.java:4745)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:511)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
 at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:511)
 at android.view.View$1.onClick(View.java:3586)
 at android.view.View.performClick(View.java:4084) 
 at android.view.View$PerformClick.run(View.java:16966) 
 at android.os.Handler.handleCallback(Handler.java:615) 
 at android.os.Handler.dispatchMessage(Handler.java:92) 
 at android.os.Looper.loop(Looper.java:137) 
 at android.app.ActivityThread.main(ActivityThread.java:4745) 
 at java.lang.reflect.Method.invokeNative(Native Method) 
 at java.lang.reflect.Method.invoke(Method.java:511) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
 at dalvik.system.NativeStart.main(Native Method) 
 Caused by: java.lang.IllegalArgumentException: provider==null
 at android.location.LocationManager.getLastKnownLocation(LocationManager.java:1153)
 at ru.ochakovo.ochakoco.FormActivity.GeoClick(FormActivity.java:56)
 at java.lang.reflect.Method.invokeNative(Native Method) 
 at java.lang.reflect.Method.invoke(Method.java:511) 
 at android.view.View$1.onClick(View.java:3586) 
 at android.view.View.performClick(View.java:4084) 
 at android.view.View$PerformClick.run(View.java:16966) 
 at android.os.Handler.handleCallback(Handler.java:615) 
 at android.os.Handler.dispatchMessage(Handler.java:92) 
 at android.os.Looper.loop(Looper.java:137) 
 at android.app.ActivityThread.main(ActivityThread.java:4745) 
 at java.lang.reflect.Method.invokeNative(Native Method) 
 at java.lang.reflect.Method.invoke(Method.java:511) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
 at dalvik.system.NativeStart.main(Native Method) }
JMSilla
  • 1,326
  • 10
  • 17

2 Answers2

0

I think your provider empty null value...

Ramesh Prajapati
  • 654
  • 6
  • 10
0

You are getting this error most probably, due to un-avialability of GPS Providers. Please ask the person to check settings if Gps Provider or Network Provider has been enabled into Location settings..Hope so it is helpful for you

Damini Mehra
  • 3,257
  • 3
  • 13
  • 24