android 填充另一个 class ArrayList
android populate another class ArrayList
我正在尝试编写代码以从一个 class 异步任务的输出中填充一个数组。
现在代码给我空引用..把代码放在下面
数组CLASS:
public class ClientDiscovery {
public static ArrayList<InetAddress> clientes;
public static void addclient(InetAddress address)
{
clientes.add(address);
}
public static void removeclient(InetAddress address)
{
clientes.remove(address);
}
public static ArrayList<InetAddress>getclient()
{
Set<InetAddress> noduplicate = new HashSet<>();
if (!(clientes ==null)) {
noduplicate.addAll(clientes);
clientes.clear();
clientes.addAll(noduplicate);
}
return clientes;
}
}
填充数组的异步任务
public class RequestDiscoveryAsyncTask extends AsyncTask<Void,Void,Void>{
Context context;
public RequestDiscoveryAsyncTask(Context context) {
this.context = context.getApplicationContext();
}
@Override
protected Void doInBackground(Void...voids) {
///HERE I INITIALIZE THE SOCKETS ****INTENTIONALLY OMITED TO EASY READING***
/*HERE I INTENT TO GET THE ACK BACK FROM THE CLIENTES "MY CUSTOM ACK"*/
while(!timeout) {
byte[] ready = new byte[5];
DatagramPacket pkgready = new DatagramPacket(ready, 0, 5);
socket.receive(pkgready);
if (new String(pkgready.getData()).contains("READY"))
ClientDiscovery.addclient(pkgready.getAddress());
}
}
return null;
}
}
socket初始化和捕获异常省略,方便阅读
LOGCAT:
07-18 21:50:43.889 4327-5025/fr.xplod.focal E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:838)
Caused by: java.lang.NullPointerException
at org.cyanogenmod.focal.ClientDiscovery.addclient(ClientDiscovery.java:16)
at org.cyanogenmod.focal.RequestDiscoveryAsyncTask.doInBackground(RequestDiscoveryAsyncTask.java:68)
at org.cyanogenmod.focal.RequestDiscoveryAsyncTask.doInBackground(RequestDiscoveryAsyncTask.java:23)
at android.os.AsyncTask.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:230)
atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
atjava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:838)
刚刚创建对象,当前ArrayList对象不存在
添加此行:
public static ArrayList<InetAddress> clientes = new ArrayList<InetAddress>();
我正在尝试编写代码以从一个 class 异步任务的输出中填充一个数组。
现在代码给我空引用..把代码放在下面
数组CLASS:
public class ClientDiscovery {
public static ArrayList<InetAddress> clientes;
public static void addclient(InetAddress address)
{
clientes.add(address);
}
public static void removeclient(InetAddress address)
{
clientes.remove(address);
}
public static ArrayList<InetAddress>getclient()
{
Set<InetAddress> noduplicate = new HashSet<>();
if (!(clientes ==null)) {
noduplicate.addAll(clientes);
clientes.clear();
clientes.addAll(noduplicate);
}
return clientes;
}
}
填充数组的异步任务
public class RequestDiscoveryAsyncTask extends AsyncTask<Void,Void,Void>{
Context context;
public RequestDiscoveryAsyncTask(Context context) {
this.context = context.getApplicationContext();
}
@Override
protected Void doInBackground(Void...voids) {
///HERE I INITIALIZE THE SOCKETS ****INTENTIONALLY OMITED TO EASY READING***
/*HERE I INTENT TO GET THE ACK BACK FROM THE CLIENTES "MY CUSTOM ACK"*/
while(!timeout) {
byte[] ready = new byte[5];
DatagramPacket pkgready = new DatagramPacket(ready, 0, 5);
socket.receive(pkgready);
if (new String(pkgready.getData()).contains("READY"))
ClientDiscovery.addclient(pkgready.getAddress());
}
}
return null;
}
}
socket初始化和捕获异常省略,方便阅读
LOGCAT:
07-18 21:50:43.889 4327-5025/fr.xplod.focal E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:838)
Caused by: java.lang.NullPointerException
at org.cyanogenmod.focal.ClientDiscovery.addclient(ClientDiscovery.java:16)
at org.cyanogenmod.focal.RequestDiscoveryAsyncTask.doInBackground(RequestDiscoveryAsyncTask.java:68)
at org.cyanogenmod.focal.RequestDiscoveryAsyncTask.doInBackground(RequestDiscoveryAsyncTask.java:23)
at android.os.AsyncTask.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:230)
atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
atjava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:838)
刚刚创建对象,当前ArrayList对象不存在
添加此行:
public static ArrayList<InetAddress> clientes = new ArrayList<InetAddress>();