我有这个MainClass扩展了InputMethodService(缩短版本):
public class PcAsKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener {
private Keyboard mKeyboard;
private KeyboardView mInputView;
private InputMethodManager mInputMethodManager;
@Override
public void onCreate() {
super.onCreate();
mInputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
ServerThread chatServerThread = new ServerThread(this);
chatServerThread.start();
}
public void output(CharSequence text) {
getCurrentInputConnection().commitText(text, 1); // Sends CharSequence "text" to input field
}
}
现在我想访问函数/ void"输出"来自另一个班级(缩短):
public class ClientThread extends Thread {
// the socket where to listen/talk
Socket socket;
InputStream inputStream;
//InputMethodService ims = new InputMethodService();
String message;
BufferedReader bufferedReader;
//private PcAsKeyboard main = null;
// Constructor
ClientThread(Socket socket, PcAsKeyboard main) {
this.socket = socket;
// Creating stream for data input & a reader for the stream to get the message
try
{
inputStream = socket.getInputStream();
InputStreamReader isReader = new InputStreamReader(inputStream);
bufferedReader = new BufferedReader(isReader);
}
catch (IOException e) {
// Exception creating InputStream!
return;
}
}
// Runs forever
public void run() {
while(true) {
// Trying to get a line from the input stream
try {
message = bufferedReader.readLine();
}
catch (IOException e) {
// "Exception reading InputStream!
break;
}
// TODO: Write var message in input Field
// main.output(message);
// ims.getCurrentInputConnection().commitText(message, 1);
}
try {
inputStream = socket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
}
At" // TODO:在输入字段"中写入var消息我想用变量" message"来运行输出函数。 我已经尝试在创建" ClientThread"时将主类(PcAsKeyboard)作为变量传递。类,但这没有工作/给空指针异常。当我尝试访问" InputMethodService"并调用" .getCurrentInputConnection()"然后
我希望你们中的某个人可以帮助我解决这个问题^ - ^