findViewById(android.R.id.progress)返回null [英] findViewById(android.R.id.progress) returns null
问题描述
Android开发者参考列出了内置的 R.id.progress 查看可用资源.
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);
它返回null.
即使我将它与ProgressBar
类结合使用,它也会返回null:
It returns null even when I use it in conjunction with the ProgressBar
class:
ProgressBar pv = (ProgressBar) getWindow().findViewById(android.R.id.progress);
即使在没有getWindow()的情况下,它也返回null:
And it returns null even without the getWindow():
ProgressBar pv = (ProgressBar) findViewById(android.R.id.progress);
知道为什么吗?
为确保我不会产生幻觉,我遵循@Geobits的建议,从头开始创建了一个新项目,其中包含
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;
}
}
}
当然,此代码抛出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)
推荐答案
R.id.progress
内置标记是为ProgressBar
类设计的.如果您的XML中没有ProgressBar
对象或ProgressBar
,则它将返回null.
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.
这篇关于findViewById(android.R.id.progress)返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!