开发者

Android new ProgressBar without NullPointerException?

Whenever I try to construct a ProgressBar, it gives NullPointerException. The examples on the net say the second parameter can be null, even though it is supposed to be AttributeSet? Could that be part of the problem? This is compiling for 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;
}


The NPE is being caused by the null AttributeSet parameter that you're passing to the constructor.

You could try using a different Constructor, and styling your control using a theme:

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

then define a theme in 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>

This is basically over-riding the default style of the ProgressBar when MyTheme is applied to it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜