我有以下资料:
private OnItemSelectedListener CommentCodeListener = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.i(LogTAG, "spinner selection: "+ position);
LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View view2=inflater.inflate(R.layout.commentdialog, null);
CheckBox CommentPaper = (CheckBox) view.findViewById(R.id.CommentPaper);
EditText CommentField = (EditText) view.findViewById(R.id.CommentOtherField);
String[] arr = getResources().getStringArray(R.array.CommentCodeListValues);
String CommentCode = arr[position];
int CommentCodeInt = Integer.parseInt(arr[position]);
Log.i(LogTAG, "spinner selection (int): "+ CommentCodeInt);
CommentPaper.setVisibility(View.GONE);
CommentField.setVisibility(View.GONE);
if( (CommentCodeInt >= 21 && CommentCodeInt <= 31) || CommentCodeInt == 41 ) {
Log.i(LogTAG, "set CommentPaper visible");
CommentPaper.setVisibility(View.VISIBLE);
}
if( (CommentCodeInt >=22 && CommentCodeInt <=33) || (CommentCodeInt >= 35 && CommentCodeInt <=36 ) || (CommentCodeInt >= 42 && CommentCodeInt <=43 ) ) {
Log.i(LogTAG, "set CommentPaper visible");
}
if( CommentCodeInt >=41 && CommentCodeInt <=43 ) {
Log.i(LogTAG, "set CommentField visible");
CommentField.setVisibility(View.VISIBLE);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
Log.i(LogTAG, "nothing selected");
}
};
布局:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<Spinner
android:id="@+id/CommentCode_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/MultiLineComment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/commentLabel"
android:inputType="textMultiLine"
android:lines="3"
android:scrollbars="vertical" >
</EditText>
<EditText
android:id="@+id/CommentOtherField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/commentLabel"
android:visibility="visible" >
</EditText>
<CheckBox
android:id="@+id/CommentPaper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/reportCheckBox"
android:visibility="visible" />
<Button
android:id="@+id/AddCommentButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/okButton" />
</LinearLayout>
</LinearLayout>
请注意,上面的布局(commentdialog.xml)是一个对话框。
当我使用微调器选择某个对象时,将调用CommentcodeListener。当setvisibility被注释时,一切都工作。
我试过了
CheckBox CommentPaper = (CheckBox) findViewById(R.id.CommentPaper);
CheckBox CommentPaper = (CheckBox) view.findViewById(R.id.CommentPaper);
CheckBox CommentPaper = (CheckBox) view2.findViewById(R.id.CommentPaper);
但运气不好。
我错过了什么?
最佳答案
没有setinvisible不能引起npe。commentpaper/commentfield为空,您正在尝试使用它