我正在尝试将 iPhone 应用程序移植到 Android,但遇到一些启动困难。
我有以下函数来获取数据:
public void onClick(View v) {
try{
//Toast.makeText(getApplicationContext(), "Starting update", Toast.LENGTH_SHORT).show();
progressDialog = ProgressDialog.show(MainActivity.this, "", "Loading...");
new Thread() {
public void run() {
try{
DataManager manager = new DataManager(getApplicationContext());
manager.updateData();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Error...");
alertDialog.setMessage(e.getMessage());
alertDialog.show();
}
progressDialog.dismiss();
}
}.start();
} catch (Exception e) {
Log.e(TAG, "Problem with getting the data: " + e.getMessage());
Toast.makeText(getApplicationContext(), "Error during update",
Toast.LENGTH_SHORT).show();
}
}
但是如果发生异常,我会收到以下错误(我最初尝试使用 AsyncTask,但遇到了同样的问题):
08-29 20:23:24.389: ERROR/WindowManager(974): Activity org.idoms.iDomsAndroid.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@412fcc38 that was originally added here
android.view.WindowLeaked: Activity org.idoms.iDomsAndroid.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@412fcc38 that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
at android.view.Window$LocalWindowManager.addView(Window.java:537)
at android.app.Dialog.show(Dialog.java:278)
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 org.idoms.iDomsAndroid.MainActivity$1.onClick(MainActivity.java:46)
at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14105)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
这样做的正确方法是什么?