I´m using a Button and checkbox in my code. I want that when button is clicked then it make the checkbox visibles, but if I click on button then error is coming in logcat thatr null pointer exception.
Here is the code:
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);
}
}
Here Is xml:
<LinearLayout
xmlns:android="http://ift.tt/nIICcg"
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>
Here Is Xml:
<LinearLayout
xmlns:android="http://ift.tt/nIICcg"
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>
Aucun commentaire:
Enregistrer un commentaire