2011-12-28 215 views
4

有人能告訴我爲什麼從「啓動新代碼」開始的代碼不起作用? 點擊事件偵聽器中的onClick方法不會被調用,甚至標籤的文本更改也不會發生。Android Button Click Not Working

日誌顯示按鈕的ID,因此可以找到某物。按鈕和標籤位於LinearLayout內的TableLayout

我綁定按鈕的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> 

UPDATE 我試圖消除header一部分,事實上,我添加header部分,因爲它在我簡化之前不工作ly叫findViewById()

+0

如何設置安卓:對XML的onClick?那樣有用嗎? – 2011-12-28 13:32:51

回答

1

刪除此標頭 .findViewById(R.id.btnSettings);

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

//你是不是誇大的任何佈局,已經在你的內容觀點有佈局contact_list.xml在這個只有你的按鈕和標籤的存在。

+0

嗨,我試着刪除'header'部分,事實上,我添加了'header'部分,因爲在我簡單地調用'findViewById()'之前它不工作。我在ListView中有其他按鈕。 – Ali 2011-12-28 13:44:56

+1

你對「膨脹的佈局」的評論實際上指向了我在我的代碼中進行刷新所必須做的問題。所以雖然沒有我的問題的答案,你得到的要點:) – Ali 2011-12-28 14:08:20

+0

謝謝有一個快樂的編碼:-) – 2011-12-28 14:11:18

1

您使用此與您的按鈕

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

映射,但是嘗試,如果你的labelHeader.setText("Jump");行添加一個斷點這樣

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

乾草Solanki很好歡迎到Stackoverflow。繼續晃動.... – 2011-12-28 13:36:04

+0

嘿,我試圖刪除'header'部分,事實上,我添加了'header'部分,因爲它不工作之前,我只是簡單地調用'findViewById()' – Ali 2011-12-28 13:44:44

0

使用的是labelHeader空?

也代替new View.OnClickListener()嘗試new OnClickListener()