Button的Android可见性问题?

时间:2015-05-05 07:23:12

标签: android button layout checkbox visibility

我在代码中使用了ButtonCheckBox。我想在单击按钮以使复选框可见时,但是当我单击此按钮时,此错误将出现在logcat中:NullExceptionPointer

以下是代码:

public class FriendListActivity extends Activity {

    SimpleCursorAdapter adapter;
    ListView lvContacts;
    Button b1;
    CheckBox chk;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_friend_listview);
        getcontacts();

        b1 = (Button)findViewById(R.id.btn_invite);

        b1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {



                // Perform action on clicks, depending on whether it's now checked
                if (((Button) v).isClickable()) {

                    int visible = 0;
                    chk.setVisibility(visible);


                } else {

                    int invisible = 0;
                    chk.setVisibility(invisible);


                }
            }
        });

}

    @SuppressWarnings("deprecation")
    protected void getcontacts() {


        lvContacts = (ListView) findViewById(R.id.lv_friend_list);
        ContentResolver cr = getContentResolver();

        // Read Contacts
        Cursor c = cr.query(ContactsContract.Contacts.CONTENT_URI,
        new String[] { ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME }, null, null,
                null);

        // Attached with cursor with Adapter
        adapter = new SimpleCursorAdapter(this, R.layout.activity_phonecontact,
                c, new String[] { ContactsContract.Contacts.DISPLAY_NAME },
                new int[] { R.id.tv_name });
               lvContacts.setAdapter(adapter);



    }
}

这是Xml:

 <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="1"
        android:gravity="center_vertical">


        <ImageView
            android:id="@+id/img_frnd_list"
            android:layout_width="0dp"
            android:layout_weight="0.2"
            android:layout_height="wrap_content"
            android:background="@null"
            android:contentDescription="@string/imageView_contact_image"
            android:paddingBottom="10dp"
            android:paddingLeft="10dp"
            android:paddingTop="10dp"
            android:src="@drawable/contactimage" />


       <TextView
            android:id="@+id/tv_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.6"
            android:paddingLeft="10dp"
            android:text="@string/Contact_Name"
            android:textSize="@dimen/contact_text_size" />

       <FrameLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2" >

            <Button
                android:id="@+id/btn_invite"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/invite"
                android:background="@color/orng"
                android:textColor="@color/white">

        </Button>


             <CheckBox
            android:id="@+id/chk_frnd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="invisible"/>

        </FrameLayout> 

    </LinearLayout>

5 个答案:

答案 0 :(得分:2)

chk变量为null因此请使用 findViewById 对其进行初始化。

chk= (CheckBox)findViewById(R.id.chk_id);

答案 1 :(得分:1)

对于可见和不可见的View

chk.setVisibility(View.VISIBLE);
chk.setVisibility(View.INVISIBLE);

还初始化 CheckBox ,如

chk= (CheckBox)findViewById(R.id.chk_id);

答案 2 :(得分:0)

忘记inflate the CheckBox

chk= (CheckBox)findViewById(R.id.chk_id);

答案 3 :(得分:0)

您的chk变量为空 - 使用findViewById初始化它。

答案 4 :(得分:0)

您没有在代码中初始化复选框。请在onCreate方法中插入以下代码。

chk=(CheckBox)findViewwByid(R.id.chk_frnd);