【发布时间】:2020-10-31 11:08:07
【问题描述】:
我正在开发一个用于学习目的的简单应用程序,其中有一个复选框和一个文本行,显示它已选中或未选中。但它在setContentView(R.layout.main) 和cb=(CheckBox)findViewById(R.id.check) 行显示错误,这是我的java 代码:
package com.example.kaushal.checkbox;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.R;
public class Checkbox extends Activity
implements CompoundButton.OnCheckedChangeListener {
CheckBox cb;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
cb=(CheckBox)findViewById(R.id.check);
cb.setOnCheckedChangeListener(this);
}
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
cb.setText("This checkbox is: checked");
}
else {
cb.setText("This checkbox is: unchecked");
}
}
}
这不是一个重复的问题,我已经看到了相同问题的答案,但都是针对 eclipse。我正在使用 android studio。
这是我的 xml 布局代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Checkbox">
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This checkbox is: unchecked" />
</RelativeLayout>
我是android新手,所以请指导我。
【问题讨论】:
-
您的错误到底是什么?它无法正确识别 id?
-
显示你的布局 xml。
-
它显示
can not resolve symbol 'main' -
尝试从构建菜单清理项目并重建\
-
查看布局xml代码。