Id无法解析或不是javacodegeeks教程中的字段



我正在学习来自http://www.javacodegeeks.com/2010/10/android-full-app-part-1-main-activity.html

我认为我几乎所有的事情都做对了,但在试图引用main.xml id的代码中,我仍然会出错,说"id无法解析或不是字段"。这是我的MovieSearchAppActivity.java代码

public class MovieSearchAppActivity extends Activity implements OnClickListener {
private static final String EMPTY_STRING= "";
private EditText searchEditText;
private RadioButton movieSearchRadioButton;
private RadioButton peopleSearchRadioButton;
private RadioGroup searchRadioGroup;
private TextView searchTypeTextView;
private Button searchButton;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.findAllViewsById();
    movieSearchRadioButton.setOnClickListener(radioButtonListener);
    peopleSearchRadioButton.setOnClickListener(radioButtonListener);
    searchButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        String query = searchEditText.getText().toString();
        if (movieSearchRadioButton.isChecked()){
            longToast(movieSearchRadioButton.getText() + " " + query);      
        }
        else if(peopleSearchRadioButton.isChecked()){
            longToast(peopleSearchRadioButton.getText() + " " + query);
        }
        }
    });
    searchEditText.setOnFocusChangeListener(new DftTextOnFocusListener(getString(R.string.search))); //error:"search cannot be resolved or in field.
    int id = searchRadioGroup.getCheckedRadioButtonId();
    RadioButton radioButton = (RadioButton) findViewById(id);
    searchTypeTextView.setText(radioButton.getText());
}
private void findAllViewsById() {
    EditText searchEditText = (EditText) findViewById(R.id.search_edit_text); //search_edit_text cannot be resolved or is not in field. same for below reference.
    movieSearchRadioButton = (RadioButton) findViewById(R.id.movie_search_radio_button);
    peopleSearchRadioButton = (RadioButton) findViewById(R.id.people_search_radio_button);
    searchRadioGroup = (RadioGroup) findViewById(R.id.search_radio_group);
    searchTypeTextView = (TextView) findViewById(R.id.search_type_text_view);
    searchButton = (Button) findViewById(R.id.search_button);
    }

这是我的main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
    android:id="@+id/search_edit_text"
    android:text="@string/search"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
 />
<RadioGroup
    android:id="@+id/search_radio_group"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <RadioButton
        android:id="@+id/movie_search_radio_button"
        android:checked="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/movies" />
    <RadioButton
        android:id="@+id/people_search_radio_button"
        android:checked="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/people" />
</RadioGroup>
<TextView 
    android:id="@+id/search_type_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#ffffff" />
<Button
    android:id="@+id/search_button"
    android:text="@string/search"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

这是我的字符串.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello_World">Search!</string>
<string name="app_name">MovieSearchApp</string>
<string name="search">Search</string>
<string name="movies">Movies</string>
<string name="people">People</string>
</resources>

尝试清理项目并重新生成。从Eclipse菜单"project"->"clean…".

最新更新