Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.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
Android-捕获异常时在doinbackground期间显示警报对话框_Android_Exception_Android Asynctask_Try Catch_Android Alertdialog - Fatal编程技术网

Android-捕获异常时在doinbackground期间显示警报对话框

Android-捕获异常时在doinbackground期间显示警报对话框,android,exception,android-asynctask,try-catch,android-alertdialog,Android,Exception,Android Asynctask,Try Catch,Android Alertdialog,我有这段代码,只要它点击Source.httpConn,它就会立即捕获,并在下面发送异常以捕获 try { JSONTokener sbTokener = new JSONTokener(Source.httpConn(infoUrlStr, Main.this).toString()); //array için hazırlandı JSONArray jArray=new J

我有这段代码,只要它点击
Source.httpConn
,它就会立即捕获,并在下面发送异常以捕获

         try
            {
            JSONTokener sbTokener = new JSONTokener(Source.httpConn(infoUrlStr, Main.this).toString());
            //array için hazırlandı

            JSONArray jArray=new JSONArray(sbTokener);
            //arraye eklendi
            for(int i=0; i<(jArray.length()); i++)
    .
    .
    .
     }
如何使我的应用程序不崩溃,只显示此警报对话框,然后返回到原来的主要活动。以下是我的警报对话框方法:

      protected void AlertDialogDisp(Context dialogContext, String head, String log) {


    new AlertDialog.Builder(dialogContext)
    .setTitle(head)
    .setMessage(log)
    .setPositiveButton("okay", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // On Alert Dialog Button
            dialog.dismiss();
        }
    })

    .show();
}

无法从
doInbackground
更新ui<在后台线程上调用code>doInbackground。Ui应该在Ui线程上更新。在
doInbackground
中返回结果,并在
onPostExecute

有关更多信息,请查看文档

您还可以使用
runOnUithread
,这是活动类的一种方法

runOnUiThread(new Runnable(){
        public void run() {
           // dispaly dialog here
           // no network related operation here
        }
     });

无法从
doInbackground
更新ui<在后台线程上调用code>doInbackground。Ui应该在Ui线程上更新。在
doInbackground
中返回结果,并在
onPostExecute

有关更多信息,请查看文档

您还可以使用
runOnUithread
,这是活动类的一种方法

runOnUiThread(new Runnable(){
        public void run() {
           // dispaly dialog here
           // no network related operation here
        }
     });

在活动类的方法
runonuihead
中显示对话框

runOnUiThread(new Runnable(){
        public void run() {

                  //write your alert dialog code here

        }
     });

在活动类的方法
runonuihead
中显示对话框

runOnUiThread(new Runnable(){
        public void run() {

                  //write your alert dialog code here

        }
     });

你的AyncTask是活动类的内部类吗?@Raghunandan是的。你的AyncTask是活动类的内部类吗?@Raghunandan是的。这就是问题所在,我该怎么做?我可以返回结果,但我需要在后台显示alertdialogfails@SuhrahjRothgar返回一个状态为0 0r 1或布尔值的结果,并在
onPostExecute
中显示一个对话框。我看不出有什么问题that@Raghunandan我有点困了:我明白了,非常感谢,这很好!这就是问题所在,我该怎么做?我可以返回结果,但我需要在后台显示alertdialogfails@SuhrahjRothgar返回一个状态为0 0r 1或布尔值的结果,并在
onPostExecute
中显示一个对话框。我看不出有什么问题that@Raghunandan我有点困了:我明白了,非常感谢,这很好!如果在ui线程上运行与n/w相关的操作,则会出现异常。我想你不需要在这里试抓。只有在catch op中才显示对话框,所以对话框代码应该在run Method中,但为什么需要try catch来显示警报对话框?您可以删除它。这不是问题如果在ui线程上运行n/w相关操作,则会出现异常。我想你不需要在这里试抓。只有在catch op中才显示对话框,所以对话框代码应该在run Method中,但为什么需要try catch来显示警报对话框?您可以删除它。这不是问题