2

I am creating a navigations app to the application settings.
For that I am creating the following code, but, as I mention above in Title, I am getting a Syntax Error.
Kindly Guide me through this problem.

here is my MainActivity.Java

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

    public MainActivity() {

    }

    private boolean MyStartActivity(Intent intent) {
        try {
            startActivity(intent);
        } catch (ActivityNotFoundException activitynotfoundexception) {
            return false;
        }

        return true;
    }

    protected boolean isAppInstalled(String s) {
        PackageManager packagemanager = getPackageManager();

        try {
            packagemanager.getPackageInfo(s, 128);
        } catch (android.content.pm.PackageManager.NameNotFoundException namenotfoundexception) {
            return false;
        }

        return true;
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(0x7f030000);

        if (getIntent().getIntExtra("reload", 0) == 1) {
            if (isAppInstalled("com.sample.test")) {
                Intent intent = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS");
                intent.addCategory("android.intent.category.DEFAULT");
                intent.setData(Uri.parse("package:com.sample.test"));
                startActivity(intent);
            } else {
                Toast.makeText(getApplicationContext(), "Game Not Instaled", 0).show();
            }
        }

        ((Button)findViewById(0x7f080000)).setOnClickListener(new android.view.View.OnClickListener() {
            final MainActivity this$0;

            public void onClick(View view) {
                if (isAppInstalled("com.sample.test")) {
                    Intent intent1 = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS");
                    intent1.addCategory("android.intent.category.DEFAULT");
                    intent1.setData(Uri.parse("package:com.sample.test"));
                    startActivity(intent1);

                    return;
                } else {
                    Toast.makeText(getApplicationContext(), "Game Not Instaled", 0).show();

                    return;
                }
            }

            {
                this$0 = MainActivity.this;
                super(); //Constructor call must be the first statement in a constructor
            }
        });
    }

}
8
  • 3
    What exactly are you trying to do? That will be automatically be done by the compiler while generating the inner class. Commented Apr 10, 2014 at 11:13
  • 6
    Remove the empty contructor MainActivity(){} and what the hell is final MainActivity this$0;? and this$0 = MainActivity.this; And this is insane: setContentView(0x7f030000); (you should use R.layout.your_layout_name, instead). This is also harmful: super(); //Constructor call must be the first statement in a constructor Commented Apr 10, 2014 at 11:14
  • 4
    @JaiSharma Just remove that initializer block that is giving you the error. That is simply redundant there. Also, remove the variable this$0. Commented Apr 10, 2014 at 11:17
  • 2
    @BobMalooga While your advice is sound, I'm not sure your tone is really that friendly to a new programmer. Commented Apr 10, 2014 at 11:19
  • 1
    @BobMalooga Perhaps it's just my interpretation. But if someone looked at something I did for the first time and peppered their response with "What the hell is this?" "This is sheer madness!!" etc., it's a little demeaning. Commented Apr 10, 2014 at 11:21

2 Answers 2

1

You created an anonymous inner class:

new android.view.View.OnClickListener() {
    // code exists here

    super();
}

and at the bottom of that class you call super(); which is the super constructor call. This call must be the first one, but in your case is totally unnecessary. Just remove it.

Sign up to request clarification or add additional context in comments.

1 Comment

super() cannot exist anywhere in an anonymous class.
1

For the record, the reason why this code doesn't compile isn't because you've placed super() at the wrong place in your anonymous class. It's because anonymous classes can't have an explicitly declared constructor, at all.

There is nowhere you call call super() within an anonymous class without a compiler error.

The other answers are correct in pointing out it would be redundant anyway, even if it were possible.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.