没有NullPointerException的Android新ProgressBar?

没有NullPointerException的Android新ProgressBar?,android,progress-bar,nullpointerexception,Android,Progress Bar,Nullpointerexception,每当我尝试构造ProgressBar时,它都会给出NullPointerException。网络上的例子说,第二个参数可以为null,即使它应该是AttributeSet?这可能是问题的一部分吗?这是为Android 1.5编译的 public class myListAdapter implements ListAdapter { ... @Override public View getView(int position, View convertView, ViewGroup pare

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

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;
}

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

您可以尝试使用其他构造函数,并使用主题设置控件的样式:

ProgressBar p = new ProgressBar( new ContextThemeWrapper( context, R.style.MyTheme );
然后在res/values/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>

@android:style/Widget.ProgressBar.Small

当MyTheme应用于ProgressBar时,它基本上超越了ProgressBar的默认样式。

我发现了问题所在,我用新的myListAdapter(ArrayListVariable)而不是正确的myListAdapter(This.getApplicationContext(),ArrayListVariable)调用它。