aspose file tools*
The moose likes Android and the fly likes EditText force closing with getText().toString() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "EditText force closing with getText().toString()" Watch "EditText force closing with getText().toString()" New topic
Author

EditText force closing with getText().toString()

dani alex
Greenhorn

Joined: Oct 14, 2011
Posts: 6
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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Oct 14, 2011
Posts: 6
How can I test if it is null if getText() causes the force close?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Apr 01, 2009
Posts: 153

Hey Dani can you post the complete code

-zoheb
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Apr 01, 2009
Posts: 153

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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Jun 21, 2010
Posts: 1
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

Joined: Apr 01, 2009
Posts: 153

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

Joined: Oct 14, 2011
Posts: 6
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: EditText force closing with getText().toString()