开发者

Visibility of ScrollView

I am using two ScrollViews and want to set visibility of one as GONE or INVISIBLE but when I do it by following java code the application terminates. If I do the same thing using Properties pan, in the eclipse, it just works. What is wrong?


Java code is:

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"&开发者_如何学Cgt;</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>


you haven't setContentview(yourLayout.xml)

it wont be able to findViewById.

do the findViewById AFTER the setContentView


First of you have to set layout file.

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

try this if you are getting any error then please post logcat.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜