如何管理在单独的线程进程对话框

如何管理在单独的线程进程对话框

本文介绍了如何管理在单独的线程进程对话框?不能内螺纹创建处理程序,也没有所谓的活套。prepare()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先有2名为 AsyncMySqlInsert CallSoap ,我在AsyncMySqlInsert使用CallSoap不同的非同步类。

其次他们中的每一个都有progressdialog。如果它们运行相互分离它们可以毫无例外施展progressdialogs。 ()发生无法创建内螺纹的处理程序已经不叫尺蠖prepare:但如果他们一起运行名为了java.lang.RuntimeException异常。所以,我怎么能管理在单独的线程进程对话框?

 类AsyncMySqlInsert扩展的AsyncTask<对象,字符串,太虚> {
    保护ProgressDialog progressDialog;
    私人上下文的背景下;    公共AsyncMySqlInsert(上下文的背景下,字符串activityName){
        this.context =背景;
    }    @覆盖
    在preExecute保护无效(){
        progressDialog = ProgressDialog.show(背景下,Kayıtlariçerialınıyor...,卜işlembirkaçdakikasürebilir,lütfenbekleyin,真实,真实);
    }    @覆盖
    保护无效doInBackground(对象... PARAMS){
        数据表filteredDt =(数据表)PARAMS [0];
        数据表dtSonuc =新的DataTable(Gonderim.this);
        SQL字符串=;
        INT ROWID = 0;
        dtSonuc.setColumns(新的String [] {ROWID,成功});        的for(int i = 0; I< filteredDt.getCount();我++){
            [对象] NEWROW =新对象[2]; // 2 kolonlu ROWID |成功
            ROWID =的Integer.parseInt(filteredDt.getValueByColumnName(我的RowID));
            NEWROW [0] =将String.valueOf(ROWID);
            SQL = filteredDt.getValueByColumnName(我,MySQL的);
            尝试{
                GenelSql.getInstance(Gonderim.this).execSql(SQL);
                NEWROW [1] =真;
            }赶上(例外五){
                NEWROW [1] =假;
                Log.e(mysql的畑:e.getMessage()++ SQL);
            }
            dtSonuc.insert(NEWROW,dtSonuc.getCount()℃,0:dtSonuc.getCount());
            publishProgress(Kayıtlariçeriyealınıyor(+将String.valueOf(I)+/+ filteredDt.getCount()+));
        }        DataSet的DS =新的DataSet(Gonderim.this);
        ds.add(dtSonuc);
        setMethodName(KsmMysqlUpdDelForAndroid);
        哈希表<弦乐,对象>参数=新的Hashtable<弦乐,对象>();
        parameters.put(DS,DS);
        parameters.put(REP,txtRep);
        CallSoap肥皂=新CallSoap(Gonderim.this,uri_test,soapAction属性,参数);
        //Log.e(\"KsmMysqlUpdDelForAndroid,彼岸花Mysql'den gelensatırlarıişledim,geriye dogru执行所edilen已经edilmeyenleriayırdığım井数据ILE格日yolluyorum);
        soap.execute();
        soap.setDataDownloadListener(新CallSoap.DataDownloadListener(){
            公共无效dataDownloadedSuccessfully(最后弦乐XDATA){            }
            公共无效dataDownloadFailed(){
            }
        });
        返回null;
    }    @覆盖
    保护无效onProgressUpdate(字符串值...){
        progressDialog.setMessage(值[0]);
        super.onProgressUpdate(值);
    }    @覆盖
     保护无效onPostExecute(虚空结果){
         progressDialog.dismiss();
         getDonen();
     }
 }

堆栈跟踪

  18 12-05:57:34.110:E / AndroidRuntime(31467):致命异常:AsyncTask的#5
12-05 18:57:34.110:E / AndroidRuntime(31467):了java.lang.RuntimeException:执行doInBackground发生错误()
12-05 18:57:34.110:E / AndroidRuntime(31467):在android.os.AsyncTask $ 3.done(AsyncTask.java:266)
12-05 18:57:34.110:E / AndroidRuntime(31467):在java.util.concurrent.FutureTask中$ Sync.innerSetException(FutureTask.java:273)
12-05 18:57:34.110:E / AndroidRuntime(31467):在java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-05 18:57:34.110:E / AndroidRuntime(31467):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:307)
12-05 18:57:34.110:E / AndroidRuntime(31467):在java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-05 18:57:34.110:E / AndroidRuntime(31467):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
12-05 18:57:34.110:E / AndroidRuntime(31467):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:574)
12-05 18:57:34.110:E / AndroidRuntime(31467):在java.lang.Thread.run(Thread.java:1020)
12-05 18:57:34.110:E / AndroidRuntime(31467):了java.lang.RuntimeException:所致。不能内螺纹已不叫尺蠖prepare创造处理器()
。12-05 18:57:34.110:E / AndroidRuntime(31467):在android.os.Handler<&初始化GT;(Handler.java:121)
。12-05 18:57:34.110:E / AndroidRuntime(31467):在android.app.Dialog<&初始化GT;(Dialog.java:100)
。12-05 18:57:34.110:E / AndroidRuntime(31467):在android.app.AlertDialog<&初始化GT;(AlertDialog.java:96)
。12-05 18:57:34.110:E / AndroidRuntime(31467):在android.app.AlertDialog<&初始化GT;(AlertDialog.java:80)
。12-05 18:57:34.110:E / AndroidRuntime(31467):在android.app.ProgressDialog<&初始化GT;(ProgressDialog.java:76)
12-05 18:57:34.110:E / AndroidRuntime(31467):在android.app.ProgressDialog.show(ProgressDialog.java:109)
12-05 18:57:34.110:E / AndroidRuntime(31467):在android.app.ProgressDialog.show(ProgressDialog.java:103)
12-05 18:57:34.110:E / AndroidRuntime(31467):在com.quadro.main.Util.CallSoap.on preExecute(CallSoap.java:201)
12-05 18:57:34.110:E / AndroidRuntime(31467):在android.os.AsyncTask.executeOnExecutor(AsyncTask.java:549)
12-05 18:57:34.110:E / AndroidRuntime(31467):在android.os.AsyncTask.execute(AsyncTask.java:499)
12-05 18:57:34.110:E / AndroidRuntime(31467):在com.quadro.main.Gonderim $ AsyncMySqlInsert.doInBackground(Gonderim.java:751)
12-05 18:57:34.110:E / AndroidRuntime(31467):在com.quadro.main.Gonderim $ AsyncMySqlInsert.doInBackground(Gonderim.java:1)
12-05 18:57:34.110:E / AndroidRuntime(31467):在android.os.AsyncTask $ 2.call(AsyncTask.java:252)
12-05 18:57:34.110:E / AndroidRuntime(31467):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305)
12-05 18:57:34.110:E / AndroidRuntime(31467):... 4个
12-05 18:57:34.590:E /窗口管理器(31467):活动com.quadro.main.Gonderim渗漏,最初这里添加窗口com.android.internal.policy.impl.PhoneWindow$DecorView@40ac56f0
12-05 18:57:34.590:E /窗口管理器(31467):android.view.WindowLeaked:活动com.quadro.main.Gonderim渗漏窗口com.android.internal.policy.impl.PhoneWindow$DecorView@40ac56f0这是原本在这里添加
。12-05 18:57:34.590:E /窗口管理器(31467):在android.view.ViewRoot<&初始化GT;(ViewRoot.java:288)
12-05 18:57:34.590:E /窗口管理器(31467):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:249)
12-05 18:57:34.590:E /窗口管理器(31467):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:193)
12-05 18:57:34.590:E /窗口管理器(31467):在android.view.WindowManagerImpl $ CompatModeWrapper.addView(WindowManagerImpl.java:118)
12-05 18:57:34.590:E /窗口管理器(31467):在android.view.Window $ LocalWindowManager.addView(Window.java:532)
12-05 18:57:34.590:E /窗口管理器(31467):在android.app.Dialog.show(Dialog.java:269)
12-05 18:57:34.590:E /窗口管理器(31467):在android.app.ProgressDialog.show(ProgressDialog.java:115)
12-05 18:57:34.590:E /窗口管理器(31467):在android.app.ProgressDialog.show(ProgressDialog.java:103)
12-05 18:57:34.590:E /窗口管理器(31467):在com.quadro.main.Gonderim $ AsyncMySqlInsert.on preExecute(Gonderim.java:716)
12-05 18:57:34.590:E /窗口管理器(31467):在android.os.AsyncTask.executeOnExecutor(AsyncTask.java:549)
12-05 18:57:34.590:E /窗口管理器(31467):在com.quadro.main.Gonderim $ 7.dataDownloadedSuccessfully(Gonderim.java:394)
12-05 18:57:34.590:E /窗口管理器(31467):在com.quadro.main.Util.CallSoap.onPostExecute(CallSoap.java:246)
12-05 18:57:34.590:E /窗口管理器(31467):在com.quadro.main.Util.CallSoap.onPostExecute(CallSoap.java:1)
12-05 18:57:34.590:E /窗口管理器(31467):在android.os.AsyncTask.finish(AsyncTask.java:590)
12-05 18:57:34.590:E /窗口管理器(31467):在android.os.AsyncTask.access $ 600(AsyncTask.java:149)
12-05 18:57:34.590:E /窗口管理器(31467):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:603)
12-05 18:57:34.590:E /窗口管理器(31467):在android.os.Handler.dispatchMessage(Handler.java:99)
12-05 18:57:34.590:E /窗口管理器(31467):在android.os.Looper.loop(Looper.java:132)
12-05 18:57:34.590:E /窗口管理器(31467):在android.app.ActivityThread.main(ActivityThread.java:4123)
12-05 18:57:34.590:E /窗口管理器(31467):在java.lang.reflect.Method.invokeNative(本机方法)
12-05 18:57:34.590:E /窗口管理器(31467):在java.lang.reflect.Method.invoke(Method.java:491)
12-05 18:57:34.590:E /窗口管理器(31467):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:841)
12-05 18:57:34.590:E /窗口管理器(31467):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
12-05 18:57:34.590:E /窗口管理器(31467):在dalvik.system.NativeStart.main(本机方法)


解决方案

您不能对任何东西,但在UI线程构建的AsyncTask。你将不得不更改要在UI线程上构建并传递到您的AsyncMySqlInsert你CallSoap异步任务。从那里,也许你可以有你的CallSoap异步任务setter方法​​您的MySQL任务将调用(因为它似乎从你的MySQL任务需要CallSoap任务内的值)。

Firstly there are 2 different asynch classes called AsyncMySqlInsert and CallSoap and I am using CallSoap in AsyncMySqlInsert.

Secondly each one of them has progressdialog. They can display their progressdialogs without exception if they run separate from each other. But if they run together an exception called java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() occurs. So how can I manage progress dialogs in separate threads?

    class AsyncMySqlInsert extends AsyncTask<Object, String, Void> {
    protected ProgressDialog progressDialog;
    private Context context;

    public AsyncMySqlInsert(Context context, String activityName) {
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
        progressDialog = ProgressDialog.show(context, "Kayıtlar içeri alınıyor...", "Bu işlem birkaç dakika sürebilir, lütfen bekleyin.", true, true);
    }

    @Override
    protected Void doInBackground(Object... params) {
        DataTable filteredDt = (DataTable)params[0];
        DataTable dtSonuc = new DataTable(Gonderim.this);
        String sql="";
        int rowID = 0;
        dtSonuc.setColumns(new String[]{"rowid","success"});

        for (int i = 0; i < filteredDt.getCount(); i++) {
            Object[] newRow = new Object[2]; //2 kolonlu ROWID | SUCCESS
            rowID = Integer.parseInt(filteredDt.getValueByColumnName(i, "RowID"));
            newRow[0] = String.valueOf(rowID);
            sql = filteredDt.getValueByColumnName(i, "MySql");
            try {
                GenelSql.getInstance(Gonderim.this).execSql(sql);
                newRow[1] = "true";
            } catch (Exception e) {
                newRow[1] = "false";
                Log.e("Mysql Hata: ", e.getMessage() + " " + sql);
            }
            dtSonuc.insert(newRow, dtSonuc.getCount() < 0 ? 0 : dtSonuc.getCount());
            publishProgress("Kayıtlar içeriye alınıyor (" + String.valueOf(i) + " / " + filteredDt.getCount() + ")");
        }

        DataSet ds = new DataSet(Gonderim.this);
        ds.add(dtSonuc);
        setMethodName("KsmMysqlUpdDelForAndroid");
        Hashtable<String, Object> parameters = new Hashtable<String, Object>();
        parameters.put("ds", ds);
        parameters.put("Rep", txtRep);
        CallSoap soap = new CallSoap(Gonderim.this, uri_test, soapAction, parameters);
        //Log.e("KsmMysqlUpdDelForAndroid", "Mysql'den bana gelen satırları işledim, geriye dogru execute edilen ve edilmeyenleri ayırdığım bir dataset ile geri yolluyorum.");


        soap.execute("");
        soap.setDataDownloadListener(new CallSoap.DataDownloadListener() {
            public void dataDownloadedSuccessfully(final String xdata) {

            }
            public void dataDownloadFailed() {
            }
        });


        return null;
    }

    @Override
    protected void onProgressUpdate(String... values) {
        progressDialog.setMessage(values[0]);
        super.onProgressUpdate(values);
    }

    @Override
     protected void onPostExecute(Void result) {
         progressDialog.dismiss();
         getDonen();
     }
 }

STACK TRACE

12-05 18:57:34.110: E/AndroidRuntime(31467): FATAL EXCEPTION: AsyncTask #5
12-05 18:57:34.110: E/AndroidRuntime(31467): java.lang.RuntimeException: An error occured while executing doInBackground()
12-05 18:57:34.110: E/AndroidRuntime(31467):    at android.os.AsyncTask$3.done(AsyncTask.java:266)
12-05 18:57:34.110: E/AndroidRuntime(31467):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-05 18:57:34.110: E/AndroidRuntime(31467):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-05 18:57:34.110: E/AndroidRuntime(31467):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-05 18:57:34.110: E/AndroidRuntime(31467):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-05 18:57:34.110: E/AndroidRuntime(31467):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
12-05 18:57:34.110: E/AndroidRuntime(31467):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
12-05 18:57:34.110: E/AndroidRuntime(31467):    at java.lang.Thread.run(Thread.java:1020)
12-05 18:57:34.110: E/AndroidRuntime(31467): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
12-05 18:57:34.110: E/AndroidRuntime(31467):    at android.os.Handler.<init>(Handler.java:121)
12-05 18:57:34.110: E/AndroidRuntime(31467):    at android.app.Dialog.<init>(Dialog.java:100)
12-05 18:57:34.110: E/AndroidRuntime(31467):    at android.app.AlertDialog.<init>(AlertDialog.java:96)
12-05 18:57:34.110: E/AndroidRuntime(31467):    at android.app.AlertDialog.<init>(AlertDialog.java:80)
12-05 18:57:34.110: E/AndroidRuntime(31467):    at android.app.ProgressDialog.<init>(ProgressDialog.java:76)
12-05 18:57:34.110: E/AndroidRuntime(31467):    at android.app.ProgressDialog.show(ProgressDialog.java:109)
12-05 18:57:34.110: E/AndroidRuntime(31467):    at android.app.ProgressDialog.show(ProgressDialog.java:103)
12-05 18:57:34.110: E/AndroidRuntime(31467):    at com.quadro.main.Util.CallSoap.onPreExecute(CallSoap.java:201)
12-05 18:57:34.110: E/AndroidRuntime(31467):    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:549)
12-05 18:57:34.110: E/AndroidRuntime(31467):    at android.os.AsyncTask.execute(AsyncTask.java:499)
12-05 18:57:34.110: E/AndroidRuntime(31467):    at com.quadro.main.Gonderim$AsyncMySqlInsert.doInBackground(Gonderim.java:751)
12-05 18:57:34.110: E/AndroidRuntime(31467):    at com.quadro.main.Gonderim$AsyncMySqlInsert.doInBackground(Gonderim.java:1)
12-05 18:57:34.110: E/AndroidRuntime(31467):    at android.os.AsyncTask$2.call(AsyncTask.java:252)
12-05 18:57:34.110: E/AndroidRuntime(31467):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-05 18:57:34.110: E/AndroidRuntime(31467):    ... 4 more
12-05 18:57:34.590: E/WindowManager(31467): Activity com.quadro.main.Gonderim has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40ac56f0 that was originally added here
12-05 18:57:34.590: E/WindowManager(31467): android.view.WindowLeaked: Activity com.quadro.main.Gonderim has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40ac56f0 that was originally added here
12-05 18:57:34.590: E/WindowManager(31467):     at android.view.ViewRoot.<init>(ViewRoot.java:288)
12-05 18:57:34.590: E/WindowManager(31467):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:249)
12-05 18:57:34.590: E/WindowManager(31467):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:193)
12-05 18:57:34.590: E/WindowManager(31467):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:118)
12-05 18:57:34.590: E/WindowManager(31467):     at android.view.Window$LocalWindowManager.addView(Window.java:532)
12-05 18:57:34.590: E/WindowManager(31467):     at android.app.Dialog.show(Dialog.java:269)
12-05 18:57:34.590: E/WindowManager(31467):     at android.app.ProgressDialog.show(ProgressDialog.java:115)
12-05 18:57:34.590: E/WindowManager(31467):     at android.app.ProgressDialog.show(ProgressDialog.java:103)
12-05 18:57:34.590: E/WindowManager(31467):     at com.quadro.main.Gonderim$AsyncMySqlInsert.onPreExecute(Gonderim.java:716)
12-05 18:57:34.590: E/WindowManager(31467):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:549)
12-05 18:57:34.590: E/WindowManager(31467):     at com.quadro.main.Gonderim$7.dataDownloadedSuccessfully(Gonderim.java:394)
12-05 18:57:34.590: E/WindowManager(31467):     at com.quadro.main.Util.CallSoap.onPostExecute(CallSoap.java:246)
12-05 18:57:34.590: E/WindowManager(31467):     at com.quadro.main.Util.CallSoap.onPostExecute(CallSoap.java:1)
12-05 18:57:34.590: E/WindowManager(31467):     at android.os.AsyncTask.finish(AsyncTask.java:590)
12-05 18:57:34.590: E/WindowManager(31467):     at android.os.AsyncTask.access$600(AsyncTask.java:149)
12-05 18:57:34.590: E/WindowManager(31467):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:603)
12-05 18:57:34.590: E/WindowManager(31467):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 18:57:34.590: E/WindowManager(31467):     at android.os.Looper.loop(Looper.java:132)
12-05 18:57:34.590: E/WindowManager(31467):     at android.app.ActivityThread.main(ActivityThread.java:4123)
12-05 18:57:34.590: E/WindowManager(31467):     at java.lang.reflect.Method.invokeNative(Native Method)
12-05 18:57:34.590: E/WindowManager(31467):     at java.lang.reflect.Method.invoke(Method.java:491)
12-05 18:57:34.590: E/WindowManager(31467):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
12-05 18:57:34.590: E/WindowManager(31467):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
12-05 18:57:34.590: E/WindowManager(31467):     at dalvik.system.NativeStart.main(Native Method)
解决方案

You can't construct an AsyncTask on anything but the UI Thread. You are going to have to change your CallSoap async task to be constructed on the UI thread and passed into your AsyncMySqlInsert. From there, maybe you could have a setter method on your CallSoap async task that your MySql task will call (since it appears you need values from your MySql task inside of the CallSoap task).

这篇关于如何管理在单独的线程进程对话框?不能内螺纹创建处理程序,也没有所谓的活套。prepare()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:26