简体   繁体   English

如何将主 class 中的变量导入另一个 class

[英]how to import a variable in Main class to a another class

I have a class that send a message by socket and I want that the message, it's the text that the user put in the plain text that it's supposed to send.我有一个 class 通过套接字发送消息,我想要该消息,它是用户输入应该发送的纯文本的文本。 So I try to catch the message of the plain text in the class but it didn't work if someone knows how to import a variable of the main class to another class.因此,我尝试在 class 中捕获纯文本的消息,但如果有人知道如何将主 class 的变量导入另一个 class,它就不起作用。

this is the code where I execute the class这是我执行 class 的代码



    class client extends AsyncTask<String, Void, Void> {

                Handler handler = new Handler( );

                protected Void doInBackground(String... h) {


                    TextView t3;
                    final EditText send;
                    send = (EditText) findViewById( R.id.editText );
                    t3 = (TextView) findViewById( R.id.textView );

                    try {
                        handler.post( new Runnable() {

                            @Override


                            public void run() {
                                Toast.makeText(getApplicationContext(),"start client", Toast.LENGTH_LONG).show();
                            }
                        } );
                        WifiManager manager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
                        ip = Formatter.formatIpAddress(manager.getConnectionInfo().getIpAddress());
                        String[] mess = h;
                        String messag_send=(mess+"<ip>"+ip);


                        sock = new Socket( "192.168.5.178", 5000 );


                        printWriter = new PrintWriter( sock.getOutputStream() );


                        printWriter.write(messag_send);


                        String line = "no";
                        printWriter.flush();
                        printWriter.close();
                        sock.close();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                }

and to  import 


 client client = new client();

            client.execute(h);



As you can see, you receive your variable h as a vararg .如您所见,您收到变量h作为vararg So h is really an Array of Strings.所以h实际上是一个字符串数组。

protected Void doInBackground(String... h)

However, you don't really access its content, instead you write但是,您并没有真正访问它的内容,而是您编写

String[] mess = h;
String messag_send=(mess+"<ip>"+ip);

Since mess is an array, you embed its toString() value in your message.由于mess是一个数组,因此您将其toString()值嵌入到您的消息中。

What you need to do instead is retrieving the first value of your array (which probably holds only one element anyway) like so相反,您需要做的是检索数组的第一个值(无论如何它可能只包含一个元素),就像这样

String mess = h[0];
String messag_send=(mess+"<ip>"+ip);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM