【问题标题】:Android visibility issue with Button?Button 的 Android 可见性问题?
【发布时间】:2015-07-14 19:28:29
【问题描述】:

我在我的代码中使用了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>

【问题讨论】:

  • no logcat...它在哪里

标签: android button layout checkbox visibility


【解决方案1】:

chk变量是null所以使用findViewById来初始化它。

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

【讨论】:

    【解决方案2】:

    为可见和不可见做View

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

    还初始化了你的 CheckBox 喜欢

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

    【讨论】:

      【解决方案3】:

      你只是忘记inflate the CheckBox

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

      【讨论】:

        【解决方案4】:

        您的 chk 变量为空 - 使用 findViewById 对其进行初始化。

        【讨论】:

          【解决方案5】:

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-02-21
            • 2011-01-31
            • 1970-01-01
            • 1970-01-01
            • 2012-06-15
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多