2013-02-26 39 views
0

我有活動課如何在活動中使用InputMethodService?

public class Login extends Activity 

創建類的內部登錄類

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): FATAL EXCEPTION: main 02-26 21:28:18.903: E/AndroidRuntime(747): java.lang.NullPointerException

回答

-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); 
} 
相關問題