views:

48

answers:

2

I am trying to create a very simple application (part of my learning program), vertically oriented, with an EditText at the top that 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

A: 

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.

Mayra
I'll work on adding a stacktrace in the next little while. Execution seems to go south long before there's a question even of choosing which of text or images I want to display. I didn't know about "setting to gone" so I'll check that out too. Thanks.
Russ Bateman
Oh, so, yeah, by "doesn't work", I mean a) if I just run it, it let's me type in a URL for text or image, but does nothing and b) if I run debug, I see the crash I just posted in my question above--relevant I think to the ImageView code (but I'm very new at this).
Russ Bateman
See edits...added more details about GONE too.
Mayra
I switched to using FrameLayout instead of ScrollView (but not instead of LinearLayout). I've adjusted my XML pseudo code better to reflect what I've been doing. I also added GONE. Please see my code in Download.java (and any and all other source files) at http://www.windofkeltia.com/xfer/android-download-app/ (And, by the way, thanks for this help!)
Russ Bateman
I believe fedj is pointing to your problem below. Learning to read stack traces and log output is a very important part of learning to code. In this case, it pointed you right to the offending line, where are you attempting to cast a view into a TextView that is not actually a TextView. It might be worth taking a little bit of time to read through the log, and make sure you understand what its saying. That should help you debug a problem like this next time!
Mayra
A: 

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.

fedj
I thought that was the point: one place for whichever of the two things I wanted to put there. Okay, cool, I'll revisit that and report back on my experience. Thank you very much!
Russ Bateman
Cool. It's much closer to working: I can get the text display. I'm not yet seeing any image I choose displaying, but I hope to work that out (I have a separate application that does work doing only the image side of what this one's trying to do). Thanks for your help! Once working, I'll probably publish this app as a short tutorial on my website (javahotchocolate.com).
Russ Bateman
Another thing, I don't understand the need of your framelayout (or your linearlayout), you only need one in the code I saw.Try to put the element to be displayed (either textview or imageview) with the setVisibility method (put textview visible while imageview gone and inversely).By the way, if your question was solved, mark the question as solved for other people
fedj
@fedj: I think it's more or less solved (in that it works), however, I'm not certain that I've done it the right way. In particular, this last comment makes me think I need to look closer. I'm trying to solve an HTTP issue right now and will come back to clean the present question up and I will re-post ('cause I'm like that since I appreciate when others do it). Thanks
Russ Bateman
Why did you put a LinearLayout in a FrameLayout ? This is useless because you have two layouts and you only need one.Anyway, if your question is solved, put it as solved.
fedj