2
对于Android应用程序,我试图使用微调,当选择一个选项时,将隐藏/显示相关的视图对象。对于我的应用程序,这些对象是该字段的EditText和关联的TextView标签。不幸的是,我似乎无法得到EditText来隐藏/显示,当我添加代码来隐藏/显示TextView时,我得到一个NullPointerException。我认为,因为我在一个RelativeLayout中布置视图对象,通过隐藏其中一个视图对象,我将删除它与其他视图对象的关系,因此是NullPointer。Android - 如何使用微调来隐藏/显示查看对象
任何人都可以找出为什么会发生这种情况吗?这里是我的代码:
public class FormFields extends Activity {
private Spinner mSpinner;
private EditText mTextField;
private TextView mLabel;
private static final int SPINNER_OPTION_FIRST = 0;
private static final int SPINNER_OPTION_SECOND = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form_fields);
mTextField = (EditText) findViewById(R.id.text_field);
mLabel = (TextView) findViewById(R.id.field_label)
mSpinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(
this, R.array.spinnerOptions, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter1);
mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
switch(position) {
case SPINNER_OPTION_FIRST: {
mLabel.setVisibility(View.GONE);
mTextField.setVisibility(View.GONE);
}
case SPINNER_OPTION_SECOND: {
mLabel.setVisibility(View.VISIBLE);
mTextField.setVisibility(View.VISIBLE);
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// Do nothing
}
});
}
}
form_fields.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#104667">
<TextView
android:id="@+id/spinner_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dip"
android:textStyle="bold"
android:text="Please select an option" />
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner_label"
android:layout_marginLeft="25dip"
android:layout_marginRight="25dip"
android:drawSelectorOnTop="true"
android:prompt="@string/spinnerPrompt" />
<TextView
android:id="@+id/field_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dip"
android:textStyle="bold"
android:text="Enter text here: "
android:visibility="gone" />
<EditText
android:id="@+id/text_field"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dip"
android:layout_marginRight="25dip"
android:layout_below="@+id/field_label"
android:visibility="gone" />
</RelativeLayout>
</ScrollView>
谢谢一堆。由于这段代码是从我的实际应用程序稍微调整的,分号不是实际问题(否则,我的应用程序不会编译)。实际上,我根本没有设置我的mLabel成员变量!去搞清楚。我很好奇 - break语句的目的是什么? – Keeb13r 2010-09-26 05:51:50
没有'break;'语句将每个'case'语句分隔开来,执行“通过”连续到每个'case'语句。有些原因可能会导致这种情况的发生,但通常使用'switch'模块来提供替代选项,每个'case'只应该运行该特定条件的代码。有关'switch'语句的一些示例和详细解释,请参阅:http://download.oracle.com/javase/tutorial/java/nutsandbolts/switch.html – Brian 2010-09-26 21:39:52