findViewById(android.R.id.progress) returns null
Asked Answered
U

4

6

The Android Developers reference lists R.id.progress among the built-in View resources available.

Yet, when I issue (in my activity class) the statement:

View pv = getWindow().findViewById(android.R.id.progress);

It returns null.

It returns null even when I use it in conjunction with the ProgressBar class:

ProgressBar pv = (ProgressBar) getWindow().findViewById(android.R.id.progress);

And it returns null even without the getWindow():

ProgressBar pv = (ProgressBar) findViewById(android.R.id.progress);

Any idea why?

To make sure I'm not hallucinating, I followed @Geobits's advice and created a new project from scratch, containing the exact code listed in the blog post recommended by @ArunGeorge below:

package com.droidworks;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ProgressBar;

public class ProgressBarExampleActivity extends Activity {
  private ProgressBar mProgress;
  private MyThread mBgThread;
  private final Handler mHandler = new Handler();

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mProgress = (ProgressBar) findViewById(android.R.id.progress);

    mBgThread = (MyThread) getLastNonConfigurationInstance();

    if (mBgThread == null) {
      mBgThread = new MyThread();
      mBgThread.start();
    }

    mHandler.post(mThreadWatcher);
  }

  @Override
  public Object onRetainNonConfigurationInstance() {
    return mBgThread;
  }

  @Override
  protected void onPause() {
    mHandler.removeCallbacks(mThreadWatcher);
    super.onPause();
  }

  private Runnable mThreadWatcher = new Runnable() {
      public void run() {
        int progress = mBgThread.getProgress();
        mProgress.setProgress(progress);

        if (progress != 100)
          mHandler.postDelayed(this, 50);
      }
    };

  static class MyThread extends Thread {

    private int _progress = 0;

    public void run() {
      for (; _progress < 100; _progress++) {
        try {
          Thread.sleep(100);
        }
        catch (InterruptedException ignored) {
        }
      }
    }

    private int getProgress() {
      return _progress;
    }
  }

}

Sure enough, this code throws a NullPointerException:

at com.droidworks.ProgressBarExampleActivity$1.run(ProgressBarExampleActivity.java:44)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Undertook answered 2/7, 2012 at 19:13 Comment(7)
What version of Android?Debra
because you don't have a view by that ID showing on the screen.Gamy
@user1260028 Froyo (2.2) @Tim What do you mean by "not showing"? Does it have to be visible at the time of that findViewById() call?Undertook
Visibility does not affect that. I think Tim just wanted to say that this view is simply not there. Don't you have to request the progress bar as a window feature first? And then it is unlikely that you can find it in onCreate, because layouting didn't happen, yet. You could add an OnGlobalLayoutListener for the right moment. Or maybe just post a delayed method on the UI thread for finding that viewSalomone
@NobuGames I can confirm that visibility does not affect that as I just tested it with the progress bar visible. I am issuing that findViewById() long after the entire UI has been inflated (not in onCreate()). So there must be another reason.Undertook
Post some more code from prior to that call. One line doesn't tell the whole story here.Jag
@Jag See update in my original post. +1.Undertook
B
1

The R.id.progress built-in tag is designed for the ProgressBar class. If you do not have a ProgressBar object or a ProgressBar in your XML, then it will return null.

Bidentate answered 2/7, 2012 at 19:19 Comment(6)
@ack57 I can see the ProgressBar with my eyes... It's true, though, that there isn't any in my XML. It's the system's built-in ProgressBar. Isn't that the entire purpose of those android.R.id resources? They don't need to be in my XML.Undertook
No, you must have a programmatical connection to the ProgressBar. Simply entering R.id.progress will not fetch the most convenient ProgressBar for you.Bidentate
What is "programmatical connection to the ProgressBar"?Undertook
A better way to word that is Scope. The progress bar you see does not have global fields ready for you to use.Bidentate
OK thanks. So why does it work for this blogger and the same exact code doesn't work for me? (see update above).Undertook
Either he never compiled that, or he had an xml file that he did not post.Bidentate
J
0

EDITED

Sorry, I must have counted it up wrong. that's what I get for not pasting. Either way, is there a reason you don't just use one of ProgressBar's constructors? Here's the line from the latest project I used a bar in:

mProgress = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
Jag answered 2/7, 2012 at 20:22 Comment(6)
The code posted is an exact copy & paste from my own ProgressBarExampleActivity.java class producing that exception. The null pointer exception is generated at line 44: mProgress.setProgress(progress);. The null is for mProgress. Same problem... Ideas? Thanks.Undertook
To verify that, I check for if (mProgress == null) right after the findViewById() in onCreate() and issue an error message in LogCat. It's null. I am looking at the right place. Feel free to copy this code verbatim and try it on your system. In eclipse it only takes 2 minutes to create a new project from scratch containing the same exact code.Undertook
Thanks for the correction. Yes, there is a reason why I'm not using a constructor or even an XML layout: I want the access the same exact ProgressBar that's displayed by default by the system on the TitleBar - without using a FEATURE_CUSTOM_TITLE that is. In other words, I want to access the same exact ProgressBar that's accessed by Activity.setProgress(). Any idea how to do that?Undertook
Oh, didn't know it was going in a title bar. In that case, you might want to check out this: pilhuhn.blogspot.com/2010/10/… . Two things of note: The order of calls here is important, and I think you're looking for the android:attr/progressBarStyleSmallTitle. You can still set it in XML either way.Jag
Yup. the link you provided points to a FEATURE_CUSTOM_TITLE implementation, which isn't what I want because, by design, Android does not allow combining FEATURE_CUSTOM_TITLE with other features (which I need). Back to square one. :-(Undertook
Right. Maybe I'm just confused. I thought the only reason you couldn't use FEATURE_CUSTOM_TITLE as because it didn't allow a progressbar with it. The link shows how to use both, using the default title progress bar theme(which you could customize in xml). The key is not requesting FEATURE_PROGRESS because the bar is in the custom title layout. If there's another reason you don't want FEATURE_CUSTOM_TITLE, I could understand, but you -can- do both with the method shown, as it's the whole point of the post.Jag
M
-1

android.R.id.progress is used in conjunction with the ProgressBar.

Check this link: http://foo.jasonhudgins.com/2010/03/simple-progressbar-tutorial.html

your usage is thus wrong.

Mcghee answered 2/7, 2012 at 19:20 Comment(2)
What do you mean by "your usage is thus wrong"? It returns null even when I use the ProgressBar class: ProgressBar pv = (ProgressBar) getWindow().findViewById(android.R.id.progress);. Please explain.Undertook
why don't you simply use findViewById(android.R.id.progress) without appending it with the getWindow() method?Mcghee
C
-1

From Eclipse, perform a project clean by clicking on 'Project' > 'Clean...', click on the checkbox beside your project, and click 'Ok'. And try building your project again.

Coptic answered 3/7, 2012 at 9:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.