语法错误:构造函数调用必须是构造函数中的第一个语句

时间:2014-04-10 11:11:57

标签: java android constructor

我正在为应用程序设置创建导航应用程序 为此我创建了以下代码,但是,正如我在Title中提到的那样,我收到了语法错误 请指导我解决这个问题。

这是我的 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
            }
        });
    }

}

2 个答案:

答案 0 :(得分:2)

您创建了一个匿名内部类:

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

    super();
}

并在该类的底部调用super();这是超级构造函数调用。这个电话必须是第一个,但在你的情况下是完全没必要的。只需删除它。

答案 1 :(得分:1)

为了记录,此代码无法编译的原因并不是因为您已将super()放置在匿名类的错误位置 。这是因为匿名课程can't have an explicitly declared constructor

在没有编译器错误的情况下,无法在匿名类中调用call super()

其他答案是正确的指出,即使有可能,它仍然是多余的。