0

有一点问题,因为我想在我的应用程序上让用户输入 IP 地址,并让该 IP 条目用于连接到其他设备。在这两个类中,IPEntry 类被设置为通过 EditText 读取 IP 并将其转换为字符串。然后我希望它在我的 ClientUpload 类中传递和使用。显然,我曾试图解决这个问题,但无济于事。当我以以下方式使用它时,它说它找不到 IP,所以它没有传输。我也试图进入一个方法并调用它,但这也不起作用。有没有办法做到这一点?

谢谢

IPEntry 类

public class IPEntry extends Activity {

Button Submit;
EditText IP;
TextView Thistext;
public String ipadd;
public Intent intent;

@Override
protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.ipentry);
  Submit = (Button) findViewById(R.id.bIPSubmit);
  Thistext = (TextView) findViewById(R.id.tvTextIP);
  IP = (EditText) findViewById(R.id.edIPBar);   

  Submit.setOnClickListener(new View.OnClickListener() {

     @Override
     public void onClick(View v) {
           // TODO Auto-generated method stub

                     ipadd = IP.getText().toString();

           Intent Trans = new Intent("wishift.mat.ANDROIDEXPLORER");
           startActivity(Trans);
                     }              
                     }
  );
}}

客户端上传类的相关部分

public class ClientUpload extends Thread{

IPEntry ipentry = new IPEntry();

public int UploadFile(File file) throws UnknownHostException, IOException
{


  //loop 
  int serverPort = 6880;                  
 //   String ip = "192.168.1.73";
  String ip = ipentry.ipadd;
  Socket socket = new Socket(ip, serverPort);

如您所见,我注释掉了确实有效的部分,但我非常不想在代码中手动添加 IP。

4

1 回答 1

0

这里的问题是这里IPEntry创建的对象:IPEntry ipentry = new IPEntry();在 ClientUpload 中是一个 IPEntry对象;它不共享相同的值ipadd。默认构造函数会将其设置为null.

有很多方法可以解决这个问题;您可以使用StringIP 地址的参数向 ClientUpload 添加构造函数并将其保存在实例变量中,或者向 uploadFile() 方法添加另一个参数以接受 IP 地址。

您还可以设置varible ,允许在所有ipaddIPEntrystatic实例中访问它,尽管我不建议这样做,因为它完全没有必要,并且多个IPEntry对象将需要更多(不必要的)内存和开销。

于 2012-04-25T23:42:31.580 回答