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

Missed a } and/or ) on if/else

Discussion in 'Android Development' started by SamoZain, Sep 21, 2014.

  1. SamoZain

    SamoZain Newbie
    Thread Starter

    I missed a } and/or ) at the end of the code, I tried so many times but I didn't get it, can someone know?
     



    1. Download the Forums for Android™ app!


      Download

       
  2. scary alien

    scary alien not really so scary
    VIP Member

    You're missing the trailiing "};" at the end of your code:

    Code (Text):
    1. doclink.setOnClickListener(new OnClickListener()
    2. {
    3.  @Override
    4.  public void onClick(View arg0)
    5.  {
    6.   if(!uri.contains("https://www.facebook.com/"))
    7.   {
    8.    String natgeo = "natgeo";
    9.    String uri = "fb://Page/" + natgeo;
    10.    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
    11.    startActivity(intent);
    12.   }
    13.   else
    14.   {
    15.    String natgeo = "natgeo";
    16.    String uri = "https://www.facebook.com/" + natgeo;
    17.    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
    18.    startActivity(i);
    19.   }
    20.  };
    21. [COLOR="Red"][B]};[/B]       // <<< this one here![/COLOR]
     
  3. SamoZain

    SamoZain Newbie
    Thread Starter

    Thank you, but it didn't work! I'll paste the all of the code:

    Code (Text):
    1. package com.example.a;
    2.  
    3. import android.app.Activity;
    4. import android.content.ActivityNotFoundException;
    5. import android.content.Intent;
    6. import android.net.Uri;
    7. import android.os.Bundle;
    8. import android.widget.Button;
    9. import android.view.View;
    10. import android.view.View.OnClickListener;
    11.  
    12.  
    13. public class MainActivity extends Activity {
    14.  
    15.     Button dadclink;
    16.  
    17.     @Override
    18.     public void onCreate(Bundle savedInstanceState) {
    19.         super.onCreate(savedInstanceState);
    20.         setContentView(R.layout.activity_main);
    21.         addListenerOnButton();
    22.     }
    23.    
    24.         public void addListenerOnButton() {
    25.          dadclink = (Button) findViewById(R.id.dadclink);
    26.  
    27.          doclink.setOnClickListener(new OnClickListener()
    28.          {
    29.           @Override
    30.           public void onClick(View arg0)
    31.           {
    32.            if(!uri.contains("https://www.facebook.com/"))
    33.            {
    34.             String natgeo = "natgeo";
    35.             String uri = "fb://Page/" + natgeo;
    36.             Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
    37.             startActivity(intent);
    38.            }
    39.            else
    40.            {
    41.             String natgeo = "natgeo";
    42.             String uri = "https://www.facebook.com/" + natgeo;
    43.             Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
    44.             startActivity(i);
    45.            }
    46.           };
    47.          };       // <<< this one here!
     
  4. Manaya

    Manaya Member

    That is why I prefer to open brackets on a new line with proper indent, avoids these kind of problems.

    Your code is a bit mess at the end, here it's how it should be (starting from the last else)

    Code (Text):
    1.            else
    2.            {
    3.              String natgeo = "natgeo";
    4.              String uri = "https://www.facebook.com/" + natgeo;
    5.              Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
    6.              startActivity(i);
    7.            } // close else
    8.           } // close OnClick()
    9.          }); // close OnClickListener Constructor, then close setOnClickListener() function
    10.     } // close addListenerOnButton
    11. } // close MainActivity class
     
    scary alien likes this.
  5. scary alien

    scary alien not really so scary
    VIP Member

    Sorry 'bout that :thinking:...I just matched-up the braces in vi to make sure they matched, but I didn't paste it in Eclipse like I'm guessing Manaya kindly did.

    And I agree about placing the braces on their own lines as Manaya mentioned and I demonstrated in my first post. I'll never understand why it's commonly shown and apparently taught to jam those on lines with other things--the indentation helps tremendously with catching mismatched braces (or parens or brackets for that matter), although I'm guessing that some of the IDEs these days (like Eclipse) mitigate that somewhat by their wysiwyg error displays, etc.

    Cheers!
     
  6. SamoZain

    SamoZain Newbie
    Thread Starter

    @scary alien thank you :)
    @Manaya I still have a problem with two of them, "uri" and "}" for closing addListenerOnButton ..

    Code (Text):
    1.  
    2.     doclink.setOnClickListener(new OnClickListener() {    
    3.            
    4.             @Override    
    5.             public void onClick(View arg0) {    
    6.           if(!uri.contains("https://www.facebook.com/")) {      
    7.               String natgeo = "natgeo";      
    8.               String uri = "fb://Page/" + natgeo;          
    9.               Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));      
    10.               startActivity(intent);        
    11.               }      
    12.  else
    13.            {
    14.              String natgeo = "natgeo";
    15.              String uri = "https://www.facebook.com/" + natgeo;
    16.              Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
    17.              startActivity(i);
    18.            } // close else
    19.           } // close OnClick()
    20.          }); // close OnClickListener Constructor, then close setOnClickListener() function
    21.     } // close addListenerOnButton
    22. } // close MainActivity class
    23.  
     
  7. SamoZain

    SamoZain Newbie
    Thread Starter

    Here is an entire codes .. it has no error, when I run it in the mobile, the "Run this" button appears, when I tick it to go to facebook, it gives me this error that says "Unfortunately, A has stopped"

    activity_main.xml

    Code (Text):
    1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2.     xmlns:tools="http://schemas.android.com/tools"
    3.     android:layout_width="fill_parent"
    4.     android:layout_height="fill_parent"
    5.     android:gravity="center" >
    6.  
    7.     <LinearLayout
    8.         android:layout_width="match_parent"
    9.         android:layout_height="wrap_content"
    10.         android:layout_centerHorizontal="true"
    11.         android:gravity="center"
    12.         android:orientation="vertical"
    13.         android:textStyle="italic" >
    14.  
    15.         <Button
    16.             android:id="@+id/dadclink"
    17.             android:layout_width="fill_parent"
    18.             android:layout_height="90dp"
    19.             android:layout_alignParentRight="true"
    20.             android:layout_alignParentTop="true"
    21.             android:gravity="center"
    22.             android:text="Run this"
    23.             android:textSize="38sp"
    24.             android:textStyle="italic" />
    25.     </LinearLayout>
    26.  
    27. </RelativeLayout>
    MainActivity.java

    Code (Text):
    1. package com.example.a;
    2.  
    3. import android.app.Activity;
    4. import android.content.Intent;
    5. import android.net.Uri;
    6. import android.os.Bundle;
    7. import android.widget.Button;
    8. import android.view.View;
    9. import android.view.View.OnClickListener;
    10.  
    11. public class MainActivity extends Activity {
    12.  
    13.     Button dadclink;
    14.  
    15.     @Override
    16.     public void onCreate(Bundle savedInstanceState) {
    17.         super.onCreate(savedInstanceState);
    18.         setContentView(R.layout.activity_main);
    19.         addListenerOnButton();
    20.     }
    21.  
    22.     public void addListenerOnButton() {
    23.         dadclink = (Button) findViewById(R.id.dadclink);
    24.  
    25.         dadclink.setOnClickListener(new OnClickListener() {
    26.             @SuppressWarnings("null")
    27.             @Override
    28.             public void onClick(View arg0) {
    29.                 String uri = null;
    30.                 String uri2 = uri;
    31.                 if (!uri2.contains("https://www.facebook.com/")) {
    32.                     String natgeo = "natgeo";
    33.                     String uri1 = "fb://Page/" + natgeo;
    34.                     Intent intent = new Intent(Intent.ACTION_VIEW, Uri
    35.                             .parse(uri1));
    36.                     startActivity(intent);
    37.                 } else {
    38.                     String natgeo = "natgeo";
    39.                     String uri1 = "https://www.facebook.com/" + natgeo;
    40.                     Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri1));
    41.                     startActivity(i);
    42.                 } // close else
    43.             } // close OnClick()
    44.         }); // close OnClickListener Constructor, then close
    45.             // setOnClickListener() function
    46.     } // close addListenerOnButton
    47. } // close MainActivity class
    Manifest.xml

    Code (Text):
    1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    2.     package="com.example.a"
    3.     android:versionCode="1"
    4.     android:versionName="1.0" >
    5.  
    6.     <uses-sdk
    7.         android:minSdkVersion="8"
    8.         android:targetSdkVersion="8" />
    9.  
    10.     <application
    11.         android:allowBackup="true"
    12.         android:icon="@drawable/ic_launcher"
    13.         android:label="@string/app_name"
    14.         android:theme="@style/AppTheme" >
    15.         <activity
    16.             android:name=".MainActivity"
    17.             android:label="@string/app_name"
    18.             android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
    19.             <intent-filter>
    20.                 <action android:name="android.intent.action.MAIN" />
    21.  
    22.                 <category android:name="android.intent.category.LAUNCHER" />
    23.             </intent-filter>
    24.         </activity>
    25.     </application>
    26.  
    27. </manifest>
     
  8. SamoZain

    SamoZain Newbie
    Thread Starter

    Solved :)
     
    scary alien likes this.
Loading...

Share This Page

Loading...