java.lang.IllegalStateException:无法执行活动的方法

时间:2016-04-29 11:23:42

标签: java android

在开发过程中,出现了类似的问题,也因为我是新手,不知道如何处理它,感谢提前帮助的人! 我试图改变一些东西来重写而所有其他方法都失败了,我无法理解我在什么时候做错了什么

活动

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) {}
    };
}

错误

 {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) }

2 个答案:

答案 0 :(得分:0)

我认为您的提供商空空值...

答案 1 :(得分:0)

由于GPS提供商的不可用性,您最有可能收到此错误。如果Gps提供商或网络提供商已启用到位置设置,请让此人检查设置。希望这对您有所帮助