2011-05-19 72 views
3

每当我尝试构建ProgressBar时,它都会给出NullPointerException。网上的例子说第二个参数可以是null,即使它应该是AttributeSet?这可能是问题的一部分吗?这是针对Android 1.5编译的。Android新的ProgressBar没有NullPointerException?

public class myListAdapter implements ListAdapter { 

... 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    Log.d(TAG,"getView"); 
    LinearLayout view = new LinearLayout(context); 
    //This new ProgressBar causes N.P.E.: 
    ProgressBar p = new ProgressBar(context, null,android.R.attr.progressBarStyleSmall); ; 
    view.addView(p); 
    return view; 
} 

回答

0

NPE是由传递给构造函数的null AttributeSet参数引起的。

你可以尝试使用不同的构造函数,并使用一个主题样式的控制:

ProgressBar p = new ProgressBar(new ContextThemeWrapper(context, R.style.MyTheme); 

然后定义在res /价值观主题/的themes.xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="MyTheme" parent="@android:style/Theme"> 
     <item name="android:progressBarStyle">@android:style/Widget.ProgressBar.Small</item> 
    </style> 
</resources> 

这基本上是当MyTheme应用于ProgressBar时,会覆盖ProgressBar的默认样式。

+0

我发现有什么问题,我用新的myListAdapter(ArrayListVariable)而不是正确的myListAdapter(this.getApplicationContext(),ArrayListVariable)调用它。 – anonymous 2011-05-20 03:33:33