Эта проблема сводит меня с ума. Я пропустил некоторые базовые, но очень важные знания о том, как обрабатывать длительные операции в новом потоке, созданном в рамках действия, и как изменять компоненты вида, такие как текст и т.д. После длительной операции.
Позвольте мне сначала показать вам часть моего кода, где эта проблема:
mProgressDialog = ProgressDialog.show(mContext, "Tripplanner", "please wait...", true, false);
connectAndGetRoute();
private void connectAndGetRoute(){
new Thread(){
@Override
public void run() {
try {
if(!connectTo9292ov()) return;// conncetto9292ov() connects to a website, parses the reasult into an arraylist. The arraylist contains route.
} catch(UnknownHostException e){
Toast.makeText(mContext, "failed to connect to server", Toast.LENGTH_LONG).show();
}catch (ClientProtocolException e) {
Toast.makeText(mContext, "failed to connect to server", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(mContext, "failed to connect to server", Toast.LENGTH_LONG).show();
}
handler.post(runConnection);
}
}.start();
handler = new Handler();
runConnection = new Runnable(){
@Override
public void run() {
mProgressDialog.dismiss();
showOnScreen();
}
};
}
и это ошибка, которую я получаю:
ERROR/WindowManager(8297): Activity mp.tripplanner.OvPlanner has leaked window [email protected] that was originally added here
ERROR/WindowManager(8297): android.view.WindowLeaked: Activity mp.tripplanner.OvPlanner has leaked window [email protected] that was originally added here
ERROR/WindowManager(8297): at android.view.ViewRoot.<init>(ViewRoot.java:251)
ERROR/WindowManager(8297): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
ERROR/WindowManager(8297): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
ERROR/WindowManager(8297): at android.view.Window$LocalWindowManager.addView(Window.java:424)
ERROR/WindowManager(8297): at android.app.Dialog.show(Dialog.java:241)
ERROR/WindowManager(8297): at android.app.ProgressDialog.show(ProgressDialog.java:107)
ERROR/WindowManager(8297): at android.app.ProgressDialog.show(ProgressDialog.java:95)
ERROR/WindowManager(8297): at mp.tripplanner.OvPlanner$3.onClick(OvPlanner.java:351)
ERROR/WindowManager(8297): at android.view.View.performClick(View.java:2408)
ERROR/WindowManager(8297): at android.view.View$PerformClick.run(View.java:8817)
ERROR/WindowManager(8297): at android.os.Handler.handleCallback(Handler.java:587)
ERROR/WindowManager(8297): at android.os.Handler.dispatchMessage(Handler.java:92)
ERROR/WindowManager(8297): at android.os.Looper.loop(Looper.java:144)
ERROR/WindowManager(8297): at android.app.ActivityThread.main(ActivityThread.java:4937)
ERROR/WindowManager(8297): at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/WindowManager(8297): at java.lang.reflect.Method.invoke(Method.java:521)
ERROR/WindowManager(8297): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
ERROR/WindowManager(8297): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
ERROR/WindowManager(8297): at dalvik.system.NativeStart.main(Native Method)
Но еще одно сообщение об ошибке записывается в журнал до указанного выше:
ERROR/AndroidRuntime(8297): FATAL EXCEPTION: Thread-9
ERROR/AndroidRuntime(8297): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
ERROR/AndroidRuntime(8297): at android.os.Handler.<init>(Handler.java:121)
ERROR/AndroidRuntime(8297): at android.widget.Toast.<init>(Toast.java:68)
ERROR/AndroidRuntime(8297): at android.widget.Toast.makeText(Toast.java:231)
ERROR/AndroidRuntime(8297): at mp.tripplanner.OvPlanner$4.run(OvPlanner.java:371)
Благодарим вас за помощь.