启动服务时java.lang.StackOverflowError

时间:2013-06-19 14:44:58

标签: java android stack-overflow fatal-error

我不确定我究竟改变了什么 - 但突然之间我得到了一个“java.lang.StackOverflowError”,从我正在阅读的内容是由于infinate递归。由于我是Java / Android开发相对较新的事实,我以前从未遇到过这个问题,因此我在确定这个递归问题产生的确切位置时遇到了一些麻烦。

非常感谢任何帮助。

  • Amani Swann

ACTIVITY SOURCE:

public class ServiceStarter extends Activity {


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

    startService(new Intent(this, DataCountService.class));

}
}

服务来源:

public class DataCountService extends Service {
    String text = "USI;0; 0375515651;21/45/37/01/07/14;CN100.757,WN300.545;CO100.554,WO20.747";
    String ERROR = "DataCountService";

    private Intent getIntent() {
        // TODO Auto-generated method stub
        return null;
    }

    // compat to support older devices
    @Override
    public void onStart(Intent intent, int startId) {
        onStartCommand(intent, 0, startId);

        // @Override
        // public int onStartCommand(Intent intent, int flags, int startId) {
        //display in long period of time
        Toast.makeText(getApplicationContext(), "GO", Toast.LENGTH_LONG).show();
        Bundle extras = getIntent().getExtras();
        // check for Enable or Disable Value - if set to enable
        if (text.contains("USI;1;")) {

            // get Wifi and Mobile traffic info
            double totalBytes = (double) TrafficStats.getTotalRxBytes()
                    + TrafficStats.getTotalTxBytes();
            double mobileBytes = TrafficStats.getMobileRxBytes()
                    + TrafficStats.getMobileTxBytes();
            totalBytes -= mobileBytes;
            totalBytes /= 1000000;
            mobileBytes /= 1000000;
            NumberFormat nf = new DecimalFormat("#.##");
            String totalStr = nf.format(totalBytes);
            String mobileStr = nf.format(mobileBytes);
            String info = String.format("WN%s,CN%s", totalStr, mobileStr);

            // send traffic info via sms
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage("7865555555", null, info, null, null);
            String alarm = Context.ALARM_SERVICE;

            // TODO Auto-generated method stub

            // check for Enable or Disable Value - if set to disable
        } else if

        (text.contains("USI;1;")) {
            stopSelf();
            // check for Enable or Disable Value - if set to any other character
        } else {

            Log.e(ERROR, "Invalid Enable/Disable Value");

        }

    }

    @Override
    public void onCreate() {

    }

    private void startServiceTimer() {
        timer.schedule(new TimerTask() {
            public void run() {

                Bundle extras = getIntent().getExtras();
                // check for Enable or Disable Value - if set to enable
                if (text.contains("USI;1;")) {

                    // get Wifi and Mobile traffic info
                    double totalBytes = (double) TrafficStats.getTotalRxBytes()
                            + TrafficStats.getTotalTxBytes();
                    double mobileBytes = TrafficStats.getMobileRxBytes()
                            + TrafficStats.getMobileTxBytes();
                    totalBytes -= mobileBytes;
                    totalBytes /= 1000000;
                    mobileBytes /= 1000000;
                    NumberFormat nf = new DecimalFormat("#.###");
                    String totalStr = nf.format(totalBytes);
                    String mobileStr = nf.format(mobileBytes);
                    String info = String.format("WN%s,CN%s", totalStr,
                            mobileStr);

                    // save data in sharedPreferences

                    SharedPreferences pref = getApplicationContext()
                            .getSharedPreferences("WifiData", 0);
                    Editor editor = pref.edit();
                    editor.putString("last_month", info);
                    editor.commit();

                    // send SMS (including current Wifi usage and last month's
                    // data
                    // as well)
                    String sms = "";
                    sms += ("WN" + (TrafficStats.getTotalRxBytes()
                            + TrafficStats.getTotalTxBytes() - (TrafficStats
                            .getMobileRxBytes() + TrafficStats
                            .getMobileTxBytes())) / 1000000);
                    sms += ("DN" + (TrafficStats.getMobileRxBytes() + TrafficStats
                            .getMobileTxBytes()) / 1000000);

                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage("7865555555", null,
                            sms + pref.getString("last_month", ""), null, null);

                    // check for Enable or Disable Value - if set to disable
                } else if

                (text.contains("USI;1;")) {
                    stopSelf();
                    // check for Enable or Disable Value - if set to any other
                    // character
                } else {

                    Log.e(ERROR, "Invalid Enable/Disable Value");

                }

            }
        }, DELAY_INTERVAL, PERIOD);
    }

    private Timer timer = new Timer();
    private final long PERIOD = 1000 * 15; // x min
    private final long DELAY_INTERVAL = 0; // x Seconds

    @Override
    public IBinder onBind(Intent intent) {

        // TODO Auto-generated method stub

        return null;

    }

    @Override
    public boolean onUnbind(Intent intent) {

        // TODO Auto-generated method stub

        return super.onUnbind(intent);

    }

}

1 个答案:

答案 0 :(得分:0)

请勿从onStartCommand()拨打onStart(),因为这会为您提供无限递归。服务不应该调用 onStartCommand(),除非在其超类super.onStartCommand(...)上覆盖onStartCommand()

此外,由于onStart()已被弃用超过三年,请覆盖onStartCommand(),而不是onStart()