我正在尝试使用android java编写一个带有AIDE的应用程序,我需要的最终结果是一个使用滑块的应用程序,当我释放滑块时,与滑块位置关联的数字IP"在这里插入IP"和端口"在这里插入端口",但是我从不同的类中提取值,我需要它来写入方法内部的值,如何将变量拉出方法?
public void sendPort(int port){
//TO-DO take port variable and input into sendIp
}
里面的
public void sendIp(String ip){
//needs the port variable in here
}
但是当我尝试时,它告诉我我的代码中有错误并且无法编译,直到错误得到修复,我得到的错误说有一个" Missing' }'"之后是一个"意外的'}'"。是否存在类似公共空白或检测到大括号的方法的脚本? 菜单的代码
public void showChangeLangDialog() {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView = inflater inflate(R.layout.custom_dialog, null);
dialogBuilder.setView(dialogView);
final EditText ip = (EditText) dialogView.findViewById(R.id.edit1);
final EditText edt = (EditText) dialogView.findViewById(R.id.edit2);
dialogBuilder.setTitle("Custom dialog");
dialogBuilder.setMessage("Enter text below");
dialogBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String port = edt.getText().toString();
mTcpClient.sendIp(String.valueOf(ip));
mTcpClient.sendPort(Integer.valueOf(port));
//do something with edt.getText().toString();
}
});
dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//pass
}
});
AlertDialog b = dialogBuilder.create();
b.show();
}
检索变量并发送滑块位置编号的代码
public void sendMessage(String message){
if (out != null && !out.checkError()) {
out.println(message);
out.flush();
out.close();
}
}
public void sendPort(int port){
Log.i("TCPClient", "Recieved Port");
int SERVERPORT = 0 + port;
public void sendIp(String ip){
Log.i("TCPClient", "Recieved IP");
String SERVERIP = "" + ip;
try {
InetAddress serverAddr = InetAddress.getByName(SERVERIP);
Log.e("TCP Client", "C: Connecting...");
Socket socket = new Socket(serverAddr, SERVERPORT);
try {
//send the message to the server
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
Log.e("TCP Client", "C: Sent.");
Log.e("TCP Client", "C: Done.");
//receive the message which the server sends back
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//in this while the client listens for the messages sent by the server
while (mRun) {
serverMessage = in.readLine();
if (serverMessage != null && mMessageListener != null) {
//call the method messageReceived from MyActivity class
mMessageListener.messageReceived(serverMessage);
}
serverMessage = null;
}
Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + serverMessage + "'");
} catch (Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
//the socket must be closed. It is not possible to reconnect to this socket
// after it is closed, which means a new socket instance has to be created.
socket.close();
}
} catch (Exception e) {
Log.e("TCP", "C: Error", e);
}
}
//Declare the interface. The method messageReceived(String message) will must be implemented in the MyActivity
//class at on asynckTask doInBackground
public interface OnMessageReceived {
public void messageReceived(String message);
}
}
我从另一个类的菜单中抓取端口和IP的值,你看到的第二个脚本是一个不是活动的类,它只是一个背景类。
答案 0 :(得分:2)
在Java中,您无法在另一个方法中声明新方法。如果你需要两个单独的方法,在你的类中单独声明它们,然后在另一个中调用一个,如下所示:
public void sendPort(int port){
//some sort of code here
}
public void sendIp(String ip){
//some code
sendPort(portAsInt);
//more code
}
如果您不需要两种不同的方法,那么只需在sendPort
方法中包含sendIp
方法中的逻辑。