简体   繁体   English

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

[英]Error: setContentView(R.layout.main) in Android Studio

I'm developing a simple app for learning purpose, In which there is a single check box and a text line, which shows it is checked or unchecked.我正在开发一个用于学习目的的简单应用程序,其中有一个复选框和一个文本行,显示它已选中或未选中。 But it shows an error in line setContentView(R.layout.main) and cb=(CheckBox)findViewById(R.id.check) this is my java code:但它在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");
        }
    }
}

It is not a duplicate question,I've seen the answers of the same questions but it was all for eclipse.I'm working with android studio.这不是一个重复的问题,我已经看到了相同问题的答案,但都是针对 eclipse 的。我正在使用 android studio。

This is my xml layout code:这是我的 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>

I'm new to android so please guide me.我是android的新手,所以请指导我。

Your import is wrong:您的导入错误:

import android.R;

You didn't import your own R file, try to import your <package>.R, maybe com.example.kaushal.R , not android.R.您没有导入自己的 R 文件,请尝试导入您的 <package>.R,也许com.example.kaushal.R ,而不是 android.R。

import android.R;
import android.R.layout;

Clean your project and rebuild it again.清理您的项目并再次重建它。

you import your package name and than R file.你导入你的包名而不是 R 文件。 like that像那样

import com.example.kaushal.checkbox.R;导入 com.example.kaushal.checkbox.R;

and than rebuild your project.而不是重建你的项目。

import com.example.kaushal.checkbox.R;

Try setContentView(R.layout.activity_main);试试setContentView(R.layout.activity_main);

instead of setContentView(R.layout.main);而不是setContentView(R.layout.main);

I found the answer by my self.我自己找到了答案。 I thing the error is not because of wrong import as answered here.我认为错误不是因为此处回答的导入错误。 It was because of the use of wrong name and id of layout file and check box respectively.这是因为布局文件和check box分别使用了错误的nameid My .xml layout file name was activity_my_checkbox.xml .我的.xml布局文件名为activity_my_checkbox.xml So I should use:所以我应该使用:

setContentView(R.layout.activity_my_checkbox);

And my check box id was c instead of check , so I should use我的复选框 ID 是c而不是check ,所以我应该使用

cb=(CheckBox)findViewById(R.id.c);

I am solving this issue in a simple step:我正在通过一个简单的步骤解决这个问题:

go to file, select invalid cache/restart, choose invalid cache/restart.转到文件,选择无效缓存/重新启动,选择无效缓存/重新启动。

wait for a minute...等一下...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM