2011-12-28 128 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()