我正在Android Studio中开展一个项目。我为android做了聊天客户端。我在NetBeans IDE上创建服务器。但我在android上的聊天客户端遇到了麻烦。这是我的代码:
class ChatUpd extends AsyncTask<Void, String, String> {
ProgressBar progressBar;
int count;
protected void onPreExecute() {
progressBar = findViewById(R.id.progressBar);
progressBar.setMax(200);
progressBar.setProgress(1);
count = 0;
}
@Override
protected String doInBackground(Void... voids) {
for ( ; ; ) {
publishProgress();
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
protected void onProgressUpdate(String... values) {
count++;
progressBar.setProgress(count);
adapter.notifyDataSetChanged();
if (connected == 1) {
new Thread(new Runnable() {
@Override
public void run() {
try {
while (true) {
if (inMessage.hasNext()) {
String inMes = inMessage.nextLine();
String clientsInChat = "Clients count = ";
if (inMes.indexOf(clientsInChat) == 0) {
} else {
chat.add(inMes);
adapter.notifyDataSetChanged();
}
}
}
} catch (Exception e) {
Log.w("Infinite loop error!", e);
}
}
}).start();
}
}
public void onPostExecute(String result) {
Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
}
}
}
这部分代码由于某种原因给我一个错误。那个错误是:
java.lang.OutOfMemoryError: pthread_create (stack size 16384 bytes) failed: Try again
at java.lang.VMThread.create(Native Method)
at java.lang.Thread.start(Thread.java:1029)
at com.lolcreeper0.testapp.MainActivity$ChatUpd.onProgressUpdate(MainActivity.java:256)
at com.lolcreeper0.testapp.MainActivity$ChatUpd.onProgressUpdate(MainActivity.java:176)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:647)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5136)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
我究竟做错了什么?