1

I am New to Android, and have doing coding to satisfy my various needs.

I have a strange problem, in which i need to show Progress Dialog to cover up my background work of downloading images from Facebook and show them in a ListView after i get them in a Vector Object.

My Code Goes Like this :

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        facebook.authorize(this, new String[] { "email", "publish_checkins",
                "user_photos" },

        //new Dialog Listner code

        btnAlb = (Button) findViewById(R.id.btnAlb);
        logout = (Button) findViewById(R.id.logout);

        btnAlb.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {


    //**            Log.i("pDialog","Entry");
        pdialog = ProgressDialog.show(MainActivity.this, "Processing Data","Loading..");
    //this is not working !!
    //**            Log.i("pDialog","Exit");

                try {

    //Lot of JSON Parsing
                    pdialog.dismiss();

                    Intent intent = new Intent(MainActivity.this,AlbumHolder.class);

                    startActivity(intent);

                } catch (Exception e) {
                    e.printStackTrace();
                    pdialog.dismiss();
                }


            }
        });

}

So i have Logged the Start and End of ProgressDialog and expected the dialog to show up. but it didn't. can somebody explain why?

I have also used a runOnUiThread() Thread, but it also yielded the same result.

Nate-Wilkins
  • 5,364
  • 4
  • 46
  • 61
Sri Krishna
  • 302
  • 1
  • 2
  • 14
  • The output of the Two Consecutive Logs are Displayed, but the Progress Dialog is not. – Sri Krishna Oct 01 '12 at 13:02
  • Is your JSON parsing being done? If it isn't, that means the button's onClick() is not getting executed. – Vinay S Shenoy Oct 01 '12 at 13:03
  • My Log Shows As Follows : 10-01 18:36:37.772: DEBUG/Facebook-authorize(6544): Login Success! access_token=$$ expires=$$ 10-01 18:36:37.822: WARN/InputManagerService(59): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@627f5390 10-01 18:36:40.752: INFO/pDialog(6544): Entry 10-01 18:36:40.802: INFO/pDialog(6544): Exit 10-01 18:36:40.802: DEBUG/Facebook-Util(6544): GET URL: https://graph.facebook.com/me/albums?access_token=$$&format=json – Sri Krishna Oct 01 '12 at 13:05
  • Never mind.. If the Logs are showing, it means that the onClick() is executing. – Vinay S Shenoy Oct 01 '12 at 13:06
  • My JSON Parsing is 100% performing, as i have laden it with lots of Logs. Also i am able to go to the next activity and show my pictures. but the progress dialog is not showing itself. – Sri Krishna Oct 01 '12 at 13:06
  • Out of curiosity, could it be that your JSON parsing is being done fast enough that the ProgressDialog is being shown and instantly dismissed? Print a System.currentUptimeMillis() before doing ProgressDialog.show() and one more at the end of your JSON parsing and compare the two timestamps. – Vinay S Shenoy Oct 01 '12 at 13:11

2 Answers2

3

Your method of defining the progress dialog is not proper. Try like this

 pdialog =new ProgressDialog(mContext);
 pdialog.setMessage("Your message");
 pdialog.show();

here mContext is your context of the activity and use pdialog.dismiss(); to dismiss it

Antrromet
  • 15,294
  • 10
  • 60
  • 75
  • This doesn't work all the time. Many times, I have got null pointers when I try to use mContext or getApplicationContext(), but the progress dialog shows when I have used Activity.this instead. – Vinay S Shenoy Oct 01 '12 at 13:08
  • My Activity's name is MainActivity. so for the purpose of Context in the pDialog creation, i have used "MainActivity.this" and run the code. The Progress Dialog remains invisible. and as a second mesure, instead of using the above said context, i have created a Context class variable inside my MainActivity and passed it to the pDialog, Then i am getting a NullPointerException. Also i am using the pdialog.dismiss() method to dismiss my PD at the end of my JSON parsing work. Please Guide me on what is wrong with this situation. – Sri Krishna Oct 01 '12 at 13:20
  • pdialog =new ProgressDialog(MainActivity.this); should work in your case. Could you update your code, so that I can know what is wrong in it? – Antrromet Oct 01 '12 at 13:23
  • @VinaySShenoy you should see this link http://stackoverflow.com/questions/987072/using-application-context-everywhere – Antrromet Oct 01 '12 at 13:27
  • @ Antrromet I have tried all Possible answers, but no avail. The View from which the Dialog has to appear is somewhat going into an ANR State and hence is completely ignoring my ProgressDialog request, even though it is full-filling all the other processing statements. it simply cannot accept to pop up a Dialog. Any way to solve this problem is much appreciate. I know i can't use Threads to solve this problem, as we can't perform any UI changes in another Thread than main/UI Thread. SO please Help me with my predicament. – Sri Krishna Oct 04 '12 at 06:21
  • I Further Add these Facts : 1) I am using Heavy JSON Parsing. 2) Using HTTP Web Requests to Download Images From Facebook to be shown in my listview, in turn to be shown in a Gallery View. 3) The Progress Dialog is in the Stage of JSON Parsing performing Class. – Sri Krishna Oct 04 '12 at 06:21
  • You never mentioned that you were doing heavy parsing and getting huge data from internet. For these kinds of stuff, AsyncTask is the correct way to handle things, so that your app does not go into ANR. You will find tutorials on internet for AsyncTasks. http://www.vogella.com/articles/AndroidPerformance/article.html is one of them – Antrromet Oct 04 '12 at 06:48
  • @ Antrromet Thank you for the response, i will try it in those lines. – Sri Krishna Oct 05 '12 at 06:49
0

You should use pdialog.dismiss(); when your task completed.For this problem you should use AsyncTask class. More Information Read This Tut.

Rishabh Agrawal
  • 861
  • 2
  • 15
  • 25