2012-04-28 34 views
0

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

尝试运行Project>从Eclipse清理或从命令行中清除ant clean,然后再次尝试运行您的应用程序。 – CommonsWare 2012-04-28 23:16:37

+0

您之前是否调用过setContentView? – MByD 2012-04-28 23:57:51

+0

谢谢Binyamin,解决了它,你可以把你的评论作为答案,让我接受它吗? – callback 2012-04-29 01:34:36

回答

2

layoutProgressBar在它已先膨胀,你可能需要使用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,而不是我的ActivityfindViewById(),并root具有其中包括一个LinearLayout其中ProgressBar是一个元素充气视图。

0

改变风格,以这样的:

style="@android:style/Widget.ProgressBar.Horizontal"