繁体   English   中英

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

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

我有一个 class 通过套接字发送消息,我想要该消息,它是用户输入应该发送的纯文本的文本。 因此,我尝试在 class 中捕获纯文本的消息,但如果有人知道如何将主 class 的变量导入另一个 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);



如您所见,您收到变量h作为vararg 所以h实际上是一个字符串数组。

protected Void doInBackground(String... h)

但是,您并没有真正访问它的内容,而是您编写

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

由于mess是一个数组,因此您将其toString()值嵌入到您的消息中。

相反,您需要做的是检索数组的第一个值(无论如何它可能只包含一个元素),就像这样

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

暂无
暂无

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

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