Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EditText force closing with getText().toString()

 
dani alex
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have searched and tried all the solutions....and nothing seems to work. help please!!

here is the offending snippet, pulled out of the rest of my code. any way i try to use


creates a force close. If I comment out that line, it works fine.



update: Any form of getText() or setText() that I call within the void onClick causes a force close
 
Tim Moores
Bartender
Posts: 2789
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by examining the logcat output to figure out the actual exception that is being thrown. Have you made sure that "ET" is not null?
 
dani alex
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I test if it is null if getText() causes the force close?
 
Tim Moores
Bartender
Posts: 2789
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't do that, then :-) Output the value of ET before calling any method on it.

But this is just shooting in the dark - you really need to take a look at the logcat output to see what the exception is.
 
zoheb hassan
Ranch Hand
Posts: 154
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Dani can you post the complete code

-zoheb
 
Tim Moores
Bartender
Posts: 2789
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
zoheb hassan wrote:Hey Dani can you post the complete code

What for? It's pretty clear what's happening, is it not?
 
zoheb hassan
Ranch Hand
Posts: 154
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:
zoheb hassan wrote:Hey Dani can you post the complete code

What for? It's pretty clear what's happening, is it not?


No Tim its not clear whats happening or going wrong over here, would you care to explain it

Thanks
Zoheb
 
Tim Moores
Bartender
Posts: 2789
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's happening is that there's an exception, all the details of which can be found in the logcat output. So the next step is for dani alex to examine the logcat output, and -if further help is needed- post it here. For us to see the rest of the code doesn't help with that.
 
vikshep singh
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you are trying to do here. Just remove the line finish(); under the OnClickListener and the code should run.
You are forcing the activity to close by calling finish();.

Button sB;
EditText ET;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.writestory);

ET = (EditText)findViewById(R.id.story);

sB = (Button)findViewById(R.id.sb);

sB.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Log.v("EditText", ET.getText().toString());
//finish(); Remove this line
}
});
 
zoheb hassan
Ranch Hand
Posts: 154
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you call finish(), you will be exiting the activity not force closing the activity.So i don't think the finish() method is the culprit here. We cannot comment on whats wrong until he posts the complete code or puts himself what was wrong.Let's wait for him to respond

-Zoheb
 
dani alex
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. So I fixed it. Not quite sure why this worked though.

If I put the editString = getText().toString() inside the onFocusChange for the edit text field, instead of the onclick of the button it works fine. Then I just have the button do it's own thing with the editString.

Thanks for the replys!

I am new at Android and just learning how to work with it. The debug and exception systems make sense now. It is actually quite lovely.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic