@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class HomeActivity extends Activity {
private static final String TAG = HomeActivity.class.getSimpleName();
private ImageButton imageButton;
private ImageButton imageButton2;
public ImageButton imageButton4;
public ImageButton imageButton5;
private ImageView imageView;
private ImageView imageView2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
imageButton = (ImageButton) findViewById(R.id.imageButton);
imageButton2 = (ImageButton) findViewById(R.id.imageButton2);
imageButton4 = (ImageButton) findViewById(R.id.imageButton4);
imageButton5 = (ImageButton) findViewById(R.id.imageButton5);
imageView = (ImageView) findViewById(R.id.imageView);
imageView2 = (ImageView) findViewById(R.id.imageView2);
//Single player button click event
imageButton4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(i);
finish();
};},
//multi player game button event
imageButton5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
MultiPlayer.class);
startActivity(i);
finish();
};}),
//Settings button event
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
MusicActivity.class);
startActivity(i);
finish();
}
});
}}
此代码中存在一个问题,如此')'在某个地方失踪了。光标在设置按钮事件中显示缺少的组件。它不是因为这个错误而构建的。缺少')'它终止了编译器并且无法构建。
答案 0 :(得分:2)
你永远不会在imagebutton4
关闭你的第一个onclicklistenerimageButton4.setOnClickListener**(**new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(i);
finish();
};},
结束行应该是这样的
}}),
但我并没有真正理解那些背后的kommas
答案 1 :(得分:0)
onClickListener出错:
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class HomeActivity extends Activity {
private static final String TAG = HomeActivity.class.getSimpleName();
private ImageButton imageButton;
private ImageButton imageButton2;
public ImageButton imageButton4;
public ImageButton imageButton5;
private ImageView imageView;
private ImageView imageView2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
imageButton = (ImageButton) findViewById(R.id.imageButton);
imageButton2 = (ImageButton) findViewById(R.id.imageButton2);
imageButton4 = (ImageButton) findViewById(R.id.imageButton4);
imageButton5 = (ImageButton) findViewById(R.id.imageButton5);
imageView = (ImageView) findViewById(R.id.imageView);
imageView2 = (ImageView) findViewById(R.id.imageView2);
//Single player button click event
imageButton4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(i);
finish();
}});
//multi player game button event
imageButton5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
MultiPlayer.class);
startActivity(i);
finish();
}});
//Settings button event
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
MusicActivity.class);
startActivity(i);
finish();
}
});
}}