android - 创建了一个空进度条?

标签 android xml progress-bar

当我运行下面的代码时,它会在日志文件中打印“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" />

最佳答案

带有ProgressBarlayout 必须先膨胀,您可能需要使用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/

相关文章:

html - 如何制作带链接的进度条?

android - 在android studio中添加库?

java - 无法将 Google Analytics v4 转换为 Analytics

android - 对话框关闭时是否取消了 doInBackground?

xml - 是否允许在 XSD 中有多个同名元素?

javascript - 带有 axios 的进度条

python - 为什么 ttk Progressbar 在 Tkinter 处理后出现

android - 如何在 Eclipse 中安装较旧的 Android SDK 平台工具 23.0.1?

xml - 在 XSLT 中按月份和年份对 XML 节点进行分组

html - 什么是站点地图.xml