安卓按钮点击无效

4

有人能告诉我为什么从“开始新代码”开始的代码不起作用吗?单击事件侦听器中的onClick方法没有被调用,甚至标签的文本更改也没有发生。

日志显示按钮的ID,因此可以找到某些内容。按钮和标签位于TableLayout中,该表格布局位于LinearLayout中。

在我绑定按钮的onCreate方法中。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.contact_list);

    setDefaultKeyMode(DEFAULT_KEYS_SHORTCUT);

    mContactList = (ListView) findViewById(R.id.contactList);
    mInputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    mApplicationData = (GlobalApplicationData) getApplication();

    setTitle(mApplicationData.getAppTitle());
    mSettings = mApplicationData.getSettings();
    initializedEncryptionKey();

    mContactsDB = mApplicationData.getContactsDB();

    mXmppModalUI =  new XmppModalUI(mInputManager);
    mXmppModalUI.initializeModalUI(this, mSettings, this);

    mApplicationData.getPersistentXMPP().setModalUI(mXmppModalUI);
    mApplicationData.getPersistentXMPP().setContactListCallback(this);

    mApprater = new Appirater(this, mSettings);

    if (mApplicationData.getUpdateChecker() == null && mApplicationData.getUpdateCheckerUrl() != null) {
        mApplicationData.setUpdateChecker(new UpdateChecker(getApplicationContext(), mApplicationData.getUpdateCheckerUrl()));
    }

    //starting new code
    Toast.makeText(getApplicationContext(), "Starting Ali's Code", Toast.LENGTH_SHORT).show();

    TableLayout header = (TableLayout) findViewById(R.id.headerLayout);
    Log.d("TableLayout",((header == null)?"NOT FOUND":"FOUND "+header.getId()));

    final TextView labelHeader = (TextView) header.findViewById(R.id.headerText);
    labelHeader.setText("Jump");

    final ImageButton btnSettings = (ImageButton) header.findViewById(R.id.btnSettings);
    btnSettings.setClickable(true);
    Log.d("Settings Button",((btnSettings == null)?"NOT FOUND":"FOUND "+btnSettings.getId()));
    btnSettings.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            Log.d("OnClick","Here");
            // Perform action on clicks
            Toast.makeText(getApplicationContext(), "Settings", Toast.LENGTH_SHORT).show();
            startGlobalSettings();
        }
    });

    final ImageButton btnAdd = (ImageButton) header.findViewById(R.id.btnAdd);
    btnAdd.setClickable(true);
    Log.d("Add Button",((btnAdd == null)?"NOT FOUND":"FOUND "+btnAdd.getId()));
    btnAdd.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            Log.d("OnClick","Here");
            // Perform action on clicks
            Toast.makeText(getApplicationContext(), "ADD", Toast.LENGTH_SHORT).show();
            showDialog(DialogIds.ADD_CONTACT);
        }
    });

}

布局(contact_list.xml):
<TableLayout
    android:id="@+id/headerLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1" >

    <TableRow>

        <ImageButton
            android:id="@+id/btnSettings" 
            android:gravity="left"
            android:padding="3dip"
            android:src="@drawable/ic_menu_add"
            android:hint="@string/label_settings" />

        <TextView
            android:id="@+id/headerText"
            android:gravity="center"
            android:padding="3dip"
            android:text="Blank" 
            android:height="50dip"/>

        <ImageButton
            android:id="@+id/btnAdd"
            android:background="@drawable/disconnectbutton"
            android:gravity="right"
            android:padding="3dip"
            android:src="@drawable/ic_menu_add"
            android:hint="@string/label_add" />

    </TableRow>

</TableLayout>

<ListView
    android:id="@+id/contactList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:background="@color/transparent"
    android:cacheColorHint="#00000000" >
</ListView>

更新 我尝试移除了header部分,实际上,我添加了header部分是因为之前仅使用findViewById()时无法正常工作。


在 XML 上设置 android:onClick,这个方法可行吗? - Dhanush Gopinath
3个回答

1

移除此 标题。findViewById(R.id.btnSettings);

ImageButton btnSettings = (ImageButton)findViewById(R.id.btnSettings);

//你没有膨胀任何布局,已经在你的内容视图中有一个名为contact_list.xml的布局,在这个布局中只有你的按钮和标签。


嘿,我尝试删除“header”部分,事实上,我添加了“header”部分,因为之前当我仅调用“findViewById()”时它无法工作。我在“ListView”中还有其他按钮。 - Ali
1
你有关“填充布局”的评论实际上指向了我代码中正在进行的刷新操作,虽然它不是我问题的答案,但你可以得分 :) - Ali

1

你已经使用了这个来映射你的按钮

final ImageButton btnSettings = (ImageButton) header.findViewById(R.id.btnSettings);

但尝试以这种方式使用

final ImageButton btnSettings = (ImageButton)findViewById(R.id.btnSettings);

嘿,我试过去掉header部分,事实上,我添加了header部分是因为之前当我只是调用findViewById()时它不起作用。 - Ali

0

如果您在labelHeader.setText("Jump");代码行上添加了断点,那么labelHeader是null吗?

另外,尝试使用new OnClickListener()代替new View.OnClickListener()


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接