2013-04-24 61 views
0

我打電話給我的服務,MyServices,從另一個活動,UserStatus,但該應用程序正在強制關閉。無法啓動服務意圖與illegalArgumentException

這裏是我的UserStatus活動:

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


} 

MyServices類:

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 [email protected] 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 ... 許多thanx提前...

回答

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) 

在您的代碼中,您已將null傳遞給該方法的參數LocationListener

 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 
    } 
}; 
+0

代替requestLocationUpdate,我試圖requestSingleUpdate但STL其強制關閉應用程序......如果不爲空,我應該怎麼初始化ll1 ..? – akshayg21 2013-04-24 08:23:13

+0

我編輯了一些示例代碼的答案。看看它是否有幫助。 – TactMayers 2013-04-25 06:24:44

相關問題