当我运行下面的代码时,它会在日志文件中打印“null”……它有什么问题?
ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressBar1);
if (progressBar == null) {
Log.d("tag", "null");
}
xml调用是:
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="37dp"
android:layout_y="343dp" />
最佳答案
带有ProgressBar
的layout
必须先膨胀,您可能需要使用View.findViewById()
Activity.findViewById()
。今天早上我确实遇到了这个问题,我的解决方法在这里:
LayoutInflater infl = uI.getLayoutInflater();
ViewGroup rootGroup = (ViewGroup) uI.findViewById(R.id.voltoast_root);
View root = infl.inflate(R.layout.volumetoast, rootGroup);
toast.setView(root);
pB = (ProgressBar) root.findViewById(R.id.progress);
我在 root
而不是在我的 Activity
上调用 findViewById()
,并且 root
有包含 LinearLayout
的膨胀 View ,其中 ProgressBar
是一个元素。
关于android - 创建了一个空进度条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10368372/