Error Message on Main Activity: 'illegal start

2019-09-02 20:31发布

问题:

I'm trying to build a simple application where if a user inputs a text message and clicks a button, the message shows on the next view.

It used to work fine, but then I added some lines of code, and deleted it back, and now it does not work again as it used to.

The complier won't run my code, and keep saying 'illegal start of expression' and '; expected'.

I'd appreciate if anyone could help me out here. Here's my code:

public class MainActivity extends ActionBarActivity {
    public final static String EXTRA_MESSAGE = "com.yhmac.myapplication3.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        /** Called when the user clicks the Send button */

    public void sendMessage(View view) {
        // Do something in response to button
        Intent intent = new Intent(this, IntroToApp.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }
}

回答1:

Please close your onCreate method.

public class MainActivity extends ActionBarActivity {
    public final static String EXTRA_MESSAGE = "com.yhmac.myapplication3.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }



        /** Called when the user clicks the Send button */

    public void sendMessage(View view) {
        // Do something in response to button
        Intent intent = new Intent(this, IntroToApp.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }
   }


回答2:

It should be like this.

public class MainActivity extends ActionBarActivity {
    public final static String EXTRA_MESSAGE = "com.yhmac.myapplication3.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    /** Called when the user clicks the Send button */

    public void sendMessage(View view) {
        // Do something in response to button
        Intent intent = new Intent(this, IntroToApp.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }
}


回答3:

Try this. Hope it will work.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
}    

/** Called when the user clicks the Send button */

public void sendMessage(View view) {
    // Do something in response to button
    Intent intent = new Intent(this, IntroToApp.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}