Cole
Cole

Reputation: 2815

Simple error on simple code, but I don't get it?

I'm getting this error:

Syntax error on token ";", , expected

On this very simple code:

TextView bio = (TextView)findViewById(R.id.biographyText);

    String bioText = new String();

    bioText = bio.getText().toString();

The error is on the semicolon after "new String()"

What's wrong with this? In the end, if someone clicks on the TextView, it'll change the text to whatever they enter.

Here's the whole Java class:

public class Profile extends Activity{


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    }


TextView bio = (TextView)findViewById(R.id.biographyText);

String bioText = new String();

bioText = bio.getText().toString();


}

Upvotes: 0

Views: 162

Answers (1)

natewelch_
natewelch_

Reputation: 254

Skip to the last code block for the simple answer

You shouldn't instantiate bioText like that, get rid of

new String()

and just make it

String bioText ="";
bioText = bio.getText().toString();

You should never have to instantiate a String by hand, you could instantiate it as "" if you want, but it's not needed. However, to solve your problem, it's simple. The following code must be in a method

TextView bio = (TextView)findViewById(R.id.biographyText);
String bioText = "";
bioText = bio.getText().toString();

However, this still wont work, seeing as you never call setContentView() in onCreate(). The main problem is the line

bioText = bio.getText().toString();

You can't instantiate an object, other than where it is declared, outside of a method. To fix this, simple make it

String bioText = bio.getText().toString();

Upvotes: 2

Related Questions