Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在AlertDialog之后显示ProgressDialog_Java_Android_Android Asynctask_Android Alertdialog_Progressdialog - Fatal编程技术网

Java 如何在AlertDialog之后显示ProgressDialog

Java 如何在AlertDialog之后显示ProgressDialog,java,android,android-asynctask,android-alertdialog,progressdialog,Java,Android,Android Asynctask,Android Alertdialog,Progressdialog,我无法隐藏警报对话框 当用户按下按钮时,我会显示一个使用以下代码创建的对话框: new AlertDialog.Builder(this) .setTitle(R.string.enterPassword) .setView(textEntryView) .setPositiveButton(R.string.ok, new Dia

我无法隐藏警报对话框

当用户按下按钮时,我会显示一个使用以下代码创建的对话框:

new AlertDialog.Builder(this)
            .setTitle(R.string.enterPassword)                
            .setView(textEntryView)          
            .setPositiveButton(R.string.ok, 
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            String password = pwdText.getText().toString();
                            dialog.dismiss();
                            processUserAction(password,targetUri);
                        }
                    })
            .setNegativeButton(R.string.cancel, 
                    new DialogInterface.OnClickListener() {                    
                        public void onClick(DialogInterface dialog, int whichButton) {
                            }                
                        })             
            .
            create();
在“processUserAction”方法中执行了一些繁重的操作,在其中我使用了一个显示ProgressDialog的AysncTask

我遇到的问题是,提示输入密码的对话框从未出现在屏幕上(我尝试了dismise(),cancel()。 我猜在onClick方法完成之前,它一直在那里

所以,我的问题是如何关闭AlertDialog,以便显示ProgressDialog

我一直在尝试的另一种方法是在AlertDialog中设置DismissListener并从那里调用繁重的操作,但我运气不佳(没有调用它)

编辑:添加异步任务代码

public class BkgCryptOperations extends AsyncTask<File,Void,Integer>{

    @Override
    protected Integer doInBackground(File... files) {
        if (files!=null && files.length > 0){
            File source = files[0];
            File target = files[1];
            return cryptAction.process(source,password, target);
        }

        return Constants.RetCodeKO;
    }

    CryptAction cryptAction;
    String password;
    ProgressDialog progressDialog;


    public BkgCryptOperations (CryptAction cryptAction,String password,ProgressDialog progressDialog){
        this.cryptAction=cryptAction;
        this.password=password;
        this.progressDialog=progressDialog;
    }

    @Override
    protected void onPreExecute() {
        if (progressDialog!=null){
            progressDialog.show();
        }
    }
    protected void onPostExecute(Integer i) {
        if (progressDialog!=null){
            progressDialog.dismiss();
        }
    }


}
公共类BkgCryptOperations扩展异步任务{
@凌驾
受保护的整数doInBackground(文件…文件){
如果(files!=null&&files.length>0){
文件源=文件[0];
文件目标=文件[1];
返回cryptoction.process(源、密码、目标);
}
return Constants.RetCodeKO;
}
密码作用;密码作用;
字符串密码;
进行对话进行对话;
公共BkgCryptOperations(CryptAction CryptAction、字符串密码、ProgressDialog ProgressDialog){
this.cryptAction=cryptAction;
this.password=密码;
this.progressDialog=progressDialog;
}
@凌驾
受保护的void onPreExecute(){
如果(progressDialog!=null){
progressDialog.show();
}
}
受保护的void onPostExecute(整数i){
如果(progressDialog!=null){
progressDialog.disclose();
}
}
}

提前感谢

您可以在AlertDialog之外创建一个监听器,以抽象出OnClickListener中的积极按钮逻辑。这样,可以通知侦听器,AlertDialog将立即被取消。然后,用户从AlertDialog输入的任何处理都可以独立于AlertDialog进行。我不确定这是否是实现这一目标的最佳方式,但在过去它对我很有效

据我所知,我看不出AsyncTask代码有任何明显的问题

public interface IPasswordListener {
    public void onReceivePassword(String password);
}

IPasswordListener m_passwordListener = new IPasswordListener {
    @Override
    public void onReceivePassword(String password) {
        processUserAction(password,targetUri);
    }
}

public void showPasswordDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.enterPassword);
    builder.setView(textEntryView);
    builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            m_passwordListener.onReceivePassword(pwdText.getText().toString());
            dialog.dismiss();
        }
    });
    builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            dialog.cancel();
        }
    });
    builder.show();
}

您可以在AlertDialog之外创建一个监听器,以抽象出OnClickListener中的积极按钮逻辑。这样,可以通知侦听器,AlertDialog将立即被取消。然后,用户从AlertDialog输入的任何处理都可以独立于AlertDialog进行。我不确定这是否是实现这一目标的最佳方式,但在过去它对我很有效

据我所知,我看不出AsyncTask代码有任何明显的问题

public interface IPasswordListener {
    public void onReceivePassword(String password);
}

IPasswordListener m_passwordListener = new IPasswordListener {
    @Override
    public void onReceivePassword(String password) {
        processUserAction(password,targetUri);
    }
}

public void showPasswordDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.enterPassword);
    builder.setView(textEntryView);
    builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            m_passwordListener.onReceivePassword(pwdText.getText().toString());
            dialog.dismiss();
        }
    });
    builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            dialog.cancel();
        }
    });
    builder.show();
}

能否显示processUserAction(..)的代码?没有必要包括解雇。 我做了一些非常相似的事情,没有任何问题。。。 代码如下:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Export data.\nContinue?")
            .setCancelable(false)
            .setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            String file = getObra().getNome();
                            d = new ProgressDialog(MenuActivity.this);
                            d.setTitle("Exporting...");
                            d.setMessage("please wait...");
                            d.setIndeterminate(true);
                            d.setCancelable(false);
                            d.show();
                            export(file);
                        }
                    })
            .setNegativeButton("No",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
    AlertDialog alert = builder.create();
    alert.show();
在导出(文件)中,我打开线程:

private void export(final String file) {


    new Thread() {
        public void run() {
            try {
                ExportData ede = new ExportData(
                        getApplicationContext(), getPmo().getId(),
                        file);
                ede.export();
                handlerMessage("Done!!");
            } catch (Exception e) {
                handlerMessage(e.getMessage());
                System.out.println("ERROR!!!" + e.getMessage());
            }
        }
    }.start();
}
在handlerMessage中,我关闭progressDialog并显示最后一条消息。
希望对您有所帮助。

能否显示processUserAction(..)的代码?没有必要包括解雇。 我做了一些非常相似的事情,没有任何问题。。。 代码如下:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Export data.\nContinue?")
            .setCancelable(false)
            .setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            String file = getObra().getNome();
                            d = new ProgressDialog(MenuActivity.this);
                            d.setTitle("Exporting...");
                            d.setMessage("please wait...");
                            d.setIndeterminate(true);
                            d.setCancelable(false);
                            d.show();
                            export(file);
                        }
                    })
            .setNegativeButton("No",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
    AlertDialog alert = builder.create();
    alert.show();
在导出(文件)中,我打开线程:

private void export(final String file) {


    new Thread() {
        public void run() {
            try {
                ExportData ede = new ExportData(
                        getApplicationContext(), getPmo().getId(),
                        file);
                ede.export();
                handlerMessage("Done!!");
            } catch (Exception e) {
                handlerMessage(e.getMessage());
                System.out.println("ERROR!!!" + e.getMessage());
            }
        }
    }.start();
}
在handlerMessage中,我关闭progressDialog并显示最后一条消息。
希望对您有所帮助。

以下是我如何做到这一点的一个例子:

public void daten_remove_on_click(View button) {
        // Nachfragen
        if (spinadapter.getCount() > 0) {
            AlertDialog Result = new AlertDialog.Builder(this)
                    .setIcon(R.drawable.icon)
                    .setTitle(getString(R.string.dialog_data_remove_titel))
                    .setMessage(getString(R.string.dialog_data_remove_text))
                    .setNegativeButton(getString(R.string.dialog_no),
                            new DialogInterface.OnClickListener() {
                                public void onClick(
                                        DialogInterface dialogInterface, int i) {
                                    // Nicht löschen
                                    dialogInterface.cancel();
                                }
                            })
                    .setPositiveButton(getString(R.string.dialog_yes),
                            new DialogInterface.OnClickListener() {
                                public void onClick(
                                        DialogInterface dialogInterface, int i) {
                                    String _quellenName = myCursor.getString(1);
                                    deleteQuellenRecord(_quellenName);
                                    zuletztGelöscht = _quellenName;
                                }
                            }).show();
        } else {
            // Keine Daten mehr vorhanden
            Toast toast = Toast.makeText(Daten.this,
                    getString(R.string.dialog_data_remove_empty),
                    Toast.LENGTH_SHORT);
            toast.show();
        }
    }
以下是deleteQuellenRecord的代码:

private void deleteQuellenRecord(String _quellenName) {
        String DialogTitel = getString(R.string.daten_delete_titel);
        String DialogText = getString(R.string.daten_delete_text);
        // Dialogdefinition Prograssbar
        dialog = new ProgressDialog(this) {
            @Override
            public boolean onSearchRequested() {
                return false;
            }
        };
        dialog.setCancelable(false);
        dialog.setTitle(DialogTitel);
        dialog.setIcon(R.drawable.icon);
        dialog.setMessage(DialogText);
        // set the progress to be horizontal
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        // reset the bar to the default value of 0
        dialog.setProgress(0);
        // set the maximum value
        dialog.setMax(4);
        // display the progressbar
        increment = 1;
        dialog.show();
        // Thread starten
        new Thread(new MyDeleteDataThread(_quellenName)) {
            @Override
            public void run() {
                try {
                    // Datensatz löschen
                    myDB.execSQL("DELETE ... ');");
                    progressHandler
                            .sendMessage(progressHandler.obtainMessage());
                    myDB.execSQL("DELETE ...);");
                    // active the update handler
                    progressHandler
                            .sendMessage(progressHandler.obtainMessage());
                    myDB.execSQL("DELETE ...;");
                    // active the update handler
                    progressHandler
                            .sendMessage(progressHandler.obtainMessage());
                    // Einstellung speichern
                    try {
                        settings.edit().putString("LetzteQuelle", "-1")
                                .commit();
                    } catch (Exception ex) {
                        settings.edit().putString("LetzteQuelle", "").commit();
                    }

                } catch (Exception ex) {
                    // Wait dialog beenden
                    dialog.dismiss();
                    Log.e("Glutenfrei Viewer",
                            "Error in activity MAIN - remove data", ex); // log
                                                                            // the
                                                                            // error
                }
                // Wait dialog beenden
                dialog.dismiss();

            }
        }.start();
        this.onCreate(null);
    }
通过异步任务,我可以这样做:

private class RunningAlternativSearch extends
            AsyncTask<Integer, Integer, Void> {


        final ProgressDialog dialog = new ProgressDialog(SearchResult.this) {
            @Override
            public boolean onSearchRequested() {
                return false;
            }
        };



        @Override
        protected void onPreExecute() {
            alternativeSucheBeendet = false;
            String DialogTitel = getString(R.string.daten_wait_titel);
            DialogText = getString(R.string.dialog_alternativ_text);
            DialogZweiteChance = getString(R.string.dialog_zweite_chance);
            DialogDritteChance = getString(R.string.dialog_dritte_chance);
            sucheNach = getString(R.string.dialog_suche_nach);
            dialog.setCancelable(true);
            dialog.setTitle(DialogTitel);
            dialog.setIcon(R.drawable.icon);
            dialog.setMessage(DialogText);
            dialog.setOnDismissListener(new OnDismissListener() {
                public void onDismiss(DialogInterface arg0) {
                    // TODO Auto-generated method stub
                    cancleBarcodeWorker();
                    if (alternativeSucheBeendet==false){
                        // Activity nur beenden wenn die Suche
                        // nicht beendet wurde, also vom User abgebrochen
                        Toast toast = Toast.makeText(SearchResult.this, SearchResult.this
                                .getString(R.string.toast_suche_abgebrochen),
                                Toast.LENGTH_LONG);
                        toast.show();
                        myDB.close();
                        SearchResult.this.finish();
                    }
                }
            });
            dialog.show();
        }


        ...
运行AlternativeSearch的私有类扩展
异步任务{
final ProgressDialog dialog=新建ProgressDialog(SearchResult.this){
@凌驾
公共布尔值onSearchRequested(){
返回false;
}
};
@凌驾
受保护的void onPreExecute(){
备选方案,如hebeendet=false;
String DialogTitel=getString(R.String.daten\u wait\u titel);
DialogText=getString(R.string.dialog\u alternativ\u text);
DialogZweiteChance=getString(R.string.dialog\uzweite\uchance);
DialogDritteChance=getString(R.string.dialog\u dritte\u chance);
sucheNach=getString(R.string.dialog\u suche\u nach);
对话框。可设置可取消(true);
dialog.setTitle(DialogTitel);
dialog.setIcon(R.drawable.icon);
设置消息(DialogText);
setOnDismissListener(新的OnDismissListener(){
public void onDismiss(对话框接口arg0){
//TODO自动生成的方法存根
cancleBarcodeWorker();
if(可选的Suchebeendet==false){
//活动努尔·比登·温迪·苏切
//nicht beendet wurde,也是vom用户abgebrochen
Toast Toast=Toast.makeText(SearchResult.this,SearchResult.this
.getString(R.string.toast_suche_abgebrochen),
吐司长度(长);
toast.show();
myDB.close();
SearchResult.this.finish();
}
}
});
dialog.show();
}
...

以下是我如何做到这一点的一个例子:

public void daten_remove_on_click(View button) {
        // Nachfragen
        if (spinadapter.getCount() > 0) {
            AlertDialog Result = new AlertDialog.Builder(this)
                    .setIcon(R.drawable.icon)
                    .setTitle(getString(R.string.dialog_data_remove_titel))
                    .setMessage(getString(R.string.dialog_data_remove_text))
                    .setNegativeButton(getString(R.string.dialog_no),
                            new DialogInterface.OnClickListener() {
                                public void onClick(
                                        DialogInterface dialogInterface, int i) {
                                    // Nicht löschen
                                    dialogInterface.cancel();
                                }
                            })
                    .setPositiveButton(getString(R.string.dialog_yes),
                            new DialogInterface.OnClickListener() {
                                public void onClick(
                                        DialogInterface dialogInterface, int i) {
                                    String _quellenName = myCursor.getString(1);
                                    deleteQuellenRecord(_quellenName);
                                    zuletztGelöscht = _quellenName;
                                }
                            }).show();
        } else {
            // Keine Daten mehr vorhanden
            Toast toast = Toast.makeText(Daten.this,
                    getString(R.string.dialog_data_remove_empty),
                    Toast.LENGTH_SHORT);
            toast.show();
        }
    }
以下是deleteQuellenRecord的代码:

private void deleteQuellenRecord(String _quellenName) {
        String DialogTitel = getString(R.string.daten_delete_titel);
        String DialogText = getString(R.string.daten_delete_text);
        // Dialogdefinition Prograssbar
        dialog = new ProgressDialog(this) {
            @Override
            public boolean onSearchRequested() {
                return false;
            }
        };
        dialog.setCancelable(false);
        dialog.setTitle(DialogTitel);
        dialog.setIcon(R.drawable.icon);
        dialog.setMessage(DialogText);
        // set the progress to be horizontal
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        // reset the bar to the default value of 0
        dialog.setProgress(0);
        // set the maximum value
        dialog.setMax(4);
        // display the progressbar
        increment = 1;
        dialog.show();
        // Thread starten
        new Thread(new MyDeleteDataThread(_quellenName)) {
            @Override
            public void run() {
                try {
                    // Datensatz löschen
                    myDB.execSQL("DELETE ... ');");
                    progressHandler
                            .sendMessage(progressHandler.obtainMessage());
                    myDB.execSQL("DELETE ...);");
                    // active the update handler
                    progressHandler
                            .sendMessage(progressHandler.obtainMessage());
                    myDB.execSQL("DELETE ...;");
                    // active the update handler
                    progressHandler
                            .sendMessage(progressHandler.obtainMessage());
                    // Einstellung speichern
                    try {
                        settings.edit().putString("LetzteQuelle", "-1")
                                .commit();
                    } catch (Exception ex) {
                        settings.edit().putString("LetzteQuelle", "").commit();
                    }

                } catch (Exception ex) {
                    // Wait dialog beenden
                    dialog.dismiss();
                    Log.e("Glutenfrei Viewer",
                            "Error in activity MAIN - remove data", ex); // log
                                                                            // the
                                                                            // error
                }
                // Wait dialog beenden
                dialog.dismiss();

            }
        }.start();
        this.onCreate(null);
    }
通过异步任务,我可以这样做:

private class RunningAlternativSearch extends
            AsyncTask<Integer, Integer, Void> {


        final ProgressDialog dialog = new ProgressDialog(SearchResult.this) {
            @Override
            public boolean onSearchRequested() {
                return false;
            }
        };



        @Override
        protected void onPreExecute() {
            alternativeSucheBeendet = false;
            String DialogTitel = getString(R.string.daten_wait_titel);
            DialogText = getString(R.string.dialog_alternativ_text);
            DialogZweiteChance = getString(R.string.dialog_zweite_chance);
            DialogDritteChance = getString(R.string.dialog_dritte_chance);
            sucheNach = getString(R.string.dialog_suche_nach);
            dialog.setCancelable(true);
            dialog.setTitle(DialogTitel);
            dialog.setIcon(R.drawable.icon);
            dialog.setMessage(DialogText);
            dialog.setOnDismissListener(new OnDismissListener() {
                public void onDismiss(DialogInterface arg0) {
                    // TODO Auto-generated method stub
                    cancleBarcodeWorker();
                    if (alternativeSucheBeendet==false){
                        // Activity nur beenden wenn die Suche
                        // nicht beendet wurde, also vom User abgebrochen
                        Toast toast = Toast.makeText(SearchResult.this, SearchResult.this
                                .getString(R.string.toast_suche_abgebrochen),
                                Toast.LENGTH_LONG);
                        toast.show();
                        myDB.close();
                        SearchResult.this.finish();
                    }
                }
            });
            dialog.show();
        }


        ...
运行AlternativeSearch的私有类扩展
异步任务{
final ProgressDialog dialog=新建ProgressDialog(SearchResult.this){
@凌驾
公共布尔值onSearchRequested(){
返回false;
}
};
@凌驾
受保护的void onPreExecute(){
备选方案,如hebeendet=false;