- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用程序运行正常,但当应用程序进入后台时,它在应该恢复时崩溃了。正如您在源代码中看到的那样,我记录了 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/
我通过 spring ioc 编写了一些 Rest 应用程序。但我无法解决这个问题。这是我的异常(exception): org.springframework.beans.factory.BeanC
我对 TestNG、Spring 框架等完全陌生,我正在尝试使用注释 @Value通过 @Configuration 访问配置文件注释。 我在这里想要实现的目标是让控制台从配置文件中写出“hi”,通过
为此工作了几个小时。我完全被难住了。 这是 CS113 的实验室。 如果用户在程序(二进制计算器)结束时选择继续,我们需要使用 goto 语句来到达程序的顶部。 但是,我们还需要释放所有分配的内存。
我正在尝试使用 ffmpeg 库构建一个小的 C 程序。但是我什至无法使用 avformat_open_input() 打开音频文件设置检查错误代码的函数后,我得到以下输出: Error code:
使用 Spring Initializer 创建一个简单的 Spring boot。我只在可用选项下选择 DevTools。 创建项目后,无需对其进行任何更改,即可正常运行程序。 现在,当我尝试在项目
所以我只是在 Mac OS X 中通过 brew 安装了 qt。但是它无法链接它。当我尝试运行 brew link qt 或 brew link --overwrite qt 我得到以下信息: ton
我在提交和 pull 时遇到了问题:在提交的 IDE 中,我看到: warning not all local changes may be shown due to an error: unable
我跑 man gcc | grep "-L" 我明白了 Usage: grep [OPTION]... PATTERN [FILE]... Try `grep --help' for more inf
我有一段代码,旨在接收任何 URL 并将其从网络上撕下来。到目前为止,它运行良好,直到有人给了它这个 URL: http://www.aspensurgical.com/static/images/a
在过去的 5 个小时里,我一直在尝试在我的服务器上设置 WireGuard,但在完成所有设置后,我无法 ping IP 或解析域。 下面是服务器配置 [Interface] Address = 10.
我正在尝试在 GitLab 中 fork 我的一个私有(private)项目,但是当我按下 fork 按钮时,我会收到以下信息: No available namespaces to fork the
我这里遇到了一些问题。我是 node.js 和 Rest API 的新手,但我正在尝试自学。我制作了 REST API,使用 MongoDB 与我的数据库进行通信,我使用 Postman 来测试我的路
下面的代码在控制台中给出以下消息: Uncaught DOMException: Failed to execute 'appendChild' on 'Node': The new child el
我正在尝试调用一个新端点来显示数据,我意识到在上一组有效的数据中,它在数据周围用一对额外的“[]”括号进行控制台,我认为这就是问题是,而新端点不会以我使用数据的方式产生它! 这是 NgFor 失败的原
我正在尝试将我的 Symfony2 应用程序部署到我的 Azure Web 应用程序,但遇到了一些麻烦。 推送到远程时,我在终端中收到以下消息 remote: Updating branch 'mas
Minikube已启动并正在运行,没有任何错误,但是我无法 curl IP。我在这里遵循:https://docs.traefik.io/user-guide/kubernetes/,似乎没有提到关闭
每当我尝试docker组成任何项目时,都会出现以下错误。 我尝试过有和没有sudo 我在这台机器上只有这个问题。我可以在Mac和Amazon WorkSpace上运行相同的容器。 (myslabs)
我正在尝试 pip install stanza 并收到此消息: ERROR: No matching distribution found for torch>=1.3.0 (from stanza
DNS 解析看起来不错,但我无法 ping 我的服务。可能是什么原因? 来自集群中的另一个 Pod: $ ping backend PING backend.default.svc.cluster.l
我正在使用Hibernate 4 + Spring MVC 4当我开始 Apache Tomcat Server 8我收到此错误: Error creating bean with name 'wel
我是一名优秀的程序员,十分优秀!