开发者

Two children for ScrollView?

开发者 https://www.devze.com 2023-01-11 01:29 出处:网络
I am trying to create a very simple application (part of my learning program), vertically oriented, with an EditText at the top th开发者_JS百科at receives a URL, and two buttons side by side just belo

I am trying to create a very simple application (part of my learning program), vertically oriented, with an EditText at the top th开发者_JS百科at receives a URL, and two buttons side by side just below that say "Get Image" or "Get Text" depending on the URL typed in (.html vs. .png, for example). The resulting elements may be later scavanged for use in a real app.

I know that ScrollView can have only one child. Conceptually, I want the rest of the screen to be a vertically oriented content region for displaying either the text (TextView) of the URL and "Get Text" was clicked or the image (ImageView) in the case where it was an image and "Get Image" was clicked.

I tried to do this (pseudocode) in main.xml:

<LinearLayout>
  <EditText />         --where to type the URL
  <Button />           --click to treat URL as text
  <Button />           --click to treat URL as image
  <FrameLayout>        --(used to be ScrollView)
    <LinearLayout>
      <TextView />     --content region occupied by either text
      <ImageView />    --or an image (but not both)
    </LinearLayout>
  </FrameLayout>
</LinearLayout>

in order not to violate the single-child requirement. However, this doesn't work and for now I'm not finding any sample out there that clues me in to how best to accomplish this. The last complaint I got while debugging was:

Caused by: java.lang.ClassCastException: android.widget.TextView

If anyone has the time and patience to steer me in some useful direction, I'd greatly appreciate it.

Thanks,

Russ Bateman


You have two elements in your xml which have the same id, this just cannot work, it's an id.

final TextView  textView   = ( TextView ) findViewById( R.id.content );
final ImageView imageView  = ( ImageView )findViewById( R.id.content );

You have to differentiate them like textcontent and imagecontent.


What do you mean by "doesn't work"?

Can you provide a full stacktrace/ logcat output for the exception?

I don't see anything wrong with your sketched out xml. I assume you set the visiblity of one or the other to GONE, depending on what you want to display?

Since you want the TextView and ImageView to be on top of each other, you might look at using a FrameLayout instead of the LinearLayout.

Edit: Your logcat says that you have a ClassCastException on line 33 of your Download class. This implies that you are trying to cast something that isn't a TextView into a TextView. Perhaps you are grabbing the wrong UI element? Without seeing the code its hard to say.

0

精彩评论

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