在2个活动之间显示进度条

3

我有两个活动,一个叫做MainActivity,另一个叫做Circle。我想在点击MainActivity上的按钮以启动第二个活动时出现进度条加载屏幕。这是我目前拥有的代码,但它只会导致应用程序崩溃。

public class LoadingScreenActivity extends Activity 
{
//A ProgressDialog object
private ProgressDialog progressDialog;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    //Initialize a LoadViewTask object and call the execute() method
    new LoadViewTask().execute();       

}

//To use the AsyncTask, it must be subclassed
private class LoadViewTask extends AsyncTask<Void, Integer, Void>
{
    //Before running code in the separate thread
    @Override
    protected void onPreExecute() 
    {
        //Create a new progress dialog
        progressDialog = new ProgressDialog(LoadingScreenActivity.this);
        //Set the progress dialog to display a horizontal progress bar 
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        //Set the dialog title to 'Loading...'
        progressDialog.setTitle("Loading...");
        //Set the dialog message to 'Loading application View, please wait...'
        progressDialog.setMessage("Loading application View, please wait...");
        //This dialog can't be canceled by pressing the back key
        progressDialog.setCancelable(false);
        //This dialog isn't indeterminate
        progressDialog.setIndeterminate(false);
        //The maximum number of items is 100
        progressDialog.setMax(100);
        //Set the current progress to zero
        progressDialog.setProgress(0);
        //Display the progress dialog
        progressDialog.show();
    }

    //The code to be executed in a background thread.
    @Override
    protected Void doInBackground(Void... params) 
    {
        /* This is just a code that delays the thread execution 4 times, 
         * during 850 milliseconds and updates the current progress. This 
         * is where the code that is going to be executed on a background
         * thread must be placed. 
         */
        try 
        {
            //Get the current thread's token
            synchronized (this) 
            {
                //Initialize an integer (that will act as a counter) to zero
                int counter = 0;
                //While the counter is smaller than four
                while(counter <= 4)
                {
                    //Wait 850 milliseconds
                    this.wait(850);
                    //Increment the counter 
                    counter++;
                    //Set the current progress. 
                    //This value is going to be passed to the onProgressUpdate() method.
                    publishProgress(counter*25);
                }
            }
        } 
        catch (InterruptedException e) 
        {
            e.printStackTrace();
        }
        return null;
    }

    //Update the progress
    @Override
    protected void onProgressUpdate(Integer... values) 
    {
        //set the current progress of the progress dialog
        progressDialog.setProgress(values[0]);
    }

    //after executing the code in the thread
    @Override
    protected void onPostExecute(Void result) 
    {
        //close the progress dialog
        progressDialog.dismiss();
        //initialize the View
        setContentView(R.layout.content_circle);
    }   
}

}


也请添加您的Logcat日志。您是从哪里调用第二个Activity的? - Prerak Sola
2个回答

2

您可以在第二个活动的XML中添加一些默认视图,例如进度条,默认情况下可见。当您加载数据或进行任何操作时,请将其设置为view.GONE。像这样的好库:

https://github.com/81813780/AVLoadingIndicatorView

在您的second_activity.xml文件中使用:

 <com.wang.avi.AVLoadingIndicatorView
        android:layout_gravity="center"
        android:id="@+id/avloadingIndicatorView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible"
        app:indicatorName="BallPulse"
        app:indicatorColor="@color/myPrimaryColor"/>

然后在您的Activity的onCreate()方法中:

  public void onCreate(Bundle savedInstanceState) {            
            super.onCreate(savedInstanceState);
loader= (AVLoadingIndicatorView) findViewById(R.id.avloadingIndicatorView);
    }

最后,在加载完成后,只需使用以下代码:
 loader.setVisibility(View.GONE);

1
首先创建一个通用类Utility,以便重用ProgressDialog的代码。
public class Utility {
    public static ProgressDialog getProgressDialog(Context context) {
        ProgressDialog progressDialog = new ProgressDialog(context,
                R.style.TransparentDialog);
        progressDialog.setCancelable(false);
        progressDialog
                .setProgressStyle(android.R.style.Widget_ProgressBar_Small);
        progressDialog.setProgress(0);
        return progressDialog;
    }

}

然后在您的活动或片段中使用上述类。但是,您必须使用Intent来进入下一个活动。您不能直接设置下一个活动的布局。

public class LoadingScreenActivity extends Activity 
{
//A ProgressDialog object
protected ProgressDialog dialog;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    //Initialize a LoadViewTask object and call the execute() method
    new LoadViewTask().execute();       

}

//To use the AsyncTask, it must be subclassed
private class LoadViewTask extends AsyncTask<Void, Integer, Void>
{
    //Before running code in the separate thread
    @Override
    protected void onPreExecute() 
    {
            super.onPreExecute();
            dialog = Utility.getProgressDialog(context);
            dialog.setCanceledOnTouchOutside(false);
            dialog.setCancelable(false);
            if (dialog != null) {
                dialog.show();
            }

    }

    //The code to be executed in a background thread.
    @Override
    protected Void doInBackground(Void... params) 
    {
        /* This is just a code that delays the thread execution 4 times, 
         * during 850 milliseconds and updates the current progress. This 
         * is where the code that is going to be executed on a background
         * thread must be placed. 
         */
        try 
        {
            //Get the current thread's token
            synchronized (this) 
            {
                //Initialize an integer (that will act as a counter) to zero
                int counter = 0;
                //While the counter is smaller than four
                while(counter <= 4)
                {
                    //Wait 850 milliseconds
                    this.wait(850);
                    //Increment the counter 
                    counter++;
                    //Set the current progress. 
                    //This value is going to be passed to the onProgressUpdate() method.
                    publishProgress(counter*25);
                }
            }
        } 
        catch (InterruptedException e) 
        {
            e.printStackTrace();
        }
        return null;
    }



    //after executing the code in the thread
    @Override
    protected void onPostExecute(Void result) 
    {
        //close the progress dialog
         dialog.dismiss();
        // use intent here to go next activity
        Intent i = new Intent(this,SecondActivity.class);
        startIntent(i);

    }   
}

请提供TransparentDialog XML代码的样式细节。 - Dhruv Narayan Singh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接