Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.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 ProgressDialog未在AsyncTask中显示_Android - Fatal编程技术网

Android ProgressDialog未在AsyncTask中显示

Android ProgressDialog未在AsyncTask中显示,android,Android,我正在使用AsyncTask将一些文件上载到服务器,我想在AsyncTask中显示一个进度对话框。 我的AsyncTask运行良好,执行了所有步骤,但它从未显示对话框。我不知道我做错了什么。 我写了下面的代码,但没有工作。有人能帮忙吗 活动: public class MainActivity extends AppCompatActivity { ImageView videoImage ; EditText editTextVideoTitle; EditText

我正在使用AsyncTask将一些文件上载到服务器,我想在AsyncTask中显示一个进度对话框。 我的AsyncTask运行良好,执行了所有步骤,但它从未显示对话框。我不知道我做错了什么。 我写了下面的代码,但没有工作。有人能帮忙吗

活动:

public class MainActivity extends AppCompatActivity {

    ImageView videoImage ;
    EditText editTextVideoTitle;
    EditText editTextVideoDescription;
    Button btnPostButton;
    Button btnCancelButton;

    private String mVideoPath;
    private String mVideoThumb;
    String strVideoTitle;
    String strVideoDescription;
    boolean videoPosted;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_vid_with_desc);

       
        editTextVideoTitle = (EditText) findViewById(R.id.videoTitle);
        editTextVideoDescription = (EditText) findViewById(R.id.videoDescription);

        btnPostButton = (Button) findViewById(R.id.postButton);
        btnCancelButton = (Button) findViewById(R.id.cancelButton);;

        mVideoPath = getIntent().getStringExtra("path");
        mVideoThumb = getIntent().getStringExtra("thumb");

       
        btnPostButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                videoPosted = postVideo();

                if (videoPosted) {
                    Toast.makeText(getApplicationContext(), "Your video posted successfully.", Toast.LENGTH_SHORT).show();
                } else {

                    Toast.makeText(getApplicationContext(), "Error in posting video, please try again.", Toast.LENGTH_SHORT).show();
                }

            }
        });


    }
    
    public boolean postVideo() {

        Log.i("Info", "PostVidWithDescActivity : postVideo : Start");

        String strUserId = "";
        String strReservedfield = "";
        boolean isVideoPosted = false;

        sharedPreferences = this.getSharedPreferences("com.app.rapid", Context.MODE_PRIVATE);

        strVideoTitle = editTextVideoTitle.getText().toString();
        strVideoDescription = editTextVideoDescription.getText().toString();
        
        
        try {

            strUserId = sharedPreferences.getString("userId", "");
            strReservedfield = "ReservedField";
        
            outputData = new PostToServerAsyncTask().execute(mVideoPath, strUserId, strVideoTitle, strVideoDescription, strReservedfield).get();

        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }

        if (null != outputData && outputData.equalsIgnoreCase("200")) {
            isVideoPosted = true;
        } else{

            isVideoPosted = false;
        }

        

        return isVideoPosted;
    }

private class PostToServerAsyncTask extends AsyncTask<String, Void, String> {

        private String content;
        private String Error = null;
        private int serverResponseCode;

        Context context;

        ProgressDialog progressDialog;

        @Override
        protected void onPreExecute() {

            super.onPreExecute();

            Log.i("inputBuilder","PostToServerAsyncTask : onPreExecute Start") ;

            progressDialog = new ProgressDialog(MainActivity.this);

            progressDialog.setMessage("Loading...");
            progressDialog.setIndeterminate(false);
            //progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
            progressDialog.setCancelable(false);
            progressDialog.show();

            Log.i("inputBuilder","PostToServerAsyncTask : onPreExecute End") ;
        }

        @Override
        protected String doInBackground(String... inputData) {


                /*
                    File upload code
                */

            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            

            return "Some string";
        }

        @Override
        protected void onPostExecute(String result) {

            super.onPostExecute(result);

            Log.i("inputBuilder","PostToServerAsyncTask : onPostExecute Start") ;

                if (null != progressDialog && progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }


            Log.i("inputBuilder","PostToServerAsyncTask : onPostExecute End") ;
        }
        
}
}
public类MainActivity扩展了AppCompatActivity{
ImageView视频图像;
编辑文本编辑视频标题;
编辑文本编辑视频描述;
按钮btnPostButton;
按钮btnCancelButton;
私有字符串路径;
私密字符串;
字符串strVideoTitle;
字符串描述;
布尔视频发布;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_vid_with_desc);
editTextVideoTitle=(EditText)findViewById(R.id.videoTitle);
editTextVideoDescription=(EditText)findViewById(R.id.videoDescription);
btnPostButton=(按钮)findViewById(R.id.postButton);
btnCancelButton=(按钮)findViewById(R.id.cancelButton);;
mVideoPath=getIntent().getStringExtra(“路径”);
mvideotumb=getIntent().getStringExtra(“thumb”);
btnPostButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
videoPosted=postVideo();
如果(视频发布){
Toast.makeText(getApplicationContext(),“您的视频发布成功。”,Toast.LENGTH\u SHORT.show();
}否则{
Toast.makeText(getApplicationContext(),“发布视频时出错,请重试。”,Toast.LENGTH_SHORT).show();
}
}
});
}
公共布尔postVideo(){
Log.i(“信息”,“PostVidWithDescActivity:postVideo:Start”);
字符串strUserId=“”;
字符串streservedfield=“”;
布尔值isVideoPosted=false;
SharedReferences=this.getSharedReferences(“com.app.rapid”,Context.MODE\u PRIVATE);
strVideoTitle=editTextVideoTitle.getText().toString();
strVideoDescription=editTextVideoDescription.getText().toString();
试一试{
strUserId=SharedReferences.getString(“userId”,即“”);
streservedfield=“ReservedField”;
outputData=new PostToServerAsyncTask().execute(mVideoPath、strUserId、strVideoTitle、strVideoDescription、strReservedfield).get();
}捕获(执行例外){
e、 printStackTrace();
}捕捉(中断异常e){
e、 printStackTrace();
}捕获(例外e){
e、 printStackTrace();
}
if(null!=outputData&&outputData.equalsIgnoreCase(“200”)){
isVideoPosted=true;
}否则{
isVideoPosted=false;
}
返回是视频张贴;
}
私有类PostToServerAsyncTask扩展了AsyncTask{
私有字符串内容;
私有字符串错误=null;
专用int服务器响应代码;
语境;
进行对话进行对话;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
i(“inputBuilder”、“PostToServerAsyncTask:onPreExecute Start”);
progressDialog=新建progressDialog(MainActivity.this);
progressDialog.setMessage(“加载…”);
progressDialog.setUndeterminate(false);
//progressDialog.setProgressStyle(progressDialog.STYLE_水平);
progressDialog.setProgressStyle(progressDialog.STYLE_微调器);
progressDialog.getWindow().setBackgroundDrawable(新的ColorDrawable(Color.YELLOW));
progressDialog.setCancelable(假);
progressDialog.show();
i(“inputBuilder”、“PostToServerAsyncTask:onPreExecute End”);
}
@凌驾
受保护的字符串doInBackground(字符串…输入数据){
/*
文件上传代码
*/
试一试{
睡眠(10000);
}捕捉(中断异常e){
e、 printStackTrace();
}
返回“somestring”;
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
i(“inputBuilder”、“PostToServerAsyncTask:onPostExecute启动”);
if(null!=progressDialog&&progressDialog.isShowing()){
progressDialog.disclose();
}
i(“inputBuilder”、“PostToServerAsyncTask:onPostExecute端”);
}
}
}

您能试试这个吗

@Override
protected void onPreExecute() {
    getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
           progressDialog = new ProgressDialog(MainActivity.this);

            progressDialog.setMessage("Loading...");
            progressDialog.setIndeterminate(false);
            //progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
            progressDialog.setCancelable(false);
            progressDialog.show();
    });
}

我认为一般的问题在于异步任务,它实际上在单独的线程上

请显示如何调用异步任务。@blackapps,现在我已经在其中发布了完整的活动和异步任务,请查看。这是因为.get()没有看到进度对话框。不要使用.get()。在onPostExecute中处理doInBackground的结果。您好@blackapps,它起作用了…万分感谢您的帮助。@BalajiMote,在对代码进行了更深入的调查之后,我注意到您使用的是
get
而不是
execute
,这使得调用完全在UI线程上进行并阻止它。另一方面,
execute
将在异步模式下工作,因此您必须在代码中添加一些内容来处理响应。请您尝试执行而不是获取,好吗?