简体   繁体   English

在调试我的android-java代码时需要帮助

[英]Need help in debugging my android-java code

Log cat shows "Runtime exception - cant create handler inside thread that has not called looper.prepare??I want to send location update of my phone to some other phone via sms after a fixed interval of time.Please help . Suggest ways to save power also 日志猫显示“运行时异常-无法在尚未调用looper.prepare的线程内创建处理程序?我想在固定的时间间隔后通过短信将手机的位置更新发送到其他手机。请帮助。建议保存方法力量也

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
 et1 = (EditText)findViewById(R.id.editText1);
 et2 = (EditText)findViewById(R.id.editText2);
 b1  = (Button)findViewById(R.id.button1 );
 t1 = new Timer();
 t2 = new Timer();
 lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
 listener = new LocationListener() {

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        SmsManager sm = SmsManager.getDefault();
         String message = String.format(
                    "New Location \n Longitude: %1$s \n Latitude: %2$s",
                    location.getLongitude(), location.getLatitude()             );
        String number = "5556";
        sm.sendTextMessage(number,null,message,null,null);
    }
};

Schedule request update after every fixed interval 在每个固定间隔后计划请求更新

t1.scheduleAtFixedRate(new TimerTask() { t1.scheduleAtFixedRate(new TimerTask(){

    @Override
    public void run() {
        // TODO Auto-generated method stub
        lm.requestSingleUpdate(LocationManager.GPS_PROVIDER, listener,null);
    }
},0,300000);

}

}

It's not clear from the TimerTask documentation, but I suspect each task runs on its own thread. TimerTask文档中尚不清楚,但是我怀疑每个任务都在自己的线程上运行。 Also, the documentation for the LocationManager.requestSingleUpdate call you're using says “If looper is null then the callbacks will be called on the main thread”, but I suspect this is wrong, because it disagrees with the one with the alternative signature, which says “If looper is null then the callbacks will be called on the current thread”. 另外,您正在使用的LocationManager.requestSingleUpdate调用的文档说:“如果looper为null,则将在主线程上调用回调”,但是我怀疑这是错误的,因为它与带有备用签名的那个不同,它表示“如果looper为null,则将在当前线程上调用回调”。 If the latter is correct, that would explain your problem, because you are calling requestSingleUpdate within your TimerTask thread, which has no Looper . 如果后者是正确的,那将解释您的问题,因为您是在没有Looper TimerTask线程中调用requestSingleUpdate

Perhaps use the result from getMainLooper instead of null for the last arg to requestSingleUpdate . 也许将getMainLooper的结果而不是null用作requestSingleUpdate的最后一个参数。

That error basically means "You are trying to do something in a background thread that you are not allowed to do in a background thread. 该错误的基本含义是“您正在尝试在后台线程中执行某些操作,而这是您不允许在后台线程中执行的操作。

Your listener will handle the asynchronous nature of the location request. 您的侦听器将处理位置请求的异步性质。 You do not need to thread it. 您不需要线程化。

If you DO need things threaded, read up on Asynctask . 如果您确实需要线程处理,请阅读Asynctask

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM