ScrollView的可见性

| 我正在使用两个ScrollViews,并希望将其中一个的可见性设置为GONE或INVISIBLE,但是当我通过遵循Java代码执行此操作时,应用程序终止。如果我使用“属性”面板执行相同的操作,则在Eclipse中就可以使用。怎么了?                            Java代码是:
public class Test extends Activity {

    List<ScrollView> sv;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        sv = new ArrayList<ScrollView>();

        sv.add((ScrollView)findViewById(R.id.scrollView1));
        sv.add((ScrollView)findViewById(R.id.scrollView2));

        sv.get(1).setVisibility(View.GONE);

        setContentView(R.layout.main);
    }
}
                           main.xml
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
    android:orientation=\"vertical\"
    android:layout_width=\"fill_parent\"
    android:layout_height=\"fill_parent\"
    android:gravity=\"fill_vertical\">
    <ScrollView android:layout_height=\"wrap_content\" android:id=\"@+id/scrollView1\" android:layout_width=\"match_parent\">
        <LinearLayout android:layout_height=\"wrap_content\" android:layout_width=\"match_parent\" android:id=\"@+id/linearLayout1\" android:orientation=\"vertical\">
            <TextView android:text=\"TextView\" android:id=\"@+id/textView1\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\"></TextView>
        </LinearLayout>
    </ScrollView>
    <ScrollView android:layout_height=\"wrap_content\" android:layout_width=\"match_parent\" android:id=\"@+id/scrollView2\">
        <TextView android:text=\"TextView\" android:layout_width=\"match_parent\" android:id=\"@+id/textView2\" android:layout_height=\"match_parent\"></TextView>
    </ScrollView>
</LinearLayout>
    
已邀请:
您还没有setContentview(yourLayout.xml) 它将无法找到ViewById。 在setContentView之后执行findViewById     
首先必须设置布局文件。
setContentView(R.layout.new_view);
sv = new ArrayList<ScrollView>();

sv.add((ScrollView)findViewById(R.id.scrollView1));
sv.add((ScrollView)findViewById(R.id.scrollView2));

sv.get(1).setVisibility(View.GONE);
如果遇到任何错误,请尝试此操作,然后发布logcat。     

要回复问题请先登录注册