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

Apps Missed a } and/or ) on if/else

SamoZain

Newbie
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?
doclink.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
if(!uri.contains("https://www.facebook.com/")) {
String natgeo = "natgeo";
String uri = "fb://Page/" + natgeo;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
}
else{
String natgeo = "natgeo";
String uri = "https://www.facebook.com/" + natgeo;
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(i);
}
};
 
You're missing the trailiing "};" at the end of your code:

Code:
doclink.setOnClickListener(new OnClickListener()
{ 
 @Override 
 public void onClick(View arg0)
 { 
  if(!uri.contains("https://www.facebook.com/"))
  { 
   String natgeo = "natgeo"; 
   String uri = "fb://Page/" + natgeo; 
   Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri)); 
   startActivity(intent); 
  } 
  else
  { 
   String natgeo = "natgeo"; 
   String uri = "https://www.facebook.com/" + natgeo; 
   Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri)); 
   startActivity(i); 
  } 
 };
[COLOR="Red"][B]};[/B]       // <<< this one here![/COLOR]
 
Thank you, but it didn't work! I'll paste the all of the code:

Code:
package com.example.a;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;


public class MainActivity extends Activity {
 
    Button dadclink;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addListenerOnButton();
    }
    
        public void addListenerOnButton() {
         dadclink = (Button) findViewById(R.id.dadclink);

         doclink.setOnClickListener(new OnClickListener()
         { 
          @Override 
          public void onClick(View arg0)
          { 
           if(!uri.contains("https://www.facebook.com/"))
           { 
            String natgeo = "natgeo"; 
            String uri = "fb://Page/" + natgeo; 
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri)); 
            startActivity(intent); 
           } 
           else
           { 
            String natgeo = "natgeo"; 
            String uri = "https://www.facebook.com/" + natgeo; 
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri)); 
            startActivity(i); 
           } 
          };
         };       // <<< this one here!
 
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:
           else
           { 
             String natgeo = "natgeo"; 
             String uri = "https://www.facebook.com/" + natgeo; 
             Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri)); 
             startActivity(i); 
           } // close else
          } // close OnClick()
         }); // close OnClickListener Constructor, then close setOnClickListener() function
    } // close addListenerOnButton
} // close MainActivity class
 
Thank you, but it didn't work! I'll paste the all of the code:

Sorry 'bout that :confused:...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!
 
@scary alien thank you :)
@Manaya I still have a problem with two of them, "uri" and "}" for closing addListenerOnButton ..

Code:
    doclink.setOnClickListener(new OnClickListener() {    
            
            @Override    
            public void onClick(View arg0) {    
          if(!uri.contains("https://www.facebook.com/")) {      
              String natgeo = "natgeo";      
              String uri = "fb://Page/" + natgeo;          
              Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));      
              startActivity(intent);         
              }      
 else
           { 
             String natgeo = "natgeo"; 
             String uri = "https://www.facebook.com/" + natgeo; 
             Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri)); 
             startActivity(i); 
           } // close else
          } // close OnClick()
         }); // close OnClickListener Constructor, then close setOnClickListener() function
    } // close addListenerOnButton
} // close MainActivity class
 
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:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:orientation="vertical"
        android:textStyle="italic" >

        <Button
            android:id="@+id/dadclink"
            android:layout_width="fill_parent"
            android:layout_height="90dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:gravity="center"
            android:text="Run this"
            android:textSize="38sp"
            android:textStyle="italic" />
    </LinearLayout>

</RelativeLayout>
MainActivity.java

Code:
package com.example.a;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

    Button dadclink;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addListenerOnButton();
    }

    public void addListenerOnButton() {
        dadclink = (Button) findViewById(R.id.dadclink);

        dadclink.setOnClickListener(new OnClickListener() {
            @SuppressWarnings("null")
            @Override
            public void onClick(View arg0) {
                String uri = null;
                String uri2 = uri;
                if (!uri2.contains("https://www.facebook.com/")) {
                    String natgeo = "natgeo";
                    String uri1 = "fb://Page/" + natgeo;
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri
                            .parse(uri1));
                    startActivity(intent);
                } else {
                    String natgeo = "natgeo";
                    String uri1 = "https://www.facebook.com/" + natgeo;
                    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri1));
                    startActivity(i);
                } // close else
            } // close OnClick()
        }); // close OnClickListener Constructor, then close
            // setOnClickListener() function
    } // close addListenerOnButton
} // close MainActivity class
Manifest.xml

Code:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.a"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
 
Back
Top Bottom