我正在使用 moveTaskToBack 将我的应用程序置于后台。应用程序在 OS 2.2 中运行良好,但是当我在 OS 2.3.6 中测试相同的应用程序时,我遇到了一些奇怪的问题。
如果我将应用程序置于后台,然后再次置于前台,则互联网连接正常,但当应用程序处于前台时,如果我断开互联网连接,将应用程序置于后台,当我再次返回应用程序时,意味着应用程序进入前台我的应用程序调用 onCreate() 并且应用程序崩溃的时间。
代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// ////////////////////////////////////////////////////////////
Log.d("BuddyList ", "OnCreate");
txtIsConnected = (TextView) findViewById(R.id.isConnected);
txtIsConnected.setText("Online");
txtIsConnected.setTextColor(Color.GREEN);
listener = new PacketListener() {
public void processPacket(Packet packet) {
ActivityManager am = (ActivityManager) SnapApp.getInstance()
.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am
.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
String className = componentInfo.getClassName();
if (packet instanceof Message) {
final Message message = (Message) packet;
if (className
.equalsIgnoreCase("com.snapwork.xmppchatapp.ChatActivity")) {
if (ChatActivity.instance.to.contains(StringUtils
.parseBareAddress(message.getFrom()))) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
ChatActivity.instance.name(
message.getBody(),
ChatActivity.instance.to);
}
});
} else {
if (XMPPClient.notificationSenders.size() > 0
&& XMPPClient.notificationSenders
.contains(StringUtils
.parseBareAddress(message
.getFrom()))) {
NOTIFICATION_ID++;
}
XMPPClient.notificationMessages.add(message
.getBody());
XMPPClient.notificationSenders.add(StringUtils
.parseBareAddress(message.getFrom()));
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Notifications.addNotification(
message.getFrom(),
message.getBody());
}
});
}
} else if (className
.equalsIgnoreCase("com.snapwork.xmppchatapp.BuddyList")) {
XMPPClient.OfflineMessages.add(message.getBody());
XMPPClient.OfflineSenders.add(StringUtils
.parseBareAddress(message.getFrom()));
runOnUiThread(new Runnable() {
@Override
public void run() {
mAdapter = new DataAdapter(BuddyList.this,
R.layout.multi_line_list_item, datalist);
listMessages.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
}
});
}
else {
// System.out.println("While some where !!!");
XMPPClient.notificationMessages.add(message.getBody());
XMPPClient.notificationSenders.add(StringUtils
.parseBareAddress(message.getFrom()));
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Notifications.addNotification(
message.getFrom(), message.getBody());
}
});
}
} else if (packet instanceof Presence) {
// System.out.println("u have a frnd requ........ !!!");
}
}
};
// ////////////////////////////////////////////////////////////
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
XMPPClient.connection.addPacketListener(listener, filter);
buddyList = getIntent().getStringArrayListExtra("buddyList");
buddyStatus = getIntent().getStringArrayListExtra("buddystatus");
filteredList = getIntent().getStringArrayListExtra("buddyList");
filteredStat = getIntent().getStringArrayListExtra("buddystatus");
datalist = buddyList.toArray(new String[buddyList.size()]);
Statlist = buddyStatus.toArray(new String[buddyStatus.size()]);
recipient = (EditText) findViewById(R.id.recipient);
imgFindAdd = (ImageView) findViewById(R.id.findAddAccount);
imgFindAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(BuddyList.this, FindAndAdd.class);
i.putStringArrayListExtra("buddyList", buddyList);
i.putStringArrayListExtra("buddystatus", buddyStatus);
// startActivity(i);
startActivity(i);
}
});
for (int i = 0; i < datalist.length; i++) {
// Log.d("datalist : ", "" + datalist[i]);
}
listMessages = (ListView) findViewById(R.id.listMessages);
// setListAdapter();
mAdapter = new DataAdapter(BuddyList.this,
R.layout.multi_line_list_item, datalist);
listMessages.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
listMessages.setTextFilterEnabled(true);
listMessages.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long id) {
Intent intent = new Intent(BuddyList.this, ChatActivity.class);
Bundle b = new Bundle();
b.putString("to", filteredList.get(position));
intent.putExtras(b);
intent.putExtra("ustatus", Statlist[position]);
intent.putExtra("from", "BuddyList");
startActivity(intent);
}
});
/**
* Enabling Search Filter
* */
recipient.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// When user changed the Text
BuddyList.this.mAdapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
packetListener = new PacketListener() {
@Override
public void processPacket(Packet packet) {
final Presence presence = (Presence) packet;
if (presence.getType() == Type.available
|| presence.getType() == Type.unavailable) {
System.out.println("Finally Changing");
refresh();
}
ActivityManager am = (ActivityManager) SnapApp.getInstance()
.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am
.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
String className = componentInfo.getClassName();
if (className
.equalsIgnoreCase("com.snapwork.xmppchatapp.BuddyList")) {
if (presence.getType() == Type.subscribe) {
XMPPClient.OfflineFrndReq
.add(presence.getType().name());
XMPPClient.OfflineFrndReqSender.add(presence.getFrom());
FriendRequestNotification.addNotification(
presence.getFrom(), "abcd");
} else if (presence.getType() == Type.unsubscribed) {
runOnUiThread(new Runnable() {
@Override
public void run() {
ShowToast1();
}
});
} else if (presence.getType() == Type.subscribed) {
Presence subscribed = new Presence(
Presence.Type.subscribed);
subscribed.setTo(StringUtils.parseBareAddress(presence
.getFrom()));
XMPPClient.connection.sendPacket(subscribed);
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if (XMPPClient.flag)
new ShowDialogAsyncTask().execute();
else
ShowToast();
}
});
}
}
}
};
XMPPClient.connection.addPacketListener(packetListener,
new PacketTypeFilter(Presence.class));
Button btnSwitch = (Button) findViewById(R.id.switchaccount);
btnSwitch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(BuddyList.this, Accounts.class);
startActivity(i);
}
});
}
日志猫:04-08 11:33:53.585: E/AndroidRuntime(29175): FATAL EXCEPTION: main
04-08 11:33:53.585: E/AndroidRuntime(29175): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.snapwork.xmppchatapp/com.snapwork.xmppchatapp.BuddyList}: java.lang.NullPointerException
04-08 11:33:53.585: E/AndroidRuntime(29175): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
04-08 11:33:53.585: E/AndroidRuntime(29175): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
04-08 11:33:53.585: E/AndroidRuntime(29175): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-08 11:33:53.585: E/AndroidRuntime(29175): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
04-08 11:33:53.585: E/AndroidRuntime(29175): at android.os.Handler.dispatchMessage(Handler.java:99)
04-08 11:33:53.585: E/AndroidRuntime(29175): at android.os.Looper.loop(Looper.java:130)
04-08 11:33:53.585: E/AndroidRuntime(29175): at android.app.ActivityThread.main(ActivityThread.java:3687)
04-08 11:33:53.585: E/AndroidRuntime(29175): at java.lang.reflect.Method.invokeNative(Native Method)
04-08 11:33:53.585: E/AndroidRuntime(29175): at java.lang.reflect.Method.invoke(Method.java:507)
04-08 11:33:53.585: E/AndroidRuntime(29175): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-08 11:33:53.585: E/AndroidRuntime(29175): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-08 11:33:53.585: E/AndroidRuntime(29175): at dalvik.system.NativeStart.main(Native Method)
04-08 11:33:53.585: E/AndroidRuntime(29175): Caused by: java.lang.NullPointerException
04-08 11:33:53.585: E/AndroidRuntime(29175): at com.snapwork.xmppchatapp.BuddyList.onCreate(BuddyList.java:266)
04-08 11:33:53.585: E/AndroidRuntime(29175): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-08 11:33:53.585: E/AndroidRuntime(29175): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
04-08 11:33:53.585: E/AndroidRuntime(29175): ... 11 more
04-08 11:33:53.593: E/liblog(1514): failed to call dumpstate
04-08 11:33:53.593: E/(1514): Dumpstate > /data/log/dumpstate_app_error