Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

findViewById(android.R.id.progress) returns null

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)
like image 262
ateiob Avatar asked Jul 02 '12 19:07

ateiob


1 Answers

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.

like image 92
Jack Satriano Avatar answered Oct 09 '22 15:10

Jack Satriano