My app works fine but when the app goes to the background it crashes when it should resume. As you can see in the source code I log the onStart, onStop etcetera events.
In my log I can see onStart, onResume when I start the app. When I press the back-key I see: onStop, STOP, onPause and onDestroy.
当我试图立即重开坠机时,除了无法开始活动部件Info java lang外,没有其他电文。 Null PointerException.
我如何防止这种情况?
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();
iii catch (XMPPException e) {
e.printStackTrace();
iii
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("");
iii
iii);
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;
iii
iii
return false;
iii
iii);
iii
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);
iii else {
discussionThreadAdapter.addVanAnderItem(nieuweRegel);
iii
iii
iii
iii, filter);
iii
private void Notify() {
discussionThreadAdapter.notifyDataSetChanged();
listview.setSelection(discussionThreadAdapter.getCount());
iii
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);
iii
public void addVanAnderItem(final String item) {
discussionThread.add(item);
handler.post(new Runnable() {
public void run() {
Notify();
iii
iii);
iii
public void addVanMijItem(final String item) {
discussionThread.add(item);
mySet.add(discussionThread.size() - 1);
handler.post(new Runnable() {
public void run() {
Notify();
iii
iii);
iii
@Override
public int getItemViewType(int position) {
return mySet.contains(position) ? BERICHT_VAN_MIJ : BERICHT_VAN_ANDER;
iii
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
iii
@Override
public int getCount() {
return discussionThread.size();
iii
@Override
public String getItem(int position) {
return discussionThread.get(position);
iii
@Override
public long getItemId(int position) {
return position;
iii
@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;
iii
convertView.setTag(holder);
iii else {
holder = (ViewHolder)convertView.getTag();
iii
holder.textView.setText(discussionThread.get(position));
return convertView;
iii
iii
public static class ViewHolder {
public TextView textView;
iii
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("");
iii
public void onStart() {
super.onStart();
Log.i("CONN", "onStart");
startConnection();
iii
public void onRestart() {
super.onRestart();
Log.i("CONN", "onReStart");
startConnection();
iii
public void onResume() {
super.onResume();
Log.i("CONN", "onResume");
startConnection();
iii
public void onPause() {
super.onPause();
Log.i("CONN", "onPause");
stopConnection();
iii
public void onStop() {
super.onStop();
Log.i("CONN", "onStop");
stopConnection();
iii
public void onDestroy() {
super.onDestroy();
Log.i("CONN", "onDestroy");
stopConnection();
iii
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;
iii
iii
private void startConnection() {
if (connection.isConnected() ) {
iii else {
Log.i("CONN", "START");
try {
initConnection();
iii catch (XMPPException e) {
e.printStackTrace();
iii
iii
iii
iii
第一工作组修改如下:
private void startConnection() {
if (connection != null ) {
Log.i("CONN", "RUNNING");
iii else {
Log.i("CONN", "START");
try {
initConnection();
iii catch (XMPPException e) {
e.printStackTrace();
iii
iii
iii
我一开始,一切都会奏效。 记录:
05-12 08:40:21.743: D/AndroidRuntime(491): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<< 05-12 08:40:21.743: D/AndroidRuntime(491): CheckJNI is ON
05-12 08:40:22.065: D/AndroidRuntime(491): --- registering native functions ---
05-12 08:40:23.353:D/AndroidRuntime(491):切除
05-12 08:40:23.363: D/dalvikvm(491): Debugger has detached; object registry had 1 entries
05-12 08:40:23.393: I/AndroidRuntime(491): NOTE: attach of thread Binder Thread #3 failed
05-12 08:40:24.184: D/AndroidRuntime(499): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
05-12 08:40:24.184:D/AndroidRuntime(499):国家情报和安全局
05-12 08:40:24.523: D/AndroidRuntime(499): --- registering native functions ---
05-12 08:40:25.873: I/ActivityManager(70): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=nl.yeswecanclinics.chat/.Start iii
05-12 08:40:25.965:D/AndroidRuntime(499): 削减证书
05-12 08:40:25.973: D/dalvikvm(499): Debugger has detached; object registry had 1 entries
05-12 08:40:26.034: I/AndroidRuntime(499): NOTE: attach of thread Binder Thread #3 failed
05-12 08:40:26.105: I/ActivityManager(70): Start proc nl.yeswecanclinics.chat for activity nl.yeswecanclinics.chat/.Start: pid=506 uid=10032 gids={3003, 1015iii
05-12 08:40:27.843: I/global(506): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
05-12 08:40:27.843: I/global(506): Default buffer size used in BufferedWriter constructor. It would be better to be explicit if an 8k-char buffer is required.
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): at 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): at org.jivesoftware.smack.XMPPConnection.proceedTLSReceived(XMPPConnection.java:832)
05-12 08:40:28.304: W/System.err(506): at 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): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
05-12 08:40:29.014: I/global(506): Default buffer size used in BufferedWriter constructor. It would be better to be explicit if an 8k-char buffer is required.
05-12 08:40:29.483: D/dalvikvm(506): GC_FOR_MALLOC freed 3668 objects / 280752 bytes in 153ms
页: 1
页: 1
页: 1
页: 1
05-12 08:40:30.633: I/ActivityManager(70): Displayed activity nl.yeswecanclinics.chat/.Start: 4712 ms (total 384269 ms)
05-12 08:40:37.114: D/dalvikvm(175): GC_EXPLICIT freed 444 objects / 22064 bytes in 122ms
I PRESS THE BACK BUTTON
05-12 08:41:07.253:W/KeyCharacterMap(506):无主板
05-12 08:41:07.253: W/KeyCharacterMap(506): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
05-12 08:41:07.403: I/CONN(506): onPause
页: 1
05-12 08:41:07.784: W/InputManagerService(70): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@45066cf8 (uid=10032 pid=506)
05-12 08:41:07.804: W/IInputConnectionWrapper(506): showStatusIcon on inactive InputConnection
页: 1
页: 1
页: 1
05-12 08:41:30.583: I/global(506): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
05-12 08:41:30.623: I/global(506): Default buffer size used in BufferedWriter constructor. It would be better to be explicit if an 8k-char buffer is required.
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): at java.security.KeyStore.getInstance(KeyStore.java:134)
05-12 08:41:31.663: W/System.err(506): at org.jivesoftware.smack.ServerTrustManager.(ServerTrustManager.java:61)
05-12 08:41:31.674: W/System.err(506): at org.jivesoftware.smack.XMPPConnection.proceedTLSReceived(XMPPConnection.java:832)
05-12 08:41:31.674: W/System.err(506): at 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): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
05-12 08:41:31.994: I/global(506): Default buffer size used in BufferedWriter constructor. It would be better to be explicit if an 8k-char buffer is required.
05-12 08:41:32.043:D/AndroidRuntime(506):Shingdown RV
05-12 08:41:32.043: W/dalvikvm(506): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-12 08:41:32.214: D/dalvikvm(506): GC_FOR_MALLOC freed 5507 objects / 388504 bytes in 147ms
05-12 08:41:32.226:D/NativeCrypto(506):自由开放式SSL会议
05-12 08:41:32.234:E/AndroidRuntime(506): FATAL EXCEPTION: main
05-12 08:41:32.234: E/AndroidRuntime(506): java.lang.RuntimeException: Unable to start activity ComponentInfo{nl.yeswecanclinics.chat/nl.yeswecanclinics.chat.Startiii: java.lang.NullPointerException
05-12 08:41:32.234: E/AndroidRuntime(506): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-12 08:41:32.234: E/AndroidRuntime(506): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-12 08:41:32.234: E/AndroidRuntime(506): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-12 08:41:32.234: E/AndroidRuntime(506): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-12 08:41:32.234: E/AndroidRuntime(506): at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 08:41:32.234: E/AndroidRuntime(506): at android.os.Looper.loop(Looper.java:123)
05-12 08:41:32.234: E/AndroidRuntime(506): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-12 08:41:32.234: E/AndroidRuntime(506): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 08:41:32.234: E/AndroidRuntime(506): at java.lang.reflect.Method.invoke(Method.java:521)
05-12 08:41:32.234: E/AndroidRuntime(506): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 05-12 08:41:32.234: E/AndroidRuntime(506): at 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): Caused by: 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): at org.jivesoftware.smack.XMPPConnection.initConnection(XMPPConnection.java:618)
05-12 08:41:32.234: E/AndroidRuntime(506): at org.jivesoftware.smack.XMPPConnection.connectUsingConfiguration(XMPPConnection.java:565)
05-12 08:41:32.234: E/AndroidRuntime(506): at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:991)
05-12 08:41:32.234: E/AndroidRuntime(506): at nl.yeswecanclinics.chat.Start.initConnection(Start.java:131)
05-12 08:41:32.234: E/AndroidRuntime(506): at nl.yeswecanclinics.chat.Start.onCreate(Start.java:71)
05-12 08:41:32.234: E/AndroidRuntime(506): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-12 08:41:32.234: E/AndroidRuntime(506): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-12 08:41:32.234:E/AndroidRuntime(506):......