您好我有一个与Tcp服务器通信的聊天应用程序,我正在尝试使用服务,因此当我关闭它时应用程序仍将连接到服务器。
启动服务后,应用程序正在连接并且可以正常工作但是当应用程序关闭且服务器向应用程序发送消息时,它会崩溃。 我使用AsyncTask,这是我的代码
服务
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new MainActivity.connectTask().execute("");
return mStartMode;
}
任务
public static class connectTask extends AsyncTask<String, String, TCPClient> {
@Override
protected TCPClient doInBackground(String... message) {
//we create a TCPClient object and
MainActivity.mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() {
@Override
//here the messageReceived method is implemented
public void messageReceived(String message) {
//this method calls the onProgressUpdate
/*
*/
publishProgress(message);
}
});
MainActivity.mTcpClient.run();
return null;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
Communication.ManageData(values[0]);
}
}
和
public class Communication extends Activity {
public static TinyDB tinydb = new TinyDB(MainActivity.cpn);
public static ArrayList<String> dipattern = new ArrayList<String>();
public static ArrayList<String> forconfirm;
public static void ManageData(String data) {
dispatternFunc(data,dipattern);
switch (dipattern.get(0)) {
case "100":
tinydb.putBoolean("isLoged", true);
Intent intent = new Intent("finish_activity");
MainActivity.cpn.sendBroadcast(intent);
break;
case "101":
break;
case "103":
break;
case "104":
break;
case "106":
break;
case "200":
String temp = dipattern.get(2);
if( In_Chat.mAdapter2!=null)
{
In_Chat.mAdapter2.addMessage(temp, 0);
}
else
{
ChatCustomAdapter.AllChatData = tinydb.getListString(dipattern.get(1));
ChatCustomAdapter.AllChatDirection = tinydb.getListInt(dipattern.get(1) + "d");
ChatCustomAdapter.AllChatData.add(temp);
ChatCustomAdapter.AllChatDirection.add(0);
}
tinydb.putListString(dipattern.get(1), ChatCustomAdapter.AllChatData);
tinydb.putListInt(dipattern.get(1) + "d", ChatCustomAdapter.AllChatDirection);
String confirm;
forconfirm = new ArrayList<String>();
forconfirm.add(User.username);
forconfirm.add(dipattern.get(1));
confirm = Communication.pattern(forconfirm, 201);
//sends the message to the server
if (MainActivity.mTcpClient != null) {
MainActivity.mTcpClient.sendMessage(confirm);
}
break;
case "201":
break;
case "202":
break;
case "300":
break;
case "600":
break;
case "601":
break;
case "602":
TabFragmment2.fAdapter.addUser(dipattern.get(1));
TabFragmment2.fAdapter.notifyDataSetChanged();
tinydb.putListString("myFriends", FriendCustomAdapter.FrListItems);
break;
case "603":
break;
case "604":
break;
case "900":
TabFragment1.mAdapter.addUser(dipattern.get(1));
MainActivity.tempnum++;
// notify the adapter that the data set has changed. This means that new message received
// from server was added to the list
TabFragment1.mAdapter.notifyDataSetChanged();
tinydb.putListString("myUsers", MyCustomAdapter.mListItems);
break;
case "911":
break;
default:
}
dipattern.clear();
}
当我关闭它时应用程序崩溃,我收到示例
的消息“200”我收到此错误
02-22 15:40:06.194 5550-5550/com.example.user.loco E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.user.loco, PID: 5550
java.lang.ExceptionInInitializerError
at com.example.user.loco.MainActivity$connectTask.onProgressUpdate(MainActivity.java:249)
at com.example.user.loco.MainActivity$connectTask.onProgressUpdate(MainActivity.java:220)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:671)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:375)
at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:370)
at com.example.user.loco.TinyDB.<init>(TinyDB.java:51)
at com.example.user.loco.Communication.<clinit>(Communication.java:12)
at com.example.user.loco.MainActivity$connectTask.onProgressUpdate(MainActivity.java:249)
at com.example.user.loco.MainActivity$connectTask.onProgressUpdate(MainActivity.java:220)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:671)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)