0

所以我开发了一个应用程序,想将其上传到 Play 商店,但是当它说我必须更改与 com.example.app_name 不同的包名称时,我更改了它。但现在我收到一个错误:

    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

这是我的代码:

    @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;
    }
}

请帮忙,这很重要!!!!

4

2 回答 2

1

您将 locationManager 作为类成员,但在 onCreate 中重新声明它。这样你就隐藏了 memeber 类并且它永远不会被初始化。

然后在 getGpsLocation() 你这样使用它:

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

LocationManager从 onCreate中删除

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

        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
于 2013-05-29T20:59:51.287 回答
0

getGPSLocation你在你的, 在你locationManager这里得到一个 NullPointerException :

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

您应该在 in和 in 中locationManager删除(删除 type LocationManager)的重新声明:onCreategetGPSLocation

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

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

顺便说一句,您还应该在每次之前检查 locationmanager 的空值

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

于 2013-05-29T21:02:00.590 回答