我正在关注来自http://www.javacodegeeks.com/2010/10/android-full-app-part-1-main-activity.html的 android 应用程序教程
我想我几乎做对了所有事情,但是在尝试引用 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>
这是我的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>