先上代码:TextView labelView;
CheckBox checkBox;
labelView = (TextView)findViewById(R.id.label);
        checkBox = (CheckBox)findViewById(R.id.block);
        if(checkBox==null)System.out.println("checkBox为空!");
        if(labelView==null)System.out.println("labelView空!");
如上一个TextView和一个CheckView的使用,发现checkview那句有打印……为什么check一直是空的?

解决方案 »

  1.   

    发错了,是打印出labelView那句,但没有checkBox那句!
      

  2.   

    呀。我完全搞错了==!请各位原谅我傻逼……
    是只打印出checkbox为空那句话,其他的没有了。
      

  3.   

    完整代码看看,还有layout文件发来看看
      

  4.   

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello" />
        <EditText 
            android:id="@+id/entry"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
        <CheckBox
                android="@+id/block"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="返回true,阻止将按键时间传递给界面元素"/>
        <TextView 
            android:id="@+id/label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按键事件信息"/>
    </LinearLayout>
    这是layout代码!
      

  5.   

    package my.test.UIEvent;import android.app.Activity;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.View;
    import android.view.View.OnKeyListener;
    import android.widget.CheckBox;
    import android.widget.EditText;
    import android.widget.TextView;public class KeyEventActivity extends Activity {    EditText entryText;
        TextView labelView;
        CheckBox checkBox;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            entryText = (EditText)findViewById(R.id.entry);
            labelView = (TextView)findViewById(R.id.label);
            checkBox = (CheckBox)findViewById(R.id.block);
            if(checkBox==null)System.out.println("checkBox为空!");
            if(labelView==null)System.out.println("labelView为空!");
            entryText.setOnKeyListener(new OnKeyListener()
            {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent keyEvent) 
    {
    int metaState = keyEvent.getMetaState();
    int unicodeChar = keyEvent.getUnicodeChar();
    String msg="";
    msg += "按键动作:"+String.valueOf(keyEvent.getAction())+"\n";
    msg += "按键代码:"+String.valueOf(keyCode)+"\n";
    msg += "按键字符:"+(char)unicodeChar+"\n";
    msg += "UNICODE:"+String.valueOf(unicodeChar)+"\n";
    msg += "重复次数:"+String.valueOf(keyEvent.getRepeatCount())+"\n";
    msg += "功能键状态:"+String.valueOf(metaState)+"\n";
    msg += "硬件编码:"+String.valueOf(keyEvent.getScanCode())+"\n";
    msg += "按键标志:"+String.valueOf(keyEvent.getFlags())+"\n";
    labelView.setText(msg);
    /*if(checkBox.isChecked())
    return true;
    else*/
    // TODO Auto-generated method stub
    return false;
    }
            });
        }
    }
    完整代码!
      

  6.   

    @hvk697
    换ID是什么意思?我把ID名改成block1运行之后还是一样的诡异……为什么只有checkbox没能赋值上,其他的像TextView和EditText都能赋值呢~我查了checkbox的SDK,使用方法也是通过这个findviewbyid()的方法来赋值的。现在搞不懂难道是我装的软件版本问题?
    有没有人用我代码在自己机上试试?看看是不是只有我出现了这个问题!
      

  7.   

    layout里缺少id,看仔细了<CheckBox
          android="@+id/block"
          
    ->
    <CheckBox
          android:id="@+id/block"
      

  8.   

    谢谢楼上正解!
    不过这eclipse平时对错误那么敏感~这个时候却偷懒……