0

我正在尝试用 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

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

4

2 回答 2

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 */ }
于 2013-11-05T08:21:56.237 回答
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;
}
}
于 2013-11-05T08:24:34.067 回答