【问题标题】:Android: Adding OnClick Handler to Custom Preference LayoutAndroid:将 OnClick 处理程序添加到自定义首选项布局
【发布时间】:2014-01-26 03:14:13
【问题描述】:

我在一些未知的领域。不幸的是,我并不完全了解 Android 的结构。我正在尝试为用户创建一种使用首选项布局添加联系人的方法。我让我的应用程序添加联系人,但我正在研究一种从列表中删除联系人的方法。我为我的偏好对象创建了一个自定义布局。

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>

我在添加联系人时动态创建首选项对象。

添加联系人功能:

   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);

    }

我不确定在何处或如何添加要删除的 ImageButton 的 onClickListener。我什至不确定如何删除 Preference 对象。 有什么建议吗?

编辑 2014 年 1 月 26 日 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

在创建联系页面活动时出现此错误。

【问题讨论】:

    标签: android imagebutton preference


    【解决方案1】:

    我最终使用了添加

    android:onClick="deleteContact"
    

    到布局并添加了deleteContact 功能。

    【讨论】:

      【解决方案2】:

      如何设置ImageButton的onClickListener:

      ImageButton deleteButton = (ImageButton) findViewById(R.id.btnDelete);
      deleteButton.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              //code that deletes the Preference object
          }
      });
      

      我没有对 Preference 对象做任何事情,所以我不能告诉你如何从我的头上删除它们。不过,这应该很容易在文档中找到。

      【讨论】:

      • deleteButton.setOnClickListener... 给我一个 NullException 错误 deleteButton 是 NULL 因此给我这个错误。不是如何解决这个问题。
      • 确保包含 ImageButton 的声明。它应该放在 OnCreate 方法中,以便尽快创建。
      • 仍然为空...我应该创建一个自定义偏好类吗?
      • 你能编辑你的帖子并添加 logcat 输出吗?这将帮助我更好地了解正在发生的事情,因为我不明白为什么你应该得到 NullPointerException。
      • 继续添加来自 LOGCAT 的异常
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多