Android Studio MainActivity.java:: error: ';' expected

Haider T picture Haider T · Mar 8, 2016 · Viewed 8.3k times · Source

I am new to Android development, When Android Studio in "MainActivity" class is compiled I get the following error

I have pasted the error log below:

:\Users\kanak\AndroidStudioProjects\SelfDistructText\app\src\main\java\com\example\kanak\selfdistructtext\MainActivity.java:61: error: ';' expected
        public boolean onCreateOptionsMenu(Menu menu){
                                          ^
C:\Users\kanak\AndroidStudioProjects\SelfDistructText\app\src\main\java\com\example\kanak\selfdistructtext\MainActivity.java:61: error: ';' expected
        public boolean onCreateOptionsMenu(Menu menu){
                                                    ^
C:\Users\kanak\AndroidStudioProjects\SelfDistructText\app\src\main\java\com\example\kanak\selfdistructtext\MainActivity.java:69: error: ';' expected
        public boolean onOptionsItemSelected (MenuItem item){
                                            ^
C:\Users\kanak\AndroidStudioProjects\SelfDistructText\app\src\main\java\com\example\kanak\selfdistructtext\MainActivity.java:69: error: ';' expected
        public boolean onOptionsItemSelected (MenuItem item){

I have pasted my MainActivity Class code below:

enter code here

package com.example.kanak.selfdistructtext;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.app.ActionBar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import com.parse.Parse;
import com.parse.ParseAnalytics;
import com.parse.ParseObject;
import com.parse.ParseUser;


public class MainActivity extends AppCompatActivity{`enter code here`

    public static final String TAG = MainActivity.class.getSimpleName();


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

        // Parase Analytice

        ParseAnalytics.trackAppOpenedInBackground(getIntent());
        ParseUser currentUser = ParseUser.getCurrentUser();
        if (currentUser == null) {
            Intent intent = new Intent(this, LoginActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        } else {

            Log.i(TAG, currentUser.getUsername());
        }


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


        @Override
        public boolean onCreateOptionsMenu(Menu menu){
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);

            return true;
        }

        @Override
        **public boolean onOptionsItemSelected (MenuItem item){**
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }
    }
}

Answer

Android Geek picture Android Geek · Mar 8, 2016

Replace your code with the following code:

public class MainActivity extends AppCompatActivity{

    public static final String TAG = MainActivity.class.getSimpleName();

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

            // Parase Analytice

            ParseAnalytics.trackAppOpenedInBackground(getIntent());
            ParseUser currentUser = ParseUser.getCurrentUser();
            if (currentUser == null) {
                Intent intent = new Intent(this, LoginActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
            } else {

                Log.i(TAG, currentUser.getUsername());
            }


            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });

    }
            @Override
            public boolean onCreateOptionsMenu(Menu menu){
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.menu_main, menu);

                return true;
            }

            @Override
          public boolean onOptionsItemSelected (MenuItem item){
                // Handle action bar item clicks here. The action bar will
                // automatically handle clicks on the Home/Up button, so long
                // as you specify a parent activity in AndroidManifest.xml.
                int id = item.getItemId();

                //noinspection SimplifiableIfStatement
                if (id == R.id.action_settings) {
                    return true;
                }

                return super.onOptionsItemSelected(item);
            }
        }
}