0

I used the code from this answer by Jared Rummler in my project, and the following code is the outcome.

public class MainActivity extends Activity {

    Button dadclink;

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

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

        public static Intent newInstagramProfileIntent(PackageManager pm, String url) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            try {
                if (pm.getPackageInfo("com.instagram.android", 0) != null) {
                    if (url.endsWith("/")) {
                        url = url.substring(0, url.length() - 1);
                    }
                    String natgeo = url.substring(url.lastIndexOf("/") + 1);
                    intent.setData(Uri.parse("http://instagram.com/_u/" + natgeo));
                    intent.setPackage("com.instagram.android");
                    return intent;
                }
            } catch (NameNotFoundException e) {
            }
            intent.setData(Uri.parse(url));
            return intent;
        }};

However, I got syntax errors on this line:

public static Intent newInstagramProfileIntent(PackageManager pm, String url) {

with the following errors:

  • Illegal modifier for parameter newInstagramProfileIntent; only final is permitted
  • Syntax error on token ",", ; expected
  • Syntax error on token ")", ; expected

How to fix this?

Community
  • 1
  • 1
Tina
  • 15
  • 4
  • Post the exact error please. My guess is that it's an invalid cast.... – Josh Nov 17 '14 at 00:42
  • If I changed anything, it would give me several errors, the only error in the code now is the **;** in the dadclink = (Button) findViewById(R.id.dadclink)**;** – Tina Nov 17 '14 at 00:48
  • If that's what you mean! – Tina Nov 17 '14 at 00:49
  • Not quite... What's the error message? If you're using eclipse, it should give you a message detailing the error. – Josh Nov 17 '14 at 01:03
  • Desertion:System error on token ",", expected - Recurses:MainActivity - Path:/A/scr/com/example/a - Location:Line27 .. although it's on line 25 – Tina Nov 17 '14 at 01:26
  • You wrote *"I copied and pasted the code that you added and I have one error"*, from where did you copy that code? – Andrew T. Nov 17 '14 at 02:31
  • 1
    The error is actually on line 27: `public static Intent newInstagramProfileIntent(PackageManager pm, String url) {`. You cannot have a function inside a function like that. – Andrew T. Nov 17 '14 at 02:43
  • It got me really confused! I saw Jared Rummler pasting some of it and I copied it inside my code!! How should I type it? – Tina Nov 17 '14 at 02:57

1 Answers1

0

You cannot have a function declaration inside a function.

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

    // illegal operation
    public static Intent newInstagramProfileIntent(PackageManager pm, String url) {
        ...
    }
};

Related: Function within function in Java

If I understand what you wanted to do, you wanted to create a listener to a button, which uses an Instagram Intent when clicked. The code should be modified to this:

public void addListenerOnButton() {
    dadclink = (Button) findViewById(R.id.dadclink);
    dadclink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = newInstagramProfileIntent(getPackageManager(),
                    "your_string_url");

            // do something with the intent, perhaps starting an activity
            startActivity(intent);
        }
    });
}

public static Intent newInstagramProfileIntent(PackageManager pm, String url) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    try {
        if (pm.getPackageInfo("com.instagram.android", 0) != null) {
            if (url.endsWith("/")) {
                url = url.substring(0, url.length() - 1);
            }

            String natgeo = url.substring(url.lastIndexOf("/") + 1);
            intent.setData(Uri.parse("http://instagram.com/_u/" + natgeo));
            intent.setPackage("com.instagram.android");
            return intent;
        }
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }

    intent.setData(Uri.parse(url));
    return intent;
}
Community
  • 1
  • 1
Andrew T.
  • 4,701
  • 8
  • 43
  • 62
  • 1
    It's working well, still need a little work on the manifest! Thank you so much :) – Tina Nov 17 '14 at 03:46
  • Another question, if you don't mind .. How can I code for example #cat in Instagram like the picture? http://oi60.tinypic.com/2rw8axg.jpg In (A) I go to search, then "HASHTAGS", then I write "Cat", I get a result of "#Cat", I then click on "#Cat" to get all "#Cat" pictures in "HASHTAGS" in (B)? Thank you :) – Tina Nov 17 '14 at 05:02
  • Sorry, I don't have any idea for that, and that should be asked as a new question. But please don't post the question as it is; at least show the effort of what you've tried. We don't encourage giving free code here. – Andrew T. Nov 17 '14 at 05:08
  • Andrew - as you know, the code worked fine, but when I uninstall the Instagram, it gives me an error, I just want it to go to play.google to install Instagram – Tina Nov 17 '14 at 07:55
  • For that, you might be interested to [check if the app is installed](http://stackoverflow.com/q/11392183/2821954), then [open the Play Store programmatically](http://stackoverflow.com/q/11753000/2821954). As a hint, you need to use `com.instagram.android` as the argument (taken from the [URL of its Play Store](https://play.google.com/store/apps/details?id=com.instagram.android)) – Andrew T. Nov 18 '14 at 03:08