Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在扩展InputMethodService的类中运行函数/void_Java_Android_Class - Fatal编程技术网

Java 在扩展InputMethodService的类中运行函数/void

Java 在扩展InputMethodService的类中运行函数/void,java,android,class,Java,Android,Class,我有一个扩展InputMethodService的MainClass(简称版本): 现在我想从另一个类访问函数/void“output”(缩写): 在“//TODO:在输入字段“我想用变量“message”运行输出函数”中写入var消息。 在创建“ClientThread”类时,我已经尝试将主类(PcAsKeyboard)作为变量进行传输,但这不起作用/出现空指针异常。当我试图访问“InputMethodService”并调用“.getCurrentInputConnection()”时,也发生

我有一个扩展InputMethodService的MainClass(简称版本):

现在我想从另一个类访问函数/void“output”(缩写):

在“//TODO:在输入字段“我想用变量“message”运行输出函数”中写入var消息。 在创建“ClientThread”类时,我已经尝试将主类(PcAsKeyboard)作为变量进行传输,但这不起作用/出现空指针异常。当我试图访问“InputMethodService”并调用“.getCurrentInputConnection()”时,也发生了这种情况


我希望你们中有人能帮我解决这个问题^-^

我想在第一段代码中,您是在创建
ClientThread
而不是
ServerThread
?添加
this.main=main有什么问题
ClientThread
构造函数中?@AntonSavin噢,实际上首先创建了ServerThread,后者随后创建了ClientThread。似乎我在ClientThread construcor中忘记了这一点-愚蠢的我,在你忘记传递
main
的路上的某个地方。是的,请看我编辑的评论
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
    }
}
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();
        }

    }

}