我的应用程序工作正常,但是当应用程序进入后台时,它应该恢复时崩溃。正如您在源代码中看到的,我记录了 onStart、onStop 等事件。
在我的日志中,当我启动应用程序时,我可以看到 onStart、onResume。当我按下返回键时,我看到:onStop、STOP、onPause 和 onDestroy。
当我尝试重新启动应用程序时,它会立即崩溃,除了“无法启动 Activity ComponentInfo java lang.NullPointerException”之外,日志中没有其他消息。
我怎样才能防止这种情况?
public class Start extends Activity {
private Handler handler = new Handler();
private ArrayList<String> discussionThread;
private EditText textMessage;
private ListView listview;
private ConnectionConfiguration config;
private Presence presence;
private MultiUserChat muc;
private DiscussionHistory history;
private PacketFilter filter;
private MyCustomAdapter discussionThreadAdapter;
private XMPPConnection connection;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
initConnection();
} catch (XMPPException e) {
e.printStackTrace();
}
final EditText textMessage = (EditText) this.findViewById(R.id.message);
listview = (ListView) this.findViewById(R.id.list);
discussionThread = new ArrayList<String>();
discussionThreadAdapter = new MyCustomAdapter();
listview.setAdapter(discussionThreadAdapter);
Button send = (Button) this.findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String text = textMessage.getText().toString();
Message msg = new Message(ROOM, Message.Type.groupchat);
msg.setBody(text);
connection.sendPacket(msg);
discussionThreadAdapter.notifyDataSetChanged();
textMessage.setText("");
}
});
textMessage.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
SendText();
return true;
default:
break;
}
}
return false;
}
});
}
private void initConnection() throws XMPPException {
config = new ConnectionConfiguration(SERVER_HOST, SERVER_PORT, SERVICE_NAME);
connection = new XMPPConnection(config);
connection.connect();
connection.login(LOGIN, PASSWORD);
presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
muc = new MultiUserChat(connection, ROOM);
history = new DiscussionHistory();
history.setMaxStanzas(25);
muc.join(LOGIN, PASSWORD, history, SmackConfiguration.getPacketReplyTimeout());
filter = new MessageTypeFilter(Message.Type.groupchat);
connection.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = message.getFrom().substring(48);
String nieuweRegel = fromName + ": " + message.getBody();
fromName = fromName.toUpperCase();
if (fromName.equals(LOGIN.toUpperCase())) {
discussionThreadAdapter.addVanMijItem(nieuweRegel);
} else {
discussionThreadAdapter.addVanAnderItem(nieuweRegel);
}
}
}
}, filter);
}
private void Notify() {
discussionThreadAdapter.notifyDataSetChanged();
listview.setSelection(discussionThreadAdapter.getCount());
}
private class MyCustomAdapter extends BaseAdapter {
private static final int BERICHT_VAN_ANDER = 0;
private static final int BERICHT_VAN_MIJ = 1;
private static final int TYPE_MAX_COUNT = BERICHT_VAN_MIJ + 1;
private LayoutInflater mInflater;
private TreeSet<Integer> mySet = new TreeSet<Integer>();
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addVanAnderItem(final String item) {
discussionThread.add(item);
handler.post(new Runnable() {
public void run() {
Notify();
}
});
}
public void addVanMijItem(final String item) {
discussionThread.add(item);
mySet.add(discussionThread.size() - 1);
handler.post(new Runnable() {
public void run() {
Notify();
}
});
}
@Override
public int getItemViewType(int position) {
return mySet.contains(position) ? BERICHT_VAN_MIJ : BERICHT_VAN_ANDER;
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
@Override
public int getCount() {
return discussionThread.size();
}
@Override
public String getItem(int position) {
return discussionThread.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case BERICHT_VAN_ANDER:
convertView = mInflater.inflate(R.layout.bericht_van_ander_item, null);
holder.textView = (TextView)convertView.findViewById(R.id.textline);
break;
case BERICHT_VAN_MIJ:
convertView = mInflater.inflate(R.layout.bericht_van_mij_item, null);
holder.textView = (TextView)convertView.findViewById(R.id.textline);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.textView.setText(discussionThread.get(position));
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
private void SendText() {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(textMessage.getWindowToken(), 0);
String text = textMessage.getText().toString();
Message msg = new Message(ROOM, Message.Type.groupchat);
msg.setBody(text);
connection.sendPacket(msg);
textMessage.setText("");
}
public void onStart() {
super.onStart();
Log.i("CONN", "onStart");
startConnection();
}
public void onRestart() {
super.onRestart();
Log.i("CONN", "onReStart");
startConnection();
}
public void onResume() {
super.onResume();
Log.i("CONN", "onResume");
startConnection();
}
public void onPause() {
super.onPause();
Log.i("CONN", "onPause");
stopConnection();
}
public void onStop() {
super.onStop();
Log.i("CONN", "onStop");
stopConnection();
}
public void onDestroy() {
super.onDestroy();
Log.i("CONN", "onDestroy");
stopConnection();
}
private void stopConnection() {
if (connection != null) {
Log.i("CONN", "STOP");
connection.disconnect(presence);
connection = null;
filter = null;
history = null;
muc = null;
presence = null;
config = null;
discussionThreadAdapter = null;
}
}
private void startConnection() {
if (connection.isConnected() ) {
} else {
Log.i("CONN", "START");
try {
initConnection();
} catch (XMPPException e) {
e.printStackTrace();
}
}
}
}
好的,我更改了以下内容:
private void startConnection() {
if (connection != null ) {
Log.i("CONN", "RUNNING");
} else {
Log.i("CONN", "START");
try {
initConnection();
} catch (XMPPException e) {
e.printStackTrace();
}
}
}
我启动应用程序,一切正常。日志:
05-12 08:40:21.743: D/AndroidRuntime(491): >>>>>>>>>>>>>> AndroidRuntime 开始 <<<<<<<<<<<<< 05-12 08 :40:21.743: D/AndroidRuntime(491): CheckJNI 开启
05-12 08:40:22.065: D/AndroidRuntime(491): --- 注册本机函数 ---
05-12 08:40:23.353: D/AndroidRuntime(491): 关闭虚拟机
05-12 08:40:23.363: D/dalvikvm(491): 调试器已分离;对象注册表有 1 个条目
05-12 08:40:23.393: I/AndroidRuntime(491): 注意: 附加线程 'Binder Thread #3' 失败
05-12 08:40:24.184: D/AndroidRuntime(499): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
05-12 08:40:24.184: D/AndroidRuntime(499): CheckJNI 开启
05-12 08:40:24.523: D/AndroidRuntime(499): --- 注册本机函数 ---
05-12 08:40:25.873: I/ActivityManager(70): 开始活动: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=nl.yeswecanclinics。聊天/.开始 }
05-12 08:40:25.965: D/AndroidRuntime(499): 关闭虚拟机
05-12 08:40:25.973: D/dalvikvm(499): 调试器已分离;对象注册表有 1 个条目
05-12 08:40:26.034: I/AndroidRuntime(499): 注意: 附加线程 'Binder Thread #3' 失败
05-12 08:40:26.105: I/ActivityManager(70): 启动 proc nl.yeswecanclinics.chat 活动 nl.yeswecanclinics.chat/.Start: pid=506 uid=10032 gids={3003, 1015}
05-12 08:40:27.843: I/global(506): BufferedReader 构造函数中使用的默认缓冲区大小。如果需要 8k 字符的缓冲区,最好是明确的。
05-12 08:40:27.843: I/global(506): BufferedWriter 构造函数中使用的默认缓冲区大小。如果需要 8k 字符的缓冲区,最好是明确的。
05-12 08:40:28.294: W/System.err(506): java.security.KeyStoreException: KeyStore jks implementation not found
05-12 08:40:28.294: W/System.err(506): 在 java.security.KeyStore.getInstance(KeyStore.java:134)
05-12 08:40:28.294: W/System.err(506): at org.jivesoftware.smack.ServerTrustManager.(ServerTrustManager.java:61)
05-12 08:40:28.294: W/System.err(506): 在 org.jivesoftware.smack.XMPPConnection.proceedTLSReceived(XMPPConnection.java:832)
05-12 08:40:28.304: W/System.err(506): 在 org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:268)
05-12 08:40:28.304: W/System.err(506): at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:44)
05-12 08:40:28.313: W/System.err(506): at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:71)
05-12 08:40:29.004: I/global(506): BufferedReader 构造函数中使用的默认缓冲区大小。如果需要 8k 字符的缓冲区,最好是明确的。
05-12 08:40:29.014: I/global(506): BufferedWriter 构造函数中使用的默认缓冲区大小。如果需要 8k 字符的缓冲区,最好是明确的。
05-12 08:40:29.483: D/dalvikvm(506): GC_FOR_MALLOC 在 153 毫秒内释放了 3668 个对象/280752 个字节
05-12 08:40:29.663: I/CONN(506): onStart
05-12 08:40:29.685:I/CONN(506):正在运行
05-12 08:40:29.685: I/CONN(506): onResume
05-12 08:40:29.693:I/CONN(506):正在运行
05-12 08:40:30.633: I/ActivityManager(70): 显示的活动 nl.yeswecanclinics.chat/.Start: 4712 ms(总共 384269 ms)
05-12 08:40:37.114: D/dalvikvm(175): GC_EXPLICIT 在 122 毫秒内释放了 444 个对象/22064 个字节
我按下返回按钮
05-12 08:41:07.253: W/KeyCharacterMap(506): id 0 没有键盘
05-12 08:41:07.253:W/KeyCharacterMap(506):使用默认键盘映射:/system/usr/keychars/qwerty.kcm.bin
05-12 08:41:07.403: I/CONN(506): onPause
05-12 08:41:07.403: I/CONN(506): 停止
05-12 08:41:07.784: W/InputManagerService(70): 在非聚焦客户端 com.android.internal.view.IInputMethodClient$Stub$Proxy@45066cf8 (uid=10032 pid=506) 上开始输入
05-12 08:41:07.804: W/IInputConnectionWrapper(506): 非活动 InputConnection 上的 showStatusIcon
05-12 08:41:08.173: I/CONN(506): onStop
05-12 08:41:08.173: I/CONN(506): onDestroy
我通过单击图标重新启动应用程序
05-12 08:41:30.583: I/global(506): BufferedReader 构造函数中使用的默认缓冲区大小。如果需要 8k 字符的缓冲区,最好是明确的。
05-12 08:41:30.623: I/global(506): BufferedWriter 构造函数中使用的默认缓冲区大小。如果需要 8k 字符的缓冲区,最好是明确的。
05-12 08:41:31.663: W/System.err(506): java.security.KeyStoreException: KeyStore jks implementation not found
05-12 08:41:31.663: W/System.err(506): 在 java.security.KeyStore.getInstance(KeyStore.java:134)
05-12 08:41:31.663: W/System.err(506): 在 org.jivesoftware.smack.ServerTrustManager.(ServerTrustManager.java:61)
05-12 08:41:31.674: W/System.err(506): 在 org.jivesoftware.smack.XMPPConnection.proceedTLSReceived(XMPPConnection.java:832)
05-12 08:41:31.674: W/System.err(506): 在 org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:268)
05-12 08:41:31.683: W/System.err(506): at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:44)
05-12 08:41:31.683: W/System.err(506): at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:71)
05-12 08:41:31.984: I/global(506): BufferedReader 构造函数中使用的默认缓冲区大小。如果需要 8k 字符的缓冲区,最好是明确的。
05-12 08:41:31.994: I/global(506): BufferedWriter 构造函数中使用的默认缓冲区大小。如果需要 8k 字符的缓冲区,最好是明确的。
05-12 08:41:32.043: D/AndroidRuntime(506): 关闭虚拟机
05-12 08:41:32.043: W/dalvikvm(506): threadid=1: 线程退出未捕获异常 (group=0x4001d800)
05-12 08:41:32.214: D/dalvikvm(506): GC_FOR_MALLOC 在 147 毫秒内释放了 5507 个对象/388504 个字节
05-12 08:41:32.226: D/NativeCrypto(506): 释放 OpenSSL 会话
05-12 08:41:32.234: E/AndroidRuntime(506): 致命异常: main
05-12 08:41:32.234: E/AndroidRuntime(506): java.lang.RuntimeException: 无法启动活动 ComponentInfo{nl.yeswecanclinics.chat/nl.yeswecanclinics.chat.Start}: java.lang.NullPointerException
05-12 08:41:32.234: E/AndroidRuntime(506): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-12 08:41:32.234: E/AndroidRuntime(506): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-12 08:41:32.234: E/AndroidRuntime(506): 在 android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-12 08:41:32.234: E/AndroidRuntime(506): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-12 08:41:32.234: E/AndroidRuntime(506): 在 android.os.Handler.dispatchMessage(Handler.java:99)
05-12 08:41:32.234: E/AndroidRuntime(506): 在 android.os.Looper.loop(Looper.java:123)
05-12 08:41:32.234: E/AndroidRuntime(506): 在 android.app.ActivityThread.main(ActivityThread.java:4627)
05-12 08:41:32.234: E/AndroidRuntime(506): 在 java.lang.reflect.Method.invokeNative(Native Method)
05-12 08:41:32.234: E/AndroidRuntime(506): 在 java.lang.reflect.Method.invoke(Method.java:521)
05-12 08:41:32.234: E/AndroidRuntime(506): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 05-12 08:41:32.234: E/AndroidRuntime( 506): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-12 08:41:32.234: E/AndroidRuntime(506): at dalvik.system.NativeStart.main(Native Method)
05-12 08:41:32.234: E/AndroidRuntime(506): 由: java.lang.NullPointerException 引起
05-12 08:41:32.234: E/AndroidRuntime(506): at org.jivesoftware.smackx.muc.MultiUserChat$1.connectionCreated(MultiUserChat.java:114)
05-12 08:41:32.234: E/AndroidRuntime(506): 在 org.jivesoftware.smack.XMPPConnection.initConnection(XMPPConnection.java:618)
05-12 08:41:32.234: E/AndroidRuntime(506): 在 org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:565)
05-12 08:41:32.234: E/AndroidRuntime(506): 在 org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:991)
05-12 08:41:32.234: E/AndroidRuntime(506): 在 nl.yeswecanclinics.chat.Start.initConnection(Start.java:131)
05-12 08:41:32.234: E/AndroidRuntime(506): 在 nl.yeswecanclinics.chat.Start.onCreate(Start.java:71)
05-12 08:41:32.234: E/AndroidRuntime(506): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-12 08:41:32.234: E/AndroidRuntime(506): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-12 08:41:32.234: E/AndroidRuntime(506): ... 11 更多