Helder De Baere
Helder De Baere

Reputation: 450

java.lang.IllegalStateException: Could not execute method of the activity error after changing package name

So I've developed an app, wanted to upload it to the play store, but when it said i had to change the package name different from com.example.app_name I changed it. But now I'm getting an error:

    05-29 22:35:15.599: E/AndroidRuntime(2196): java.lang.IllegalStateException: Could not execute method of the activity
05-29 22:35:15.599: E/AndroidRuntime(2196):     at android.view.View$1.onClick(View.java:3039)
05-29 22:35:15.599: E/AndroidRuntime(2196):     at android.view.View.performClick(View.java:3480)
05-29 22:35:15.599: E/AndroidRuntime(2196):     at android.view.View$PerformClick.run(View.java:13983)
05-29 22:35:15.599: E/AndroidRuntime(2196):     at android.os.Handler.handleCallback(Handler.java:605)
05-29 22:35:15.599: E/AndroidRuntime(2196):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-29 22:35:15.599: E/AndroidRuntime(2196):     at android.os.Looper.loop(Looper.java:137)
05-29 22:35:15.599: E/AndroidRuntime(2196):     at android.app.ActivityThread.main(ActivityThread.java:4340)
05-29 22:35:15.599: E/AndroidRuntime(2196):     at java.lang.reflect.Method.invokeNative(Native Method)
05-29 22:35:15.599: E/AndroidRuntime(2196):     at java.lang.reflect.Method.invoke(Method.java:511)
05-29 22:35:15.599: E/AndroidRuntime(2196):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-29 22:35:15.599: E/AndroidRuntime(2196):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-29 22:35:15.599: E/AndroidRuntime(2196):     at dalvik.system.NativeStart.main(Native Method)
05-29 22:35:15.599: E/AndroidRuntime(2196): Caused by: java.lang.reflect.InvocationTargetException
05-29 22:35:15.599: E/AndroidRuntime(2196):     at java.lang.reflect.Method.invokeNative(Native Method)
05-29 22:35:15.599: E/AndroidRuntime(2196):     at java.lang.reflect.Method.invoke(Method.java:511)
05-29 22:35:15.599: E/AndroidRuntime(2196):     at android.view.View$1.onClick(View.java:3034)
05-29 22:35:15.599: E/AndroidRuntime(2196):     ... 11 more
05-29 22:35:15.599: E/AndroidRuntime(2196): Caused by: java.lang.NullPointerException
05-29 22:35:15.599: E/AndroidRuntime(2196):     at com.gip.icomplain.Home.getGPSLocation(Home.java:209)
05-29 22:35:15.599: E/AndroidRuntime(2196):     at com.gip.icomplain.Home.send(Home.java:130)
05-29 22:35:15.599: E/AndroidRuntime(2196):     ... 14 more

here is my code:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_home);

        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        imv = (ImageView) findViewById(R.id.imvFoto);
        onderwerp = (TextView) findViewById(R.id.txtSubject);
        commentaar = (TextView) findViewById(R.id.txtComment);
        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            showGPSDisabledAlertToUser();
        }
    }
    public void getGPSLocation() {
        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            showGPSDisabledAlertToUser();
        }
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new MyLocationListener());
        currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if (currentLocation != null) {
            longitude = currentLocation.getLongitude();
            latitude = currentLocation.getLatitude();
        }
        EXTRA_TEXT = "http://maps.google.com/maps?q=" + latitude + "," + longitude;
    }
}

Please help, this is very important!!!!

Upvotes: 0

Views: 728

Answers (2)

Alejandro Colorado
Alejandro Colorado

Reputation: 6094

You are getting a NullPointerException at your getGPSLocation, in your locationManager here:

if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
    showGPSDisabledAlertToUser();
}

You should remove the re-declaration of locationManager (removing the type LocationManager) both in onCreate and in getGPSLocation:

@Override
public void onCreate(Bundle savedInstanceState) {
...
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Instead of: 
// LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
...
}

and

public void getGPSLocation() {
...
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Instead of: 
// LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
...
}

BTW, you should also check for a null value of locationmanager before every

if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157447

You have locationManager as class member but redeclare it inside the onCreate. This way you hide the memeber class and it is never initialized.

Then in getGpsLocation() you use it this way:

public void getGPSLocation() {
        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            showGPSDisabledAlertToUser();
        }

remove LocationManager from onCreate

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_home);

        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

Upvotes: 1

Related Questions