我要做的是使用异步任务在我的应用搜索其位置时显示进度对话框。找到它们的位置后,它应显示一个包含一些信息的警告对话框。但我不是百分百肯定如何做到这一点。这就是我在LocationChanged.java中所拥有的:
@Override
protected Void doInBackground( Void... params ) {
final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
geocoder = new Geocoder(context, Locale.getDefault());
final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
lat =location.getLatitude();
lon = location.getLongitude();
//TextView tv = (TextView) findViewById(R.id.tv3);
try{
addresses=(geocoder.getFromLocation(lat, lon, 1));
if (addresses.size() > 0) {
// String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String id = city.substring(0,city.length()-5);
String state1=String.valueOf(id.charAt(id.length()-3));
String state2=String.valueOf(id.charAt(id.length()-2));
STATE = state1+state2;
//String country = addresses.get(0).getAddressLine(2);
//tv.setText(STATE);
progressDialog.dismiss();
locationManager.removeUpdates(this);
}
else {
//tv.setText("Oops...");
}
}catch(IOException e){
e.printStackTrace();
}
}
这是运行时的logcat:
07-08 17:26:21.456: ERROR/AndroidRuntime(2755): FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:864)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:209)
at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:209)
at android.location.LocationManager._requestLocationUpdates(LocationManager.java:687)
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:512)
at com.nick.whatsthetip.LocationChanged.doInBackground(LocationChanged.java:121)
at com.nick.whatsthetip.LocationChanged.doInBackground(LocationChanged.java:24)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 5 more
07-08 17:26:21.716: ERROR/EmbeddedLogger(696): App crashed! Process: com.nick.whatsthetip
07-08 17:26:21.716: ERROR/EmbeddedLogger(696): App crashed! Package: com.nick.whatsthetip v5 (1.1.0)
07-08 17:26:21.987: ERROR/WindowManager(2755): Activity com.nick.whatsthetip.Settings has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41075198 that was originally added here
android.view.WindowLeaked: Activity com.nick.whatsthetip.Settings has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41075198 that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:468)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:419)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:351)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:171)
at android.view.Window$LocalWindowManager.addView(Window.java:558)
at android.app.Dialog.show(Dialog.java:282)
at android.app.ProgressDialog.show(ProgressDialog.java:116)
at android.app.ProgressDialog.show(ProgressDialog.java:99)
at android.app.ProgressDialog.show(ProgressDialog.java:94)
at com.nick.whatsthetip.LocationChanged.onPreExecute(LocationChanged.java:49)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
at android.os.AsyncTask.execute(AsyncTask.java:534)
at com.nick.whatsthetip.Settings$3.onClick(Settings.java:119)
at android.view.View.performClick(View.java:4102)
答案 0 :(得分:2)
您应该知道,AsyncTask的doInBackground方法是在“Worker”线程上执行的,只有onProgressUpdate和onPostExecute在主线程上执行,因此,尝试修改与UI相关的任何内容(如进度对话框.. 。)来自doInBackground将导致异常,您需要做的是确保修改UI的任何代码都是从主线程调用而不是从工作线程调用...
此致
答案 1 :(得分:1)
此培训课程:Displaying a Location Address向您展示如何运行AsyncTask以反转地理位置。
没有必要在后台响应位置更新。我建议你从doInBackground中取出onLocationChanged并将其作为UI线程中的方法添加。它是异步的,因此不会导致ANR。获得位置后,然后执行查找城市,州等的后台任务。培训课程将向您展示如何在不使用对话框的情况下建立进度指示器。