我正在开发一个用于学习目的的简单应用程序,其中有一个复选框和一个文本行,显示它被选中或未选中。但它显示了线setContentView(R.layout.main)
和cb=(CheckBox)findViewById(R.id.check)
一个错误,这是我的Java代码:错误:Android Studio中的setContentView(R.layout.main)
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.I'm与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。 – starkshang
它显示'无法解析符号'主'' – Kaushal28