3

我有以下内容:

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);

但没有运气。

我错过了什么?

4

4 回答 4

0

没有setinvisible 不会引起NPE。CommentPaper/CommentField 只是 null 并且您正在尝试使用它

于 2013-10-02T18:52:34.503 回答
0

你正在膨胀commentdialog.xml,所以如果你的CheckBoxEditText在其中,layout那么你需要使用view2来初始化它们

(CheckBox) view2.findViewById(R.id.CommentPaper);

EditText或任何其他View相同layout

于 2013-10-02T18:57:59.213 回答
0

好的,我将我的对话框设置为全局:

public class MainActivity extends Activity {
  private Dialog AddComment_Dialog;
  ..

这样我就可以在任何我想要的地方调用它。所以我只是输入:

AddComment_Dialog.findViewById(R.id.sometext);

我不知道为什么原始代码不起作用,但它已被处理。
谢谢大家!

于 2013-10-02T20:26:05.847 回答
0

我会给你一个例子,我如何在 AlertDialog 中设置复选框的可见性,我知道它与第一篇文章中的不一样,但它可能对某人有所帮助。

            AlertDialog.Builder builder = new  AlertDialog.Builder(this);
            LayoutInflater inflater = this.getLayoutInflater();
            View layout = inflater.inflate(R.layout.my_dialog, null);

            CheckBox checkBox=(CheckBox)layout.findViewById(R.id.checkbox);
            checkBox.setVisibility(View.GONE);

            builder.setView(layout)  //some Böttons
                            .setPositiveButton("Zurück",new DialogInterface.OnClickListener(){
                                @Override
                                public void onClick(DialogInterface dialog,int which) { }});

            AlertDialog alert = builder.create();
            alert.show(); 
于 2014-07-14T21:39:18.503 回答