Java 默认构造函数无法处理隐式超级构造函数引发的异常类型IOException。必须定义显式构造函数
我有两个java类Configuration.java和login.java Configuration.java login.java 有人能帮我解决这个错误吗 还可以按如下方式更改配置文件:Java 默认构造函数无法处理隐式超级构造函数引发的异常类型IOException。必须定义显式构造函数,java,android,Java,Android,我有两个java类Configuration.java和login.java Configuration.java login.java 有人能帮我解决这个错误吗 还可以按如下方式更改配置文件: public class Configuration { public Configuration() {} public String[] getparams() throws IOException { Properties properties = new Pr
public class Configuration {
public Configuration() {}
public String[] getparams() throws IOException {
Properties properties = new Properties();
FileInputStream fileStream = new FileInputStream("C:/.../Desktop/configuration.txt");
try {
properties.load(fileStream);
String ip = (String) properties.get("IP");
String port = (String) properties.get("Port");
return new String[]{ip, port};
} finally {
try {
fileStream.close();
} catch (IOException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
}
}
}
}
为什么不将getParam的签名更改为不抛出错误呢。我看到您这样做部分是为了调用properties.load() 比如: 公共类配置{
public Configuration() {}
public String getparams() {
Properties properties = new Properties();
FileInputStream fileStream = null;
try {
fileStream = new FileInputStream("C:/.../Desktop/configuration.txt");
properties.load(fileStream);
String ip = (String) properties.get("IP");
String port = (String) properties.get("Port");
return ip + port;
}
catch(IOException ex)
{
return null;
}
finally {
try {
fileStream.close();
} catch (IOException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
}
}
}
}
如果try块也可以抛出,您可能希望将新属性移动到try块中。您从哪里得到消息?我在Login中调用getparams()时出错。java您可以向Kushtrim显示
Login.java
?p:我必须将您的建议放在方法中吗?getparams()方法会抛出IOException,因此无论您在哪里调用它,您应该用try-catch语句将其包围起来。因此,只需将try-catch添加到login.java中的代码中,因为我知道try-catch bloc必须在methode中,因为当我在没有methode的情况下执行该操作时,在标记“;”,{在该标记之后应为“”,是的,您应该在onCreate(…)上初始化变量方法谢谢你的回复只是一个提示:在我剩下的代码中,LOGIN_URL无法解析为变量!你是指getParam返回的URL吗?
public class Login extends Activity implements OnClickListener {
Configuration cfg;
String ip;
String port;
private String LOGIN_URL;
//other variables
protected void onCreate(Bundle savedInstanceState) {
//here you initialize your variables
try {
cfg = new Configuration();
String[] params = cfg.getparams();
ip = params[0];
port = params[1];
LOGIN_URL = "http://"+ ip +":"+port+"/webservice/login.php";
} catch (IOException e) {
//handle the exception
}
//.. your other code ...
}
//... your other methods ...
}
public class Configuration {
public Configuration() {}
public String[] getparams() throws IOException {
Properties properties = new Properties();
FileInputStream fileStream = new FileInputStream("C:/.../Desktop/configuration.txt");
try {
properties.load(fileStream);
String ip = (String) properties.get("IP");
String port = (String) properties.get("Port");
return new String[]{ip, port};
} finally {
try {
fileStream.close();
} catch (IOException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public Configuration() {}
public String getparams() {
Properties properties = new Properties();
FileInputStream fileStream = null;
try {
fileStream = new FileInputStream("C:/.../Desktop/configuration.txt");
properties.load(fileStream);
String ip = (String) properties.get("IP");
String port = (String) properties.get("Port");
return ip + port;
}
catch(IOException ex)
{
return null;
}
finally {
try {
fileStream.close();
} catch (IOException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
}
}
}