1

我正在尝试将 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)

这样做的正确方法是什么?

4

3 回答 3

1

我的猜测是,线程不能与主 UI 线程元素交互。

progressDialog 是在主 Ui 线程中创建的,其他线程无法与之交互。你可以在字段中给它一个信号并检查它的进度,但我真的建议使用 Asynctask 来完成这个任务。

异步任务

于 2012-08-29T20:08:49.573 回答
0

UI 元素(如 AlertDialog)只能从主 (UI) 线程进行操作。如果您尝试从后台线程(这是您正在使用的new Thread() { ... })操作它们,您将收到此异常。

runOnUiThread(new Runnable() { ... });您可以通过在访问AlertDialogor时调用来解决此问题progressDialog,但这非常、非常、非常脏

The better solution is to use AsyncTask (or even better AsyncTaskLoader as it takes care of orientationchanges and some other stuff for you). When using AsyncTask, make sure not to access UI elements in the doInBackground()-method. As the name, saysdoInBackground(...) is executed in a background thread. If you attempt to manipulate UI elements from that method, you'll indeed get the same error as you're experiencing now. If you want to access your UI-elements from your AsyncTask, you can do so in the onPreExecute(...), onProgressUpdate(...) and onPostExecute(...) methods.

于 2012-08-29T20:23:02.357 回答
0

The reason for the window leaking is due to your alertDialog not being dismissed before the app closes.

于 2012-08-29T21:48:14.763 回答