Question

I am trying to use QuickContactBadge in custom dialog. When I try to modify qcb, it always crash, but I can´t find why. Can anyone help? Thank you in advance.

custom_xml.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<QuickContactBadge 
android:id="@+id/quickContactBadge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/quick_contact_badge"
android:padding="5dp"
></QuickContactBadge>
</RelativeLayout>

Java code:

Dialog dialog = new Dialog(NameOfActivity.this);
dialog.setContentView(R.layout.custom_xml);
dialog.show();

QuickContactBadge qcb = (QuickContactBadge) findViewById (R.id.quickContactBadge); 
//This make app always crash
qcb.setMode(ContactsContract.QuickContact.MODE_LARGE); 
qcb.assignContactFromPhone("12345678", true);  
qcb.setImageResource(R.drawable.icon);
Was it helpful?

Solution

You must be getting a NullPointerException. Call findViewById() on dialog object.

QuickContactBadge qcb = (QuickContactBadge) 
                                 dialog.findViewById (R.id.quickContactBadge);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top