I am on some unknown territory. Unfortunately, I don't fully understand how Android is structured. I am trying to create a way for users to add contacts using a preference layout. I got my app to add contact, but I am working on a way to remove the contact from the list. I created a custom layout for my preference object.
contact_pref_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical" android:paddingRight="?android:attr/scrollbarSize">
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginLeft="15dip"
android:layout_marginRight="6dip" android:layout_marginTop="6dip"
android:layout_marginBottom="6dip" android:layout_weight="1">
<TextView android:id="@+android:id/title"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:singleLine="true" android:textAppearance="?android:attr/textAppearanceLarge"
android:ellipsize="marquee" android:fadingEdge="horizontal"
android:textColor="#FF0000" />
<TextView android:id="@+android:id/summary"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@android:id/title" android:layout_alignLeft="@android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:maxLines="4" />
<ImageButton android:id="@+id/btnDelete"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentRight="true"
android:src="@drawable/trash_can"
/>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout android:id="@+android:id/widget_frame"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:gravity="center_vertical" android:orientation="vertical" />
</RelativeLayout>
</LinearLayout>
I am dynamically creating the preference object everything I add a contact.
Add Contact Function:
private void addContact(String contactName, String contactPhone)
{
Log.i(TAG,"in addContact Function");
Toast.makeText(this, contactName + "'s phone number found: " + contactPhone , Toast.LENGTH_SHORT).show();
PreferenceCategory targetCategory = (PreferenceCategory)findPreference("contact_category");
Preference newContact = new Preference(this);
newContact.setKey("contact123");
newContact.setTitle(contactName);
newContact.setSummary(contactPhone);
newContact.setLayoutResource(R.layout.contact_pref_layout);
Log.i(TAG,"Add new Contact");
targetCategory.addPreference(newContact);
}
I am not sure where or how to add the onClickListener for the ImageButton to delete. I am not sure even sure how to delete Preference object either. Any suggestions?
EDIT 1/26/2014 LOGCAT
01-26 08:22:36.846: E/AndroidRuntime(18987): FATAL EXCEPTION: main
01-26 08:22:36.846: E/AndroidRuntime(18987): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simpleemergencywidget/com.example.simpleemergencywidget.ContactPrefActivity}: java.lang.NullPointerException
01-26 08:22:36.846: E/AndroidRuntime(18987): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
01-26 08:22:36.846: E/AndroidRuntime(18987): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
01-26 08:22:36.846: E/AndroidRuntime(18987): at android.app.ActivityThread.access$700(ActivityThread.java:165)
01-26 08:22:36.846: E/AndroidRuntime(18987): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
01-26 08:22:36.846: E/AndroidRuntime(18987): at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 08:22:36.846: E/AndroidRuntime(18987): at android.os.Looper.loop(Looper.java:137)
01-26 08:22:36.846: E/AndroidRuntime(18987): at android.app.ActivityThread.main(ActivityThread.java:5455)
01-26 08:22:36.846: E/AndroidRuntime(18987): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 08:22:36.846: E/AndroidRuntime(18987): at java.lang.reflect.Method.invoke(Method.java:525)
01-26 08:22:36.846: E/AndroidRuntime(18987): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
01-26 08:22:36.846: E/AndroidRuntime(18987): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
01-26 08:22:36.846: E/AndroidRuntime(18987): at dalvik.system.NativeStart.main(Native Method)
01-26 08:22:36.846: E/AndroidRuntime(18987): Caused by: java.lang.NullPointerException
01-26 08:22:36.846: E/AndroidRuntime(18987): at com.example.simpleemergencywidget.ContactPrefActivity.onCreate(ContactPrefActivity.java:36)
01-26 08:22:36.846: E/AndroidRuntime(18987): at android.app.Activity.performCreate(Activity.java:5372)
01-26 08:22:36.846: E/AndroidRuntime(18987): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
01-26 08:22:36.846: E/AndroidRuntime(18987): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
01-26 08:22:36.846: E/AndroidRuntime(18987): ... 11 more
I get this error when contactpage actvity is being created.
I ended up using adding
android:onClick="deleteContact"
to the layout and added a deleteContact function.