문제

I am trying to do display a ProgressBar.

I am an Android beginner.

When I press the button, the task should be running in the background, but it does not display the ProgressBar.

What is problem? I cannot understand.

Please help me!

MainActivity:

package com.example.shikkok_services;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {


    private Button btnThread,btntask;
    private ProgressDialog pd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnThread=(Button)findViewById(R.id.btnStartThread);
        btntask=(Button)findViewById(R.id.btntsk);

        btntask.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {


                //start myTast

                new MyTask(MainActivity.this).execute();


            }
        });



    }

}

MyTask:

 package com.example.shikkok_services;

import android.app.Dialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MyTask extends AsyncTask<Void, Integer, Void> {

    Context context;
    Handler handler;
    Dialog dialog;
    TextView txtprogrss;
    ProgressBar progress;
    Button btnCancel;

    MyTask(Context context, Handler handler){
        this.context=context;
        this.handler=handler;

    }

    MyTask(Context context){
      this.context=context;
      this.handler=handler;
    }

    @Override
    protected void onPreExecute() {

        super.onPreExecute();
        // create dialog
        dialog=new Dialog(context);
        dialog.setCancelable(true);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.pogressdialog);
        txtprogrss=(TextView) dialog.findViewById(R.id.txtProgress);
        progress=(ProgressBar)dialog.findViewById(R.id.progressBar2);
        btnCancel=(Button)dialog.findViewById(R.id.btnProgress);

        btnCancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                MyTask.this.cancel(true);
            }
        });


    }


    @Override
    protected Void doInBackground(Void... arg0) {


        for (int i = 0; i < 100; i++) {
            if(isCancelled()){
            break;
            }else{
            Log.e("In Background","current value;"+ i);
            publishProgress(i);

            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            }

        }

        return null;    
    }

    @Override
    protected void onProgressUpdate(Integer... values) {


        super.onProgressUpdate(values);


        progress.setProgress(values[0]);
        txtprogrss.setText("progress update"+ values[0]+"%");

    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);


        dialog.dismiss();
        Toast.makeText(context, "Finished", Toast.LENGTH_LONG).show();

    }





}
도움이 되었습니까?

해결책 2

You are not calling dialog.show() in onPreExecute method of your AsyncTask.

다른 팁

You forgot to call dialog.show() at the onPreExecute()

Use ProgressDialog. You don't need any layout in this case.

ProgressDialog progressDialog = new ProgressDialog(context);

in onPreExecute show it

progressDialog.show();

and in onPostExecute dissmiss it

progressDialog.dismiss();

Add to OnPostExecute method :

pg.setVisibility(View.INVISIBLE);

Add to onPreExecute method :

pg.setVisibility(View.VISIBLE);

and in Layout file you should add to progress bar :

android:visibility="invisible" 

You created the dialog but not showing it anywhere. Your onPreExecute() should look like:

@Override
protected void onPreExecute() {

    super.onPreExecute();
    // create dialog
    dialog=new Dialog(context);
    dialog.setCancelable(true);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.pogressdialog);
    txtprogrss=(TextView) dialog.findViewById(R.id.txtProgress);
    progress=(ProgressBar)dialog.findViewById(R.id.progressBar2);
    btnCancel=(Button)dialog.findViewById(R.id.btnProgress);

    btnCancel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            MyTask.this.cancel(true);
            dialog.dismiss();  //On button click cancel AsyncTask and dismiss dialog
        }
    });
    dialog.show();  //Show the dialog
}

You also need to dismiss the dialog when clicked on btnCancel.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top