Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ProgressDialog context problems

Quick summary: I'm making an application that parses a binary file, stores vertices and their properties, and displays them in openGL. I'm trying to implement a ProgressDialog while it parses, but I'm having considerable trouble. I've tried implementing this in many places, but this is my current setup:

    public class PointViewer extends Activity{
     ...
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            Bundle extras = getIntent().getExtras();
            filePath = extras.getString("file_path");

            mGLView = new GLSurfaceView(this);
            theRenderer = new OpenGLRenderer();
            mGLView.setRenderer(theRenderer);
            //Parse the file and set the arrays
            theRenderer.setLAS(filePath);
            setContentView(mGLView);

        }
     ...
    }

The Rendering class...

public class OpenGLRenderer extends Activity implements GLSurfaceView.Renderer {
         ...
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

            }

            public void setLAS (String fileName){

                new ProgressTask(this).execute();


            }
        ...
        /*
         * The class for the progress dialog 
        */
        private class ProgressTask extends AsyncTask<String, Void, Boolean> {
           private ProgressDialog dialog;
           private Context context;

           //private List<Message> messages;
           public ProgressTask(Context ctx) {

               context = ctx;
               dialog = new ProgressDialog(context);
           }



           /** progress dialog to show user that the backup is processing. */

           protected void onPreExecute() {
               this.dialog.setMessage("Progress start");
               this.dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
               this.dialog.show();
           }

            @Override
           protected void onPostExecute(final Boolean success) {


               if (dialog.isShowing()) {
                   dialog.dismiss();
               }

               if (success) {
                   Toast.makeText(context, "OK", Toast.LENGTH_LONG).show();
               } else {
                   Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();
               }
           }

           protected Boolean doInBackground(final String... args) {
               try{    
                ptCloud = new PointCloud(args[0]);

                    ...

                   dialog.setProgress(percentParsed);
                   return true;
                } catch (Exception e){
                   Log.e("tag", "error", e);
                   return false;
                }
             }


}

When I call dialog = new ProgressDialog(context); It errors on a null pointer exception, I'm assuming because there is a context issue... Does anyone have any ideas?

like image 856
RedLeader Avatar asked Feb 03 '26 14:02

RedLeader


1 Answers

First, you shouldn't create OpenGLRenderer yourself, because its an Activity and is supposed to be managed by system. When you create OpenGLRenderer yourself, then this activity has its context incorrectly initialized. And when your create ProgressDialog with invalid context, you receive NullPointerException.


But even if you start OpenGlRenderer correctly, your app will still crash at line:

dialog.setProgress(percentParsed);

You should use publishProgress instead and update ProgressDialog in AsyncTask's onProgressUpdate function. That's because you can't update UI from non-ui thread.

like image 56
inazaruk Avatar answered Feb 06 '26 04:02

inazaruk