java - Android Studio 中的错误 : setContentView(R.layout.main)

标签 java android checkbox

我正在开发一个用于学习目的的简单应用程序,其中有一个复选框和一个文本行,显示它已选中或未选中。但它在 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/

相关文章:

java - Android UI 元素的可见性没有改变

android - Viewflipper 如何处理 Android 中的后退按钮?

jquery - jsTree 复选框 ajax 请求,已选中

Android:带复选框的ListView

回调中的 Java RMI 超时

Java,基本类路径问题

java - sendBroadcast() 是在 Android 中保持 WakefulBroadcastReceiver 存活的最佳方式吗?

android - 线条在 Canvas 上不可见

android - ScrollView 不在Android Bottom Sheet 中滚动

javascript - 如何为CheckedMultiSelect的每个元素添加不同的标题?