-2
我创建了一个类TFTPClient
,它允许用户连接到服务器并能够通过类方法发出某些请求。我已经在Android Studio之外创建并测试了这个,但是当我转移并编译我的代码时,出现错误。该错误是在MainActivity.java创建类对象时,但调用某些方法DatagramSocket - java.lang.NullPointerException [Andriod]
TFTPClient c = new TFTPClient(); //Works fine
try {
c.sendAndReceive(); //Get the error
} catch (UnknownHostException e) {
e.printStackTrace();
}
这里引起的,但不能是sendAndReceive方法的代码
public void sendAndReceive() throws UnknownHostException {
int sendPort;
sendPort = SERVER_RECV_PORT; //(60700)
int clientConnectionPort = -1;
InetAddress serverAddress = InetAddress.getByName("172.17.49.153");
try {
clientConnectionPort = sendRequest(serverAddress, sendPort, "all");
}
catch (IOException e) {
//System.out.println("error");
}
receive(serverAddress, clientConnectionPort);
}
你错了。 [你仍然需要互联网许可](https://developer.android.com/training/basics/network-ops/connecting.html)。即使是最新的文档也说明了这一点。 [互联网许可证上的文档说它不被弃用](https://developer.android.com/reference/android/Manifest.permission.html#INTERNET),这意味着它仍然是必需的。不推荐在更高版本中使用的权限,但不能删除以保持向后兼容性。你需要互联网许可,不管版本是什么 – Zoe
如果我们知道错误是什么,它仍然是最好的。 –
当你有足够的声望时,你可以通过评论做到这一点。与此同时,确保即使源代码指向Google Play而不是Android操作系统本身,也不要过分依赖答案。现在这是一个带有错误信息的答案,这个问题是重复的和无关紧要的,因为它没有MCVE。而不是本质上增加一个荣耀的评论作为一个答案哪里有一个部分,使它错了 – Zoe