gpt4 book ai didi

android - 无法在恢复时启动 Activity ComponentInfo

转载 作者:行者123 更新时间:2023-11-30 04:12:56 29 4
gpt4 key购买 nike

我的应用程序运行正常,但当应用程序进入后台时,它在应该恢复时崩溃了。正如您在源代码中看到的那样,我记录了 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 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): Shutting down VM

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): CheckJNI is ON

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 }

05-12 08:40:25.965: D/AndroidRuntime(499): Shutting down VM

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, 1015}

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

05-12 08:40:29.663: I/CONN(506): onStart

05-12 08:40:29.685: I/CONN(506): RUNNING

05-12 08:40:29.685: I/CONN(506): onResume

05-12 08:40:29.693: I/CONN(506): RUNNING

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): No keyboard for id 0

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

05-12 08:41:07.403: I/CONN(506): STOP

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

05-12 08:41:08.173: I/CONN(506): onStop

05-12 08:41:08.173: I/CONN(506): onDestroy

I RESTART THE APP BY CLICKING THE ICON

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): Shutting down VM

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): Freeing OpenSSL session

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.Start}: 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): ... 11 more

最佳答案

虽然 Marek 的回答肯定没有错(这实际上可能是您得到 NullPointerException 的原因),但我知道的一件事是您不应该尝试在 UI 线程上建立连接.如果建立连接需要超过几秒钟的时间,这将不可避免地导致您的应用程序崩溃。您应该创建一个 AsnycTask 并在那里更改您的连接和状态。

关于android - 无法在恢复时启动 Activity ComponentInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10529769/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com