带有匿名内部类的Java Eclipse android语法错误

时间:2013-11-05 08:13:51

标签: java android syntax-error anonymous-inner-class

我正在尝试使用3个按钮在android中编程一种“菜单”,并且OnClickListeners记录每个按钮的输入。 但是,我遇到了一些奇怪的语法错误。

这是我的MainActivity.java:

package com.example.galaxydefense;

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

public class MainActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

Button buttonplay=(Button)findViewById(R.id.buttonplay);
Button buttonhelp=(Button)findViewById(R.id.buttonhelp);
Button buttoncredits=(Button)findViewById(R.id.buttoncredits);
buttonplay.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Thread play=new Thread() {
            @Override
            public void run() {
                try {
                    Intent play=new Intent("android.intent.action.PLAY");
                    startActivity(play);
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
                finally {
                    finish();
                }
            }
        };
    }
});
buttonhelp.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Thread help=new Thread() {
            @Override
            public void run() {
                try {
                    Intent help=new Intent("android.intent.action.HELP");
                    startActivity(help);
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
                finally {
                    finish();
                }
            }
        };
    }
}
);
buttoncredits.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Thread credits=new Thread() {
            @Override
            public void run() {
                try {
                    Intent credits=new Intent("android.intent.action.CREDITS");
                    startActivity(credits);
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
                finally {
                    finish();
                }
            }
        };
    }
}
);
}

我收到以下错误:

  

第24行 - 令牌“}”上的语法错误,删除此令牌

     

第94行 - 语法错误,插入“}”以完成ClassBody

我不确定这个错误实际上是语法错误还是编译器错误诊断的另一个错误。

2 个答案:

答案 0 :(得分:0)

您不能将代码直接放在类中。 请使用构造函数或初始化程序块。

你在做什么是在MainActivity中定义3个字段:(buttonPlay,buttonHelp,buttonCredits):这没关系。但是你不能像你那样直接编写代码。

您可以通过将您的字段包裹在字段内的初始化程序块中来绕过此行为,如下所示:

{ /* This is an initializer block ... */
buttonplay.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Thread play=new Thread() {
            @Override
            public void run() {
                try {
                    Intent play=new Intent("android.intent.action.PLAY");
                    startActivity(play);
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
                finally {
                    finish();
                }
            }
        };
    }
});
buttonhelp.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Thread help=new Thread() {
            @Override
            public void run() {
                try {
                    Intent help=new Intent("android.intent.action.HELP");
                    startActivity(help);
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
                finally {
                    finish();
                }
            }
        };
    }
}
);
buttoncredits.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Thread credits=new Thread() {
            @Override
            public void run() {
                try {
                    Intent credits=new Intent("android.intent.action.CREDITS");
                    startActivity(credits);
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
                finally {
                    finish();
                }
            }
        };
    }
}
);
/* Initializer block finishes here */ }

答案 1 :(得分:0)

你不能写出功能上的监听器。用以下代码替换您的代码:

package com.example.galaxydefense;

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

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button buttonplay=(Button)findViewById(R.id.buttonplay);
    Button buttonhelp=(Button)findViewById(R.id.buttonhelp);
    Button buttoncredits=(Button)findViewById(R.id.buttoncredits);
    buttonplay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Thread play=new Thread() {
                @Override
                public void run() {
                    try {
                        Intent play=new Intent("android.intent.action.PLAY");
                        startActivity(play);
                    }
                    catch(Exception e) {
                        e.printStackTrace();
                    }
                    finally {
                        finish();
                    }
                }
            };
        }
    });
    buttonhelp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Thread help=new Thread() {
                @Override
                public void run() {
                    try {
                        Intent help=new Intent("android.intent.action.HELP");
                        startActivity(help);
                    }
                    catch(Exception e) {
                        e.printStackTrace();
                    }
                    finally {
                        finish();
                    }
                }
            };
        }
    }
    );
    buttoncredits.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Thread credits=new Thread() {
                @Override
                public void run() {
                    try {
                        Intent credits=new Intent("android.intent.action.CREDITS");
                        startActivity(credits);
                    }
                    catch(Exception e) {
                        e.printStackTrace();
                    }
                    finally {
                        finish();
                    }
                }
            };
        }
    }
    );
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}