-1

* R.id 字段显示错误,因此在保存时不会像在 xml 中那样在任何字段中显示。主要活动类代码发布在请帮助下方,因为 m 仅停留在此中。清理和构建 R.java 类正在消失。它显示没有找到与给定名称匹配的资源(在“文本”处,值为“@string/feedbackbutton”)以及 * 中的其他字段

主要活动.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>

数组.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>

字符串.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>
4

2 回答 2

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 中定义了您在布局 xml 中使用的所有字符串

于 2013-09-10T14:07:55.000 回答
0

添加资源

<?xml version="1.0" encoding="utf-8"?>
<resources>

   <string name="feedbackbutton">Button</string>
</resources>
于 2013-09-10T14:09:52.887 回答