0

我正在从另一个活动 UserStatus 调用我的服务 MyServices,但该应用程序正在强制关闭。

这是我的用户状态活动:

import java.util.HashMap;

import org.json.JSONArray;

import com.salesforce.androidsdk.rest.RestRequest;
import com.salesforce.androidsdk.rest.RestResponse;
import com.salesforce.androidsdk.rest.RestClient.AsyncRequestCallback;
import com.salesforce.samples.templateapp.MyServices;

import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.FragmentActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.location.*;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

public class UserStatus extends FragmentActivity {

    ToggleButton statusToggle;



     // Creating Toggle button to monitor the stauts of donor

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_status);
        statusToggle = (ToggleButton) findViewById(R.id.toggleButton1);
        final TextView infoText;

        infoText = (TextView) findViewById(R.id.textInfo);

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        final boolean gpsEnabled = lm
                .isProviderEnabled(LocationManager.GPS_PROVIDER);




        statusToggle
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {

                        if (isChecked) {
                            statusToggle.setTextOn("On");
                            Toast.makeText(getApplicationContext(),
                                    "Toggle is on", Toast.LENGTH_LONG).show();

                            if (!gpsEnabled) {
                                new EnableGPSDialogFragment().show(
                                    getFragmentManager(), "OK");
                        }
                            Log.v("X","before myservices");
                            //Starting service if the toggle is in checked state.
                            startService(new Intent(getBaseContext(),
                                MyServices.class));
                            //Intent in1 = new Intent(UserStatus.this,GPSUpdate.class);

                            //startActivity(in1);



                            } 
                        else {
                            statusToggle.setTextOff("Off");
                            Toast.makeText(getApplicationContext(),
                                    "Toggle is off", Toast.LENGTH_LONG).show();
                            //Stopping service if the toggle is not in checked state.                           
                            stopService(new Intent(getBaseContext(),
                                    MyServices.class));
                        }
                    }
                });
    }

    //Opening GPS settings if GPS is not enabled
    private void enableLocationSettings() {
        Intent settingsIntent = new Intent(
                Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(settingsIntent);
    }

    private class EnableGPSDialogFragment extends DialogFragment {
        public Dialog onCreateDialog(Bundle SavedInstanceState) {
            return new AlertDialog.Builder(getActivity())
                    .setTitle(R.string.enable_GPS)
                    .setMessage(R.string.enable_GPS_dialog)
                    .setPositiveButton(R.string.enable_GPS,
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    enableLocationSettings();

                                }

                            }).create();
        }
    }


}

我的服务类:

import java.util.HashMap;

import com.salesforce.androidsdk.rest.RestClient;
import com.salesforce.androidsdk.rest.RestRequest;
import com.salesforce.androidsdk.rest.RestResponse;
import com.salesforce.androidsdk.rest.RestClient.AsyncRequestCallback;

import android.app.Service;
import android.content.Context;

import android.content.Intent;
import android.location.GpsStatus.Listener;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyServices extends Service implements LocationListener {
    RestClient client;
    double plat;
    double plong;
    int Two_Min=2*60*1000;



    public int onStartCommand(Intent intent, int flags, int startId) {

        Toast.makeText(this, "Location Updation has started", Toast.LENGTH_LONG)
                .show();
        Log.v("X","Response:in onStartCommand()");
        LocationListener ll1 = null;

        LocationManager lm1 = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
           Location location = lm1.getLastKnownLocation(LocationManager.GPS_PROVIDER);
           Log.v("X",
                    "Response: "+location);


            Log.v("X",
                    "Response:After creating lm and ll ");

            lm1.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll1);
            Log.v("X",
                    "Response:After lm1.requestLocationUpdates ");

        return START_STICKY;


    }




    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Location Updation has stoped", Toast.LENGTH_LONG)
                .show();
    }
......there is code here but have omitted it to reduce the code length........
}

logcat 显示以下错误:

04-24 11:25:00.412: E/AndroidRuntime(8338): FATAL EXCEPTION: main
04-24 11:25:00.412: E/AndroidRuntime(8338): java.lang.RuntimeException: Unable to start service com.salesforce.samples.templateapp.MyServices@413d52e8 with Intent { cmp=com.salesforce.samples.templateapp/.MyServices }: java.lang.IllegalArgumentException: listener==null
04-24 11:25:00.412: E/AndroidRuntime(8338):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2403)
04-24 11:25:00.412: E/AndroidRuntime(8338):     at android.app.ActivityThread.access$1900(ActivityThread.java:127)
04-24 11:25:00.412: E/AndroidRuntime(8338):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1221)
04-24 11:25:00.412: E/AndroidRuntime(8338):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 11:25:00.412: E/AndroidRuntime(8338):     at android.os.Looper.loop(Looper.java:137)
04-24 11:25:00.412: E/AndroidRuntime(8338):     at android.app.ActivityThread.main(ActivityThread.java:4448)
04-24 11:25:00.412: E/AndroidRuntime(8338):     at java.lang.reflect.Method.invokeNative(Native Method)
04-24 11:25:00.412: E/AndroidRuntime(8338):     at java.lang.reflect.Method.invoke(Method.java:511)
04-24 11:25:00.412: E/AndroidRuntime(8338):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
04-24 11:25:00.412: E/AndroidRuntime(8338):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
04-24 11:25:00.412: E/AndroidRuntime(8338):     at dalvik.system.NativeStart.main(Native Method)
04-24 11:25:00.412: E/AndroidRuntime(8338): Caused by: java.lang.IllegalArgumentException: listener==null
04-24 11:25:00.412: E/AndroidRuntime(8338):     at android.location.LocationManager.requestLocationUpdates(LocationManager.java:451)
04-24 11:25:00.412: E/AndroidRuntime(8338):     at com.salesforce.samples.templateapp.MyServices.onStartCommand(MyServices.java:51)
04-24 11:25:00.412: E/AndroidRuntime(8338):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2386)
04-24 11:25:00.412: E/AndroidRuntime(8338):     ... 10 more

请提出代码有什么问题...如果需要,会将剩余的代码放在 MyServices 中...提前多谢...

4

1 回答 1

1

根本原因是:

04-24 11:25:00.412: E/AndroidRuntime(8338): Caused by: java.lang.IllegalArgumentException: listener==null
04-24 11:25:00.412: E/AndroidRuntime(8338):     at android.location.LocationManager.requestLocationUpdates(LocationManager.java:451)

在您的代码中,您已将参数传递nullLocationListener该方法。

        LocationListener ll1 = null;
...
        lm1.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll1);

您可以创建LocationListener如下示例的实例:

    Log.v("X","Response:in onStartCommand()");
LocationListener ll1 = new LocationListener() {

    public void onStatusChanged(String provider, int status, Bundle extras) {
                //Put code to handle status change here
    }

    public void onProviderEnabled(String provider) {
                //Put code to handle when a provider is enabled
    }

    public void onProviderDisabled(String provider) {
                //Put code to handle when a provider is disabled
    }

    public void onLocationChanged(Location location) {
                //Put code to handle change in location
    }
}; 
于 2013-04-24T07:05:01.077 回答