简体   繁体   中英

Id Cannot be resolved or is not a field from javacodegeeks tutorial

I'm following an android application tutorial from http://www.javacodegeeks.com/2010/10/android-full-app-part-1-main-activity.html

I think I did almost everything right, but I'm still getting error saying "id cannot be resolved or is not a field" on the code that tries to refer to the main.xml id's. Here is my MovieSearchAppActivity.java code

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);
    }

And here is my 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>

Here is my strings.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>

Try to clean the project and rebuild. From Eclipse menu "project"->"clean...".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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