Android ProgressDialog不与AsyncTask一起工作

Android ProgressDialog不与AsyncTask一起工作,android,android-asynctask,android-dialog,android-progressbar,android-runonuithread,Android,Android Asynctask,Android Dialog,Android Progressbar,Android Runonuithread,我正在尝试在另一个线程上运行函数。不幸的是,它只是继续进行而没有被忽视。如果我在run函数中没有Try-catch,那么Try-catch可以工作。我认为这可能是我在函数中的异步任务 我也发现了这个错误 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 这是我的密码 btnSubmit.setOnClickListener(new Vie

我正在尝试在另一个线程上运行函数。不幸的是,它只是继续进行而没有被忽视。如果我在run函数中没有Try-catch,那么Try-catch可以工作。我认为这可能是我在函数中的异步任务

我也发现了这个错误

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
这是我的密码

    btnSubmit.setOnClickListener(new View.OnClickListener() {
    InputStream is = null;

    @Override
    public void onClick(View v) {


        pDialog = ProgressDialog.show(AvvikelseActivity.this, "Registrerar", "Vänta");

        //Start the function here


        //start a new thread to process job
        new Thread(new Runnable() {
            @Override
            public void run() {
                //heavy job here
                //send message to main thread
                try {

                    Bitmap immage = null;
                    //preapare the image
                    immage = BitmapFactory.decodeFile(imgurl);
                    final int maxSize = 960;
                    int outWidth;
                    int outHeight;
                    int inWidth = immage.getWidth();
                    int inHeight = immage.getHeight();
                    if (inWidth > inHeight) {
                        outWidth = maxSize;
                        outHeight = (inHeight * maxSize) / inWidth;
                    } else {
                        outHeight = maxSize;
                        outWidth = (inWidth * maxSize) / inHeight;
                    }
                    Bitmap resizedBitmap = Bitmap.createScaledBitmap(immage, outWidth, outHeight, false);


                    //initiate string imagedata, which will be the string for the actual image
                    String imagedata = null;
                    //encode the image
                    imagedata = encodeTobase64(immage);

                    //Setting nameValuePairs
                    final List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);

                    //adding the string variables
                    nameValuePairs.add(new BasicNameValuePair("reporter", reporter));
                    nameValuePairs.add(new BasicNameValuePair("department", department));
                    nameValuePairs.add(new BasicNameValuePair("errortype", errortype));
                    nameValuePairs.add(new BasicNameValuePair("title", title));
                    nameValuePairs.add(new BasicNameValuePair("description", description));
                    nameValuePairs.add(new BasicNameValuePair("percaution", percaution));
                    nameValuePairs.add(new BasicNameValuePair("imgurl", imgurl));
                    nameValuePairs.add(new BasicNameValuePair("dataset", dataset));
                    nameValuePairs.add(new BasicNameValuePair("image", imagedata));
                    nameValuePairs.add(new BasicNameValuePair("phoneid", android_id));
                    nameValuePairs.add(new BasicNameValuePair("typ", TAG_TYP));

                    //Task to upload the information in background, on another thread
                    new AsyncTask<ApiConnector, Long, Boolean>() {
                        @Override
                        protected Boolean doInBackground(ApiConnector... apiConnectors) {
                            return apiConnectors[0].uploadImageToserver(nameValuePairs);
                        }
                    }.execute(new ApiConnector());

                    //tell the user that it is registred
                    Toast.makeText(getBaseContext(), getString(R.string.registred), Toast.LENGTH_LONG).show();

                    pDialog.dismiss();
                    //change window to main
                    Intent mainIntent = new Intent(getBaseContext(), MainActivity.class);
                    startActivity(mainIntent);

                } catch (Exception e) {
                    Log.d("MainActivity", e.toString());
                }

            }
        }).start();


    } //Here ends onClick(View v)
});
btnSubmit.setOnClickListener(新视图.OnClickListener(){
InputStream=null;
@凌驾
公共void onClick(视图v){
pDialog=ProgressDialog.show(AvvikelseActivity.this,“Registrerar”,“Vänta”);
//在这里启动函数
//启动新线程以处理作业
新线程(newrunnable()){
@凌驾
公开募捐{
//这里的工作很繁重
//向主线程发送消息
试一试{
位图image=null;
//预先准备图像
immage=BitmapFactory.decodeFile(imgurl);
最终整数maxSize=960;
外展;
内部高度;
int inWidth=image.getWidth();
int inHeight=image.getHeight();
如果(英寸宽度>英寸高度){
外径=最大尺寸;
外高=(内高*最大尺寸)/内宽;
}否则{
外高=最大尺寸;
外径=(inWidth*maxSize)/inHeight;
}
Bitmap resizedBitmap=Bitmap.createScaledBitmap(Image、outWidth、outHeight、false);
//初始化字符串imagedata,它将是实际图像的字符串
字符串imagedata=null;
//对图像进行编码
imagedata=encodeTobase64(IMAGE);
//设置nameValuePairs
最终列表nameValuePairs=新的ArrayList(1);
//添加字符串变量
添加(新的BasicNameValuePair(“报告者”,报告者));
添加(新的BasicNameValuePair(“部门”,部门));
添加(新的BasicNameValuePair(“errortype”,errortype));
添加(新的BasicNameValuePair(“title”,title));
添加(新的BasicNameValuePair(“description”,description));
添加(新的BasicNameValuePair(“percaution”,percaution));
添加(新的BasicNameValuePair(“imgurl”,imgurl));
添加(新的BasicNameValuePair(“数据集”,数据集));
添加(新的BasicNameValuePair(“图像”,imagedata));
添加(新的BasicNameValuePair(“phoneid”,android_id));
添加(新的BasicNameValuePair(“典型”,标签类型));
//在后台,在另一个线程上上载信息的任务
新建异步任务(){
@凌驾
受保护的布尔doInBackground(ApiConnector…ApiConnector){
返回apiConnectors[0]。上载ImageToServer(nameValuePairs);
}
}.execute(新的ApiConnector());
//告诉用户它已注册
Toast.makeText(getBaseContext()、getString(R.string.registed)、Toast.LENGTH_LONG.show();
pDialog.disclose();
//将窗口更改为主窗口
Intent maintent=新的Intent(getBaseContext(),MainActivity.class);
星触觉(主旨);
}捕获(例外e){
Log.d(“MainActivity”,例如toString());
}
}
}).start();
}//此处结束onClick(视图v)
});
您需要使用此

           runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    pDialog.dismiss();
                }
            });
而不是普通的
pDialog.disclose()


对Ui的任何更新都必须从Ui线程或主线程进行。

不需要有不同的线程<代码>异步任务
已在不同的线程上工作。您还需要在
AsyncTask
完成后关闭对话框并启动新活动

试试这个

btnSubmit.setOnClickListener(new View.OnClickListener() {
    InputStream is = null;

    @Override
    public void onClick(View v) {
        pDialog = ProgressDialog.show(AvvikelseActivity.this, "Registrerar", "Vänta");
        try {
                Bitmap immage = null;
                //preapare the image
                immage = BitmapFactory.decodeFile(imgurl);
                final int maxSize = 960;
                int outWidth;
                int outHeight;
                int inWidth = immage.getWidth();
                int inHeight = immage.getHeight();
                if (inWidth > inHeight) {
                    outWidth = maxSize;
                    outHeight = (inHeight * maxSize) / inWidth;
                } else {
                    outHeight = maxSize;
                    outWidth = (inWidth * maxSize) / inHeight;
                }
                Bitmap resizedBitmap = Bitmap.createScaledBitmap(immage, outWidth, outHeight, false);
                //initiate string imagedata, which will be the string for the actual image
                String imagedata = null;
                //encode the image
                imagedata = encodeTobase64(immage);
                //Setting nameValuePairs
                final List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
                //adding the string variables
                nameValuePairs.add(new BasicNameValuePair("reporter", reporter));
                nameValuePairs.add(new BasicNameValuePair("department", department));
                nameValuePairs.add(new BasicNameValuePair("errortype", errortype));
                nameValuePairs.add(new BasicNameValuePair("title", title));
                nameValuePairs.add(new BasicNameValuePair("description", description));
                nameValuePairs.add(new BasicNameValuePair("percaution", percaution));
                nameValuePairs.add(new BasicNameValuePair("imgurl", imgurl));
                nameValuePairs.add(new BasicNameValuePair("dataset", dataset));
                nameValuePairs.add(new BasicNameValuePair("image", imagedata));
                nameValuePairs.add(new BasicNameValuePair("phoneid", android_id));
                nameValuePairs.add(new BasicNameValuePair("typ", TAG_TYP));
                //Task to upload the information in background, on another thread
                new AsyncTask<ApiConnector, Long, Boolean>() {
                    @Override
                    protected Boolean doInBackground(ApiConnector... apiConnectors) {
                        return apiConnectors[0].uploadImageToserver(nameValuePairs);
                    }

                    @Override
                    protected void onPostExecute(Boolean result) {
                        //tell the user that it is registred
                        Toast.makeText(getBaseContext(), getString(R.string.registred), Toast.LENGTH_LONG).show();
                        pDialog.dismiss();
                        //change window to main
                        Intent mainIntent = new Intent(getBaseContext(), MainActivity.class);
                        startActivity(mainIntent);
                    }
                }.execute(new ApiConnector());
            } catch (Exception e) {
                Log.d("MainActivity", e.toString());
            }
        }
    } //Here ends onClick(View v)
});

@由于某种原因,onPostExecute上的Rihit5k2覆盖变为红色。有什么线索吗?方法不会从超类错误中重写方法。只需将
onPostExecute
的参数类型更改为
boolean
protectedvoid onPostExecute(boolean result),如下所示?还是不行?OnPostExecute是否应该有覆盖@Rohit5K2是,只需更改参数。它可以在没有覆盖的情况下与我们的系统一起工作。我个人选择保留它。嗨,你能给我看一下“encodeTobase64”吗?
@Override
protected void onPostExecute(Boolean result) {
    //tell the user that it is registred
    Toast.makeText(getBaseContext(), getString(R.string.registred), Toast.LENGTH_LONG).show();
    pDialog.dismiss();
    //change window to main
    Intent mainIntent = new Intent(getBaseContext(), MainActivity.class);
    startActivity(mainIntent);
}