4

Can someone tell me why the code from "Starting new code" onwards does not work? The onClick method in the click event listeners does not get called and even the text change for the label does not take place.

The log shows ID's for the buttons so something is being found. The buttons and label are in a TableLayout which is inside a LinearLayout.

The onCreate method where I'm binding the buttons.

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

}

The layout (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>

UPDATE I tried removing the header part, as a matter of fact, I add the header part because it wasn't working before when I simply called findViewById()

Ali
  • 12,354
  • 9
  • 54
  • 83

3 Answers3

1

remove this header.findViewById(R.id.btnSettings);

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

//you are not inflating the any layout, already your content view has layout contact_list.xml in this only your buttons and label are there.

Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
  • Hey, I tried removing the `header` part, as a matter of fact, I add the `header` part because it wasn't working before when I simply called `findViewById()`. I have other buttons in the `ListView`. – Ali Dec 28 '11 at 13:44
  • 1
    your comment about "inflating the layout" actually point me to the problem which had to do with an refresh I was doing in my code. So while no the answer to my problem, you get the points :) – Ali Dec 28 '11 at 14:08
1

you have used this to mapped with your button

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

but try to use in this way

final ImageButton btnSettings = (ImageButton)findViewById(R.id.btnSettings);
Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
Silvans Solanki
  • 1,267
  • 1
  • 14
  • 27
  • Hey, I tried removing the `header` part, as a matter of fact, I add the `header` part because it wasn't working before when I simply called `findViewById()` – Ali Dec 28 '11 at 13:44
0

if you add a breakpoint on the line of labelHeader.setText("Jump"); is labelHeader null?

Also instead of new View.OnClickListener() try new OnClickListener()

Adel Boutros
  • 10,205
  • 7
  • 55
  • 89