Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Eclipse goes beyond me a bit

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Eclipse goes beyond me a bit

    I'm new to Java-programming, however, I do have experience with C++ and c#. I've now taken it upon myself to start programming for Android, and for that I of course need Java. I'm using Eclipse for this. Now get this: New programming language (Java), new target platform (Android) and new environment (Eclipse). So I'm in a bit over my head. Eclipse keeps complaining about the formatting of my code, and I can't see what's wrong with it.

    Below is my code, I've included in comments the errors Eclipse tells me I have.

    Basically, Eclipse claims that my accolade{}- and bracket() blocks aren't complete, but I think they are. I've purposely gone to work methodically., slowly adding code (there's not much of it, as you can see) to make sure that the brackets all added up.
    The error on line 16 is particularly puzzling: this piece of code was generated by Eclipse / Android Development Tools (ADT)-plugin. All I've done is change the formatting to make it clear if the brackets were complete. I think they are.
    And for some weird reason, Copying and psationg tis code from Eclipse gives me code which isn't visible in the editor. For instance the top line reads:
    package com.androidbook.multimedia; instead of the actual
    package com.magicmojo.timelog;
    I did copy some bits of code out of a androidbook-sample project, but not all of it, that's for sure, and besides: that line now reads package com.magicmojo.timelog;

    package com.magicmojo.timelog;
     
    import android.app.Activity;
    import android.content.ContentValues;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.media.MediaRecorder;
    import android.media.RingtoneManager;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
     
    package com.magicmojo.timelog;
     
    import android.app.Activity;
    import java.util.Date;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    public class timelog extends Activity
    {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        } // Syntax error on token "}", delete this token (line 16)
     
        //insert Button-stuff here?
        final Button record = (Button) findViewById(R.id.Button01);
        //record the current Time/Date()
        final int MAX_I = 5;
        final Date[] DateListing;
        DateListing = new Date[MAX_I];
        final int i = 0;
     
        record.setOnClickListener(new View.OnClickListener() 
        {
        	public void onClick(View v)
        	{
            	if (i<MAX_I)
                {
                	DateListing[i] = new Date();
                    i++;
                }
            	//else // toast an error
            }
        };// Syntax error, insert ")" to complete Expression (Line 37)
     
    }   // Syntax error, insert "}" to complete ClassBody (line 39)


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Eclipse goes beyond me a bit

    You can't call methods at the top of your class like that. You can only initialize variables. To call a method, you have to be in a valid method block.

    Even if you could, you're missing a closing parenthesis- it should match up with the open parenthesis after record.setOnClickListener(.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Eclipse goes beyond me a bit

    you're missing a closing parenthesis- it should match up with the open parenthesis after record.setOnClickListener(.
    Thanks, I missed that!
    And thanks for the rest of your reply too, I'll get to work on it.
    ---
    I've posted the above before I sudied the effects of what you said, but now I have and I'm confused - you're not referring to onCreate, are you? Because ADT put that there.
    And I'm affraid you r advice, as good as it is, hasn't addressed the problem of the } on line 16, which Eclipse wants to see deleted.
    Last edited by MagicMojo; March 1st, 2011 at 08:13 AM.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Eclipse goes beyond me a bit

    Quote Originally Posted by MagicMojo View Post
    you're not referring to onCreate, are you? Because ADT put that there.
    I'm referring to the entire block that begins with record.setOnClickListener(. You can't call a method at the class level like that. Put that inside another method, possibly a constructor.

    Quote Originally Posted by MagicMojo View Post
    And I'm affraid you r advice, as good as it is, hasn't addressed the problem of the } on line 16, which Eclipse wants to see deleted.
    Wrong. First off, it's not that Eclipse wants to see it deleted- it's a basic syntax error, which is a problem that any compiler is going to pick up. Eclipse has nothing to do with it. And secondly, the error is telling you to insert a closing parenthesis, which I seconded after showing you where the unmatched opening parenthesis is.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Eclipse goes beyond me a bit

    (Sorry fro replying this late!)
    I'm referring to the entire block that begins with record.setOnClickListener(. You can't call a method at the class level like that. Put that inside another method, possibly a constructor.
    Ah, but here's the sample code I got from the book Android Wireless Application Development (from Addison Wesley). This app seemed to contain all the info I needed, (a button which did soemthing, I could add my own code to the logic behind the button. They do ghe exact same thing I did (add code to button.setOnClickListener (new View.OnClickListener()) ). So anyway, I'm considering (very unelegantly) hacking my way out of this by making a new class called Parms and using that as a parameter class to lift the variables/objects I want to use to process this Button-click. A very unsatisfactory approach, but if no other solution presents itself, needs must.

    package com.androidbook.layout;
     
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
     
    public class DialogDisplay extends
        Activity {
     
        @Override
        protected void onCreate(
            Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super
                .onCreate(savedInstanceState);
     
            setContentView(R.layout.dialog);
     
            Button b = (Button)findViewById(R.id.do_dialog_btn);
     
            b.setOnClickListener(new View.OnClickListener() {
     
                public void onClick(
                    View v) {
     
                    new AlertDialog.Builder(DialogDisplay.this)
                    .setMessage("Please press agree to continue...")
                 .setPositiveButton("Agree", null)
     
                    .show();
                }
     
            });
        }
     
    }
    Now, I'm affraid tat I must beg of someone to take me by the hand. Sorry! Please?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Eclipse goes beyond me a bit

    I'm not sure what the problem is. The reason the b.setOnClickListener() line works in the code you posted is exactly what I said- it's inside another method, onCreate(). Your problem was that you were trying to do that outside of a method, which won't work.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 24
    Last Post: August 4th, 2014, 12:49 PM
  2. Eclipse
    By nasi in forum Java IDEs
    Replies: 3
    Last Post: May 6th, 2010, 01:35 PM
  3. Eclipse on Mac
    By Cuju in forum Java IDEs
    Replies: 6
    Last Post: March 19th, 2010, 09:29 PM
  4. eclipse help
    By mos33 in forum Java IDEs
    Replies: 3
    Last Post: November 24th, 2009, 02:10 PM
  5. About plugins for eclipse
    By sun26 in forum Java IDEs
    Replies: 1
    Last Post: November 20th, 2009, 09:43 AM