我正在開發一個用於學習目的的簡單應用程序,其中有一個複選框和一個文本行,顯示它被選中或未選中。但它顯示了線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