0

我有活动课

public class Login extends Activity  

我在 Login 类中创建类

public  class Keyclass extends InputMethodService
    {
        void addtext()
        {
            InputConnection ic =  getCurrentInputConnection();
             ic.commitText("1", 1);
        }
    }

并尝试在 onClick 事件中使用它:

Button but1=(Button) findViewById(R.id.button1);
         but1.setOnClickListener(new OnClickListener() 
            {
                @Override
                public void onClick(View arg0) 
                {
                    Keyclass kk= new Keyclass();
                    kk.onkey();
                }
            });

但得到错误:

02-26 21:28:18.903: E/AndroidRuntime(747): 致命异常: main 02-26 21:28:18.903: E/AndroidRuntime(747): java.lang.NullPointerException

4

1 回答 1

-1
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {

        // do what you want with the power button           
        Log.i(TAG,"PowerButton got clicked!");

        return true;
    }
    return super.onKeyDown(keyCode, event);
}
于 2013-08-19T11:55:53.523 回答