在ViewSwitcher中切换不确定的ProgressBar和Image

时间:2010-07-30 18:29:18

标签: java android xml components

尝试使用viewSwitcher在ProgressBar和Image之间切换时收到以下错误:

07-30 18:08:00.627: ERROR/AndroidRuntime(307): Caused by: java.lang.NullPointerException
07-30 18:08:00.627: ERROR/AndroidRuntime(307):     at android.view.ViewGroup.addView(ViewGroup.java:1815)
07-30 18:08:00.627: ERROR/AndroidRuntime(307):     at android.view.ViewGroup.addView(ViewGroup.java:1802)
07-30 18:08:00.627: ERROR/AndroidRuntime(307):     at com.romcessed.romsearch.activities.RomDetailActivity$LoadImageViewsTask.intializeSwitchers(RomDetailActivity.java:161)

以下是导致错误的代码:

//Place indeterminate progress bars for our ImageViews until computation is complete
switcherTask = new LoadImageViewsTask();
switcherTask.intializeSwitchers();
switcherTask.execute((Void) null);

.
..
...

void intializeSwitchers(){
    captchaSwitcher = new ViewSwitcher(RomDetailActivity.this);
    gallerySwitcher = new ViewSwitcher(RomDetailActivity.this);

    /*Line that caused error */ 
    captchaSwitcher.addView((ProgressBar)findViewById(R.layout.indeter));
    /*                       */
    gallerySwitcher.addView(findViewById(R.layout.indeter));

    captchaSwitcher.addView(iv_Captcha);
    gallerySwitcher.addView(gal_Screenshots);
}

这是indeter.xml:

<?xml version="1.0" encoding="utf-8"?>
    <ProgressBar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/progressbar"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerVertical="true" />

1 个答案:

答案 0 :(得分:2)

基本上你需要使用Layout inflater来扩展视图,然后将其添加到Switcher,或者你也可以从代码创建一个新的视图。

请参阅此链接,了解基本布局inflater功能:

http://androblip.huiges.nl/2010/05/14/add-a-view-to-a-wrapper-multiple-times-with-inflate/