我有以下幾點:爲什麼setVisibility會導致NullPointerException?
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);
,但沒有運氣。
我錯過了什麼?
什麼是正在選擇的項目的佈局? 此外,爲什麼不遵循[Java命名約定](http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367),它決定了camelCase變量名稱? –
inflating view2是什麼意思? – hakanostrom
將佈局插入到question.view2中,使包含微調控件的commentdialog.xml膨脹。 – roncsak