1. Are you ready for the Galaxy S20? Here is everything we know so far!

braces problem that i just cannot resolve

Discussion in 'Android Development' started by fenster89411, Feb 1, 2016.

  1. fenster89411

    fenster89411 Lurker
    Thread Starter

    Guys,

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

    Code (Java):
    1.    
    2.     @Override
    3.                public boolean onOptionsItemSelected(MenuItem item) {
    4.         // Handle action bar item clicks here. The action bar will
    5.         // automatically handle clicks on the Home/Up button, so long
    6.         // as you specify a parent activity in AndroidManifest.xml.
    7.         switch (item.getItemId()) {
    8.             case R.id.action_settings:
    9.         }
    10.         //launch the popup!
    11.         layoutinflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    12.         ViewGroup container = (ViewGroup) layoutinflater.inflate(R.layout.popup, null);
    13.         Aboutpopup = new PopupWindow(container, 400, 400, true);
    14.         Aboutpopup.showAtLocation(RelativeLayout, Gravity.NO_GRAVITY, 500, 500);
    15.  
    16. //close popup if main window is clicked
    17.         container.setOnTouchListener(new View.OnTouchListener() {
    18.             @Override
    19.             public boolean onTouch(View view, MotionEvent motionEvent) {
    20.                 Aboutpopup.dismiss();
    21.                 return true;
    22.  
    23.  
    24.             }
    25.         });
    26.     }
    27.  
    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.

     


  2. Best Answer:
    Post #9 by Deleted User, Feb 1, 2016 (1 points)

    1. Download the Forums for Android™ app!


      Download

       
  3. Deleted User

    Deleted User Guest

    What's up with that? You seem to have the same number of '{' and '}', and they look to be in the right places.
     
  4. InkedAndroid

    InkedAndroid Newbie

    the first bracket after the case needs to be moved down to after the aboutpopup and you need to and "break:" also
     
  5. fenster89411

    fenster89411 Lurker
    Thread Starter

    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.
     
  6. fenster89411

    fenster89411 Lurker
    Thread Starter

    I moved the bracket but it causes "cannot resolve symbol "container" :(
     
  7. Deleted User

    Deleted User Guest

    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.
     
  8. Deleted User

    Deleted User Guest

    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.
     
  9. fenster89411

    fenster89411 Lurker
    Thread Starter

    Thanks Lv426

    heres the full activity:

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

    Code (Text):
    1.  
    2. package com.welfareindustries.helloworld;
    3.  
    4. import android.content.res.AssetFileDescriptor;
    5. import android.media.MediaPlayer;
    6. import android.os.Bundle;
    7. import android.support.v7.app.AppCompatActivity;
    8. import android.support.v7.widget.Toolbar;
    9. import android.view.LayoutInflater;
    10. import android.view.Menu;
    11. import android.view.MenuItem;
    12. import android.view.View;
    13. import android.view.ViewGroup;
    14. import android.view.MotionEvent;
    15. import android.view.Gravity;
    16. import android.view.View.OnClickListener;
    17. import android.widget.ImageButton;
    18. import android.widget.PopupWindow;
    19. import android.widget.RelativeLayout;
    20. import android.widget.Spinner;
    21. import android.widget.TextView;
    22.  
    23. import org.joda.time.DateTime;
    24. import org.joda.time.Hours;
    25. import org.joda.time.Minutes;
    26.  
    27. import java.io.IOException;
    28. import java.text.SimpleDateFormat;
    29. import java.util.Date;
    30.  
    31. public class MyActivity extends AppCompatActivity {
    32.  
    33.     ImageButton buttonGo;//setimage button
    34.     final MediaPlayer mp = new MediaPlayer();//gonna play tunes
    35.  
    36.     //FOR THE POP UP
    37.     private PopupWindow Aboutpopup;
    38.     private LayoutInflater layoutinflater;
    39.     private RelativeLayout RelativeLayout;
    40.  
    41.     @Override
    42.     protected void onCreate(Bundle savedInstanceState) {
    43.         super.onCreate(savedInstanceState);
    44.         setContentView(R.layout.activity_my);
    45.  
    46.         //popup
    47.         RelativeLayout = (RelativeLayout) findViewById(R.id.relative);
    48.  
    49.  
    50.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    51.         setSupportActionBar(toolbar);
    52.         addListenerOnButton();
    53.  
    54.  
    55.         //FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    56.         //fab.setOnClickListener(new View.OnClickListener() {
    57.  
    58.  
    59.     }
    60.  
    61.     public void addListenerOnButton() {
    62.  
    63.         buttonGo = (ImageButton) findViewById(R.id.buttonGo);
    64.         buttonGo.setOnClickListener(new OnClickListener() {
    65.             @Override
    66.             public void onClick(View arg0) {
    67.  
    68.  
    69. /////////////////////////////////////play music
    70.  
    71.                 if (mp.isPlaying()) {
    72.                     mp.stop();
    73.                 }
    74.  
    75.                 try {
    76.                     mp.reset();
    77.                     AssetFileDescriptor afd;
    78.                     afd = getAssets().openFd("r2d2.wav");
    79.                     mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
    80.                     mp.prepare();
    81.                     mp.start();
    82.                 } catch (IllegalStateException e) {
    83.                     e.printStackTrace();
    84.                 } catch (IOException e) {
    85.                     e.printStackTrace();
    86.                 }
    87.  
    88.  
    89. //////////////////////////////////////////////////////////////////////////////////////////////////music stop
    90.  
    91.  
    92.                 //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
    93.                 //      .setAction("Action", null).show();
    94.  
    95. ///////////////////////////////////////SOSH/////////////////////////////////////////////////////////
    96.                 Spinner spinnerSOSH = (Spinner) findViewById(R.id.spinnerSOSH);
    97.                 String SOSHVal = spinnerSOSH.getSelectedItem().toString();
    98.  
    99.                 //final TextView textView = (TextView) findViewById(R.id.SOSHText);
    100.                 //textView.setText(SOSHVal);
    101. ////////////////////////////////////////////SOSM////////////////////////////////////////////////////
    102.  
    103.                 Spinner spinnerSOSM = (Spinner) findViewById(R.id.spinnerSOSM);
    104.                 String SOSMVal = spinnerSOSM.getSelectedItem().toString();
    105.  
    106.                 //final TextView textView1 = (TextView) findViewById(R.id.SOSMtext);
    107.                 //textView1.setText(SOSMVal);
    108. ////////////////////////////////////////////EOSH////////////////////////////////////////////////
    109.                 Spinner spinnerEOSH = (Spinner) findViewById(R.id.spinnerEOSH);
    110.                 String EOSHVal = spinnerEOSH.getSelectedItem().toString();
    111.  
    112.                 // final TextView textView2 = (TextView) findViewById(R.id.EOSHText);
    113.                 //textView2.setText(EOSHVal);
    114.  
    115. /////////////////////////////////////////////EOSM////////////////////////////////////////////////
    116.                 Spinner spinnerEOSM = (Spinner) findViewById(R.id.spinnerEOSM);
    117.                 String EOSMVal = spinnerEOSM.getSelectedItem().toString();
    118.  
    119.                 //final TextView textView3 = (TextView) findViewById(R.id.EOSMText);
    120.                 //textView3.setText(EOSMVal);
    121. ////////////////////////////////////////////////////////////////////////////////////////////////////
    122.  
    123.                 //join Start Hr and Min to get e.g. 11.15
    124.                 String DateStart = String.valueOf(SOSHVal) + ":" + String.valueOf(SOSMVal);
    125.                 String DateStop = String.valueOf(EOSHVal) + ":" + String.valueOf(EOSMVal);
    126.  
    127.  
    128.                 //String dateStart = "09:23";
    129.                 //String dateStop = "10:31";
    130.  
    131.                 SimpleDateFormat format = new SimpleDateFormat("HH:mm");
    132.  
    133.  
    134.                 Date d1 = null;
    135.                 Date d2 = null;
    136.  
    137.                 try {
    138.                     d1 = format.parse(DateStart);
    139.                     d2 = format.parse(DateStop);
    140.  
    141.                     DateTime dt1 = new DateTime(d1);
    142.                     DateTime dt2 = new DateTime(d2);
    143.  
    144.                     //System.out.print(Days.daysBetween(dt1, dt2).getDays() + " days, ");
    145.                     String Hoursresult = (Hours.hoursBetween(dt1, dt2).getHours() % 24 + " Hrs");
    146.                     String Minutesresult = (Minutes.minutesBetween(dt1, dt2).getMinutes() % 60 + " Mins");
    147.                     //System.out.print(Seconds.secondsBetween(dt1, dt2).getSeconds() % 60 + " seconds.");
    148.  
    149.  
    150.                     ///display hour result
    151.                     final TextView textView4 = (TextView) findViewById(R.id.HourDurationText);
    152.                     textView4.setText(Hoursresult);
    153.                     //display minute result
    154.                     final TextView textView5 = (TextView) findViewById(R.id.MinuteDurationText);
    155.                     textView5.setText(Minutesresult);
    156.  
    157.  
    158.                 } catch (Exception e) {
    159.                     e.printStackTrace();
    160.                 }
    161.  
    162.             }
    163.  
    164.  
    165.         });
    166.     }
    167.  
    168.  
    169.     @Override
    170.     public boolean onCreateOptionsMenu(Menu menu) {
    171.         // Inflate the menu; this adds items to the action bar if it is present.
    172.         getMenuInflater().inflate(R.menu.menu_my, menu);
    173.         return true;
    174.     }
    175.  
    176.  
    177.  
    178.  
    179.     @Override
    180.                public boolean onOptionsItemSelected(MenuItem item) {
    181.         // Handle action bar item clicks here. The action bar will
    182.         // automatically handle clicks on the Home/Up button, so long
    183.         // as you specify a parent activity in AndroidManifest.xml.
    184.         switch (item.getItemId()) {
    185.             case R.id.action_settings:
    186.  
    187.         //launch the popup!
    188.         layoutinflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    189.         ViewGroup container = (ViewGroup) layoutinflater.inflate(R.layout.popup, null);
    190.         Aboutpopup = new PopupWindow(container, 400, 400, true);
    191.         Aboutpopup.showAtLocation(RelativeLayout, Gravity.NO_GRAVITY, 500, 500);
    192.  
    193. //close popup if main window is clicked
    194.         container.setOnTouchListener(new View.OnTouchListener() {
    195.             @Override
    196.             public boolean onTouch(View view, MotionEvent motionEvent) {
    197.                 Aboutpopup.dismiss();
    198.                 return true;
    199.  
    200.  
    201.             }
    202.         });
    203.     }
    204.  
     
  10. Deleted User

    Deleted User Guest

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

    Code (Text):
    1.  
    2. public class MyActivity extends AppCompatActivity {
    3.    
    4.    ....
    5. }
    6.  
    Just add an extra '}' at the end. But doesn't Android Studio highlight this?
     
  11. fenster89411

    fenster89411 Lurker
    Thread Starter

    I tried to move the brace but no matter where I put it then Android Studio makes it as bad.
    This is my first stab at java/android and this has me stumped!

    Ive loaded the project in case any of you want to take a look - its a shift calculator for my Mrs.

    https://www.dropbox.com/s/4q9abuws8ruo2bg/HelloWorld.rar?dl=0

    tomorrow I might strip it back and try again.


    Thanks guys!
     
  12. fenster89411

    fenster89411 Lurker
    Thread Starter

    Ok this works :

    Code (Text):
    1.  
    2.  
    3. //close popup if main window is clicked
    4.                 container.setOnTouchListener(new View.OnTouchListener() {
    5.                     @Override
    6.                     public boolean onTouch(View view, MotionEvent motionEvent) {
    7.                         Aboutpopup.dismiss();
    8.                         //return true;
    9.  
    10.  
    11.                     }
    12.                 });
    13.  
    14.  
    15.         }
    16.  
    17.         return true;
    18.     }
    19. }
    20.  
    21.  
     
Loading...

Share This Page

Loading...