* R.id字段显示错误,因此在保存时显示没有像xml中那样的字段。 Main活动类代码发布在plz help下面,因为m仅限于此。关于清理和构建R.java类正在消失。它显示找不到与给定名称匹配的资源(在'text'处,值为'@ string / feedbackbutton')以及* 中的其他字段
主要Activity.java
package com.example.android.sec;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void sendFeedback(View button) {
}
final EditText nameField = (EditText) findViewById(R.id.EditTextName);
String name = nameField.getText().toString();
final EditText emailField = (EditText) findViewById(R.id.EditTextEmail);
String email = emailField.getText().toString();
final EditText feedbackField = (EditText) findViewById(R.id.EditTextFeedbackBody);
String feedback = feedbackField.getText().toString();
final Spinner feedbackSpinner = (Spinner) findViewById(R.id.SpinnerFeedbackType);
String feedbackType = feedbackSpinner.getSelectedItem().toString();
final CheckBox responseCheckbox = (CheckBox) findViewById(R.id.CheckBoxResponse);
boolean bRequiresResponse = responseCheckbox.isChecked();
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent">
<TextView
android:id="@+id/TextViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/feedbacktitle"
android:textSize="10pt">
</TextView>
<EditText
android:id="@+id/EditTextName"
android:layout_height="wrap_content"
android:hint="@string/feedbackname"
android:inputType="textPersonName"
android:layout_width="fill_parent">
</EditText>
<EditText
android:id="@+id/EditTextEmail"
android:layout_height="wrap_content"
android:hint="@string/feedbackemail"
android:inputType="textEmailAddress"
android:layout_width="fill_parent">
</EditText>
<Spinner
android:id="@+id/SpinnerFeedbackType"
android:layout_height="wrap_content"
android:prompt="@string/feedbacktype"
android:layout_width="fill_parent"
android:entries="@array/feedbacktypelist">
</Spinner>
<EditText
android:id="@+id/EditTextFeedbackBody"
android:layout_height="wrap_content"
android:hint="@string/feedbackbody"
android:inputType="textMultiLine"
android:lines="5"
android:layout_width="fill_parent">
</EditText>
<CheckBox
android:id="@+id/CheckBoxResponse"
android:layout_height="wrap_content"
android:text="@string/feedbackresponse"
android:layout_width="fill_parent">
</CheckBox>
<Button
android:id="@+id/ButtonSendFeedback"
android:layout_height="wrap_content"
android:text="@string/feedbackbutton"
android:onClick="sendFeedback"
android:layout_width="fill_parent">
</Button>
</LinearLayout>
</ScrollView>
Arrays.XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="feedbacktypelist">
<item>@string/feedbacktype1</item>
<item>@string/feedbacktype2</item>
<item>@string/feedbacktype3</item>
<item>@string/feedbacktype4</item>
</string-array>
</resources>
的strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--Other string resources also defined in this file… -->
<string name="feedbacktype1">Good</string>
<string name="feedbacktype2">Very Good</string>
<string name="feedbacktype3">Excellent</string>
<string name="feedbacktype4">Bad</string>
</resources>
答案 0 :(得分:0)
将初始化移至oncreate
EditText nameField,feedbackField;
Spinner feedbackSpinner;
CheckBox responseCheckbox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameField = (EditText) findViewById(R.id.EditTextName);
emailField = (EditText) findViewById(R.id.EditTextEmail);
String email = emailField.getText().toString();
feedbackField = (EditText) findViewById(R.id.EditTextFeedbackBody);
String feedback = feedbackField.getText().toString();
feedbackSpinner = (Spinner) findViewById(R.id.SpinnerFeedbackType);
String feedbackType = feedbackSpinner.getSelectedItem().toString();
responseCheckbox = (CheckBox) findViewById(R.id.CheckBoxResponse);
}
编辑:
确保您已在strings.xml中定义了在layout xml
中使用的所有字符串答案 1 :(得分:0)
添加资源
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="feedbackbutton">Button</string>
</resources>