【问题标题】:Android Button Click Not WorkingAndroid按钮单击不起作用
【发布时间】:2012-01-29 04:39:31
【问题描述】:

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

日志显示按钮的 ID,因此可以找到一些东西。按钮和标签位于 TableLayout 中,而 LinearLayout 位于 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 怎么样?这行得通吗?

标签: java android binding buttonclick


【解决方案1】:

删除这个标题.findViewById(R.id.btnSettings);

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

//你没有膨胀任何布局,你的内容视图已经有布局contact_list.xml,只有你的按钮和标签在那里。

【讨论】:

  • 嘿,我尝试删除header 部分,事实上,我添加了header 部分,因为之前我只是调用findViewById() 时它不起作用。我在ListView 中有其他按钮。
  • 您关于“膨胀布局”的评论实际上指出了与我在代码中进行的刷新有关的问题。所以虽然我的问题没有答案,但你得到了分数:)
【解决方案2】:

你已经用它来映射你的按钮

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

但尽量用这种方式

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

【讨论】:

  • 嘿,我尝试删除 header 部分,事实上,我添加了 header 部分,因为之前我只是调用 findViewById() 时它不起作用
【解决方案3】:

如果在labelHeader.setText("Jump");这一行加断点是labelHeader为空?

也可以代替new View.OnClickListener() 试试new OnClickListener()

【讨论】:

    猜你喜欢
    • 2016-11-09
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 2016-06-07
    相关资源
    最近更新 更多