Android Intent:没有空构造函数

时间:2014-07-22 10:17:45

标签: java android android-intent

朋友们,我的代码存在问题。我是android编程的新手,实际上我是一名电子学生。我已经在论坛上搜索了这个问题的答案,但没有说明我要做什么。我已经给出了解释我的问题的日志猫。还有一件事当我添加一个空的构造函数时它会显示错误"空白的最终字段上下文可能尚未初始化"请帮我。

public class TrackService extends IntentService implements LocationListener,
    GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener {

//boolean uploaded = false;
private Context context= getApplicationContext();
String Lat, Long;
// A request to connect to Location Services
private LocationRequest mLocationRequest;

//List<?> pkgAppsList;
//ArrayList<ArrayList<String>> list;
//Context context;
//ArrayList<String> iteminserted = new ArrayList<String>();

private LocationClient mLocationClient;

// ArrayList<ArrayList<String>> UnUploadedData = new
// ArrayList<ArrayList<String>>();


//@Override
//public IBinder onBind(Intent intent) {
    //return null;
//}
//public TrackService() {
    //super("TrackService"); 
 //}
public TrackService(Context context) {
    super("Trackservice"); 
    this.context = context;

}



@Override
public void onCreate() {
    super.onCreate();
    // Create a new global location parameters object
    mLocationRequest = LocationRequest.create();

    /*
     * Set the update interval
     */
    // mLocationRequest
    // .setInterval(LocationUtils.UPDATE_INTERVAL_IN_MILLISECONDS);
    mLocationRequest.setInterval(1000 * 60 * 2);// Every 2 minute

    // Use high accuracy
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    // Set the interval ceiling to one minute
    mLocationRequest
            .setFastestInterval(LocationUtils.FAST_INTERVAL_CEILING_IN_MILLISECONDS);
    mLocationClient = new LocationClient(this, this, this);

}





/*
 * Called by Location Services when the request to connect the client
 * finishes successfully. At this point, you can request the current
 * location or start periodic updates
 */
@Override
public void onConnected(Bundle bundle) {

    startPeriodicUpdates();

}

/**
 * Report location updates to the UI.
 * 
 * @param location
 *            The updated location.
 */
@Override
public void onLocationChanged(Location location) {
    System.out.println(location.getLatitude() + " "+ location.getLongitude());
    Lat = location.getLatitude() + "";
    Long = location.getLongitude() + "";
    //if (isConnectingToInternet())
        new UploadLocationInfo().execute();
    // if (uploaded)
    // this.stopSelf();
    }

/**
 * In response to a request to start updates, send a request to Location
 * Services
 */
private void startPeriodicUpdates() {

    mLocationClient.requestLocationUpdates(mLocationRequest, this);

}

/**
 * In response to a request to stop updates, send a request to Location
 * Services
 */
private void stopPeriodicUpdates() {
    mLocationClient.removeLocationUpdates(this);

}

//public boolean isConnectingToInternet() {
    //ConnectivityManager connectivity = (ConnectivityManager) this
        //  .getSystemService(Context.CONNECTIVITY_SERVICE);
    //if (connectivity != null) {
        //NetworkInfo[] info = connectivity.getAllNetworkInfo();
        //if (info != null)
            //for (int i = 0; i < info.length; i++)
                //if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    //return true;
                //}

    //}
    //return false;
//}



public class UploadLocationInfo extends AsyncTask<Void, Void, Void> {


    @Override
    protected Void doInBackground(Void... params) {
        // Uploading Application List
        //ArrayList<NameValuePair> LOCATION = new ArrayList<NameValuePair>();
        //LOCATION.add(new BasicNameValuePair("LAT", Lat));
        //LOCATION.add(new BasicNameValuePair("LONG", Long));

        //TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext()
            //  .getSystemService(getApplicationContext().TELEPHONY_SERVICE);
        //IMEI = telephonyManager.getDeviceId();
        //LOCATION.add(new BasicNameValuePair("IMEI", IMEI));
        double x = Double.parseDouble(Long);
        double y = Double.parseDouble(Lat);
        EditText text1 = (EditText) ((Activity)context).findViewById(R.id.text1);
        text1.setText("lat"+y+"Log"+x);
        double a = 48.6800000;
        double b = 2.2100000;  
        float[] results = new float[1];
        Location.distanceBetween(y, x, b, a, results);
        float distanceInMeters = results[0];
        boolean isWithin10m = false;
        if( distanceInMeters < 20)
        {
         isWithin10m = true;
        }



        System.out.println("Uploading New Location");

       if(isWithin10m){

        try {
            //open a web page


        } catch (Exception e) {
            e.printStackTrace();
        }
        }else{
            text1.setText("nothing to display");

        }
        return null;
    }
}

@Override
public void onConnectionFailed(ConnectionResult arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onDisconnected() {
    // TODO Auto-generated method stub

}
@Override
public void onDestroy() {
    // If the client is connected
    System.out.println("Destroy");
    if (mLocationClient.isConnected()) {
        stopPeriodicUpdates();
    }
    // After disconnect() is called, the client is considered "dead".
    mLocationClient.disconnect();

    super.onDestroy();
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}


@Override
protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub

}

}

下面给出的Logcat详细信息

07-22 11:49:37.419: D/dalvikvm(5525): Late-enabling CheckJNI  
07-22 11:49:37.490: D/dalvikvm(5525): newInstance failed: no <init>()  
07-22 11:49:37.490: D/AndroidRuntime(5525): Shutting down VM  
07-22 11:49:37.490: W/dalvikvm(5525): threadid=1: thread exiting with uncaught exception (group=0x40d1a930)  
07-22 11:49:37.490: E/AndroidRuntime(5525): FATAL EXCEPTION: main  
07-22 11:49:37.490: E/AndroidRuntime(5525): java.lang.RuntimeException: Unable to instantiate service   com.example.mobiletrackerslave.TrackService: java.lang.InstantiationException: can't instantiate class com.example.mobiletrackerslave.TrackService; no empty constructor  
07-22 11:49:37.490: E/AndroidRuntime(5525):     at   android.app.ActivityThread.handleCreateService(ActivityThread.java:2513)  
07-22 11:49:37.490: E/AndroidRuntime(5525):     at android.app.ActivityThread.access$1600(ActivityThread.java:141)
07-22 11:49:37.490: E/AndroidRuntime(5525):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)  
07-22 11:49:37.490: E/AndroidRuntime(5525):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 11:49:37.490: E/AndroidRuntime(5525):     at android.os.Looper.loop(Looper.java:137)
07-22 11:49:37.490: E/AndroidRuntime(5525):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-22 11:49:37.490: E/AndroidRuntime(5525):     at java.lang.reflect.Method.invokeNative(Native Method)
07-22 11:49:37.490: E/AndroidRuntime(5525):     at java.lang.reflect.Method.invoke(Method.java:511)
07-22 11:49:37.490: E/AndroidRuntime(5525):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-22 11:49:37.490: E/AndroidRuntime(5525):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-22 11:49:37.490: E/AndroidRuntime(5525):     at dalvik.system.NativeStart.main(Native Method)
07-22 11:49:37.490: E/AndroidRuntime(5525): Caused by: java.lang.InstantiationException: can't instantiate class com.example.mobiletrackerslave.TrackService; no empty constructor
07-22 11:49:37.490: E/AndroidRuntime(5525):     at java.lang.Class.newInstanceImpl(Native Method)
07-22 11:49:37.490: E/AndroidRuntime(5525):     at java.lang.Class.newInstance(Class.java:1319)
07-22 11:49:37.490: E/AndroidRuntime(5525):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2510)
07-22 11:49:37.490: E/AndroidRuntime(5525):     ... 10 more
07-22 11:49:39.892: D/dalvikvm(5525): Debugger has detached; object registry had 1 entries

2 个答案:

答案 0 :(得分:0)

使用

private Context context= getApplicationContext();

在您的Intent service.as

OnCreate方法中
context= getApplicationContext();

并更改

private Context context= getApplicationContext();

private Context context;

Exception告诉您需要实现默认的公共构造函数。

   public TrackService() {
      super("TrackService");
    }

默认公共构造函数是一个没有参数的构造函数

在此致电super()并传递String,用于命名IntentService

答案 1 :(得分:0)

您需要在您的类中添加一个空构造函数,即不带参数的构造函数 在类中定义此构造函数

public TrackService() {
    super("Trackservice"); 


}

No empty constructor when create a service