我正在开发一个用于学习目的的简单应用程序,其中有一个复选框和一个文本行,显示它已选中或未选中。但它在 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 新手,请指导我。
最佳答案
import android.R;
import android.R.layout;
清理您的项目并再次重建它。
关于java - Android Studio 中的错误 : setContentView(R.layout.main),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34962487/