• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps braces problem that i just cannot resolve

Guys,

could one of you chaps please advise on braces positioning in the following code?

Java:
    @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.
        switch (item.getItemId()) {
            case R.id.action_settings:
        }
        //launch the popup!
        layoutinflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
        ViewGroup container = (ViewGroup) layoutinflater.inflate(R.layout.popup, null);
        Aboutpopup = new PopupWindow(container, 400, 400, true);
        Aboutpopup.showAtLocation(RelativeLayout, Gravity.NO_GRAVITY, 500, 500);

//close popup if main window is clicked
        container.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Aboutpopup.dismiss();
                return true;


            }
        });
    }

I cannot manage to close the braces successfully.

the 3 that need close brace are
@override public boolean onOptionsItemSelected(MenuItem item) {
container.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {


Thanks in advance chaps.

Davie.
 
What's up with that? You seem to have the same number of '{' and '}', and they look to be in the right places.
 
It cleans OK , On a rebuild I get :

Error:(202, 6) error: reached end of file while parsing on that last close brace on the code.
 
break statements in switch statements are optional. If there isn't one in a case, the code execution will fall through to the next case.
 
It's not really clear what your intention is with this, as the case literally does nothing (should there be code to execute?).
But I think it would be useful to post the whole class, because I suspect your problem is somewhere else in the code, and the compiler gets really confused.
 
Thanks Lv426

heres the full activity:

The case isnt really needed - it will always be "action_settings"

Code:
package com.welfareindustries.helloworld;

import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.MotionEvent;
import android.view.Gravity;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import org.joda.time.DateTime;
import org.joda.time.Hours;
import org.joda.time.Minutes;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MyActivity extends AppCompatActivity {

    ImageButton buttonGo;//setimage button
    final MediaPlayer mp = new MediaPlayer();//gonna play tunes

    //FOR THE POP UP
    private PopupWindow Aboutpopup;
    private LayoutInflater layoutinflater;
    private RelativeLayout RelativeLayout;

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

        //popup
        RelativeLayout = (RelativeLayout) findViewById(R.id.relative);


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


        //FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        //fab.setOnClickListener(new View.OnClickListener() {


    }

    public void addListenerOnButton() {

        buttonGo = (ImageButton) findViewById(R.id.buttonGo);
        buttonGo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {


/////////////////////////////////////play music

                if (mp.isPlaying()) {
                    mp.stop();
                }

                try {
                    mp.reset();
                    AssetFileDescriptor afd;
                    afd = getAssets().openFd("r2d2.wav");
                    mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                    mp.prepare();
                    mp.start();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


//////////////////////////////////////////////////////////////////////////////////////////////////music stop


                //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                //      .setAction("Action", null).show();

///////////////////////////////////////SOSH/////////////////////////////////////////////////////////
                Spinner spinnerSOSH = (Spinner) findViewById(R.id.spinnerSOSH);
                String SOSHVal = spinnerSOSH.getSelectedItem().toString();

                //final TextView textView = (TextView) findViewById(R.id.SOSHText);
                //textView.setText(SOSHVal);
////////////////////////////////////////////SOSM////////////////////////////////////////////////////

                Spinner spinnerSOSM = (Spinner) findViewById(R.id.spinnerSOSM);
                String SOSMVal = spinnerSOSM.getSelectedItem().toString();

                //final TextView textView1 = (TextView) findViewById(R.id.SOSMtext);
                //textView1.setText(SOSMVal);
////////////////////////////////////////////EOSH////////////////////////////////////////////////
                Spinner spinnerEOSH = (Spinner) findViewById(R.id.spinnerEOSH);
                String EOSHVal = spinnerEOSH.getSelectedItem().toString();

                // final TextView textView2 = (TextView) findViewById(R.id.EOSHText);
                //textView2.setText(EOSHVal);

/////////////////////////////////////////////EOSM////////////////////////////////////////////////
                Spinner spinnerEOSM = (Spinner) findViewById(R.id.spinnerEOSM);
                String EOSMVal = spinnerEOSM.getSelectedItem().toString();

                //final TextView textView3 = (TextView) findViewById(R.id.EOSMText);
                //textView3.setText(EOSMVal);
////////////////////////////////////////////////////////////////////////////////////////////////////

                //join Start Hr and Min to get e.g. 11.15
                String DateStart = String.valueOf(SOSHVal) + ":" + String.valueOf(SOSMVal);
                String DateStop = String.valueOf(EOSHVal) + ":" + String.valueOf(EOSMVal);


                //String dateStart = "09:23";
                //String dateStop = "10:31";

                SimpleDateFormat format = new SimpleDateFormat("HH:mm");


                Date d1 = null;
                Date d2 = null;

                try {
                    d1 = format.parse(DateStart);
                    d2 = format.parse(DateStop);

                    DateTime dt1 = new DateTime(d1);
                    DateTime dt2 = new DateTime(d2);

                    //System.out.print(Days.daysBetween(dt1, dt2).getDays() + " days, ");
                    String Hoursresult = (Hours.hoursBetween(dt1, dt2).getHours() % 24 + " Hrs");
                    String Minutesresult = (Minutes.minutesBetween(dt1, dt2).getMinutes() % 60 + " Mins");
                    //System.out.print(Seconds.secondsBetween(dt1, dt2).getSeconds() % 60 + " seconds.");


                    ///display hour result
                    final TextView textView4 = (TextView) findViewById(R.id.HourDurationText);
                    textView4.setText(Hoursresult);
                    //display minute result
                    final TextView textView5 = (TextView) findViewById(R.id.MinuteDurationText);
                    textView5.setText(Minutesresult);


                } catch (Exception e) {
                    e.printStackTrace();
                }

            }


        });
    }


    @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_my, 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.
        switch (item.getItemId()) {
            case R.id.action_settings:

        //launch the popup!
        layoutinflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
        ViewGroup container = (ViewGroup) layoutinflater.inflate(R.layout.popup, null);
        Aboutpopup = new PopupWindow(container, 400, 400, true);
        Aboutpopup.showAtLocation(RelativeLayout, Gravity.NO_GRAVITY, 500, 500);

//close popup if main window is clicked
        container.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Aboutpopup.dismiss();
                return true;


            }
        });
    }
 
I could be wrong, but you appear to have no closing '}' for the class. As in

Code:
public class MyActivity extends AppCompatActivity {
   
   ....
}

Just add an extra '}' at the end. But doesn't Android Studio highlight this?
 
Ok this works :

Code:
//close popup if main window is clicked
                container.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View view, MotionEvent motionEvent) {
                        Aboutpopup.dismiss();
                        //return true;


                    }
                });


        }

        return true;
    }
}
 
Back
Top Bottom