致命异常错误Android
Posted
技术标签:
【中文标题】致命异常错误Android【英文标题】:Fatal Exception error Android 【发布时间】:2014-03-07 07:37:49 【问题描述】:我正在尝试从我的 android 设备通过 XMPPClient
连接到开放式服务器。我在Async-Task
上单独运行Network thread
。
问题是
1- 我的代码不允许我连接到 openfire 服务器,但它可以轻松连接到 gmail 服务器。我的代码中是否缺少某些内容?
2-当我的系统连接到任何服务器(比如 gmail)时,它崩溃了,我在LogCat
中收到以下错误
错误
03-07 12:29:57.085: W/dalvikvm(26662): threadid=11: thread exiting with uncaught exception (group=0x40a95210)
03-07 12:29:57.115: E/AndroidRuntime(26662): FATAL EXCEPTION: AsyncTask #1
03-07 12:29:57.115: E/AndroidRuntime(26662): java.lang.RuntimeException: An error occured while executing doInBackground()
03-07 12:29:57.115: E/AndroidRuntime(26662): at android.os.AsyncTask$3.done(AsyncTask.java:278)
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-07 12:29:57.115: E/AndroidRuntime(26662): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.lang.Thread.run(Thread.java:856)
03-07 12:29:57.115: E/AndroidRuntime(26662): Caused by: java.lang.NullPointerException
03-07 12:29:57.115: E/AndroidRuntime(26662): at com.example.test.NetworkOperation.doInBackground(NetworkOperation.java:42)
03-07 12:29:57.115: E/AndroidRuntime(26662): at com.example.test.NetworkOperation.doInBackground(NetworkOperation.java:1)
03-07 12:29:57.115: E/AndroidRuntime(26662): at android.os.AsyncTask$2.call(AsyncTask.java:264)
03-07 12:29:57.115: E/AndroidRuntime(26662): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
这是在异步任务中运行的代码
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import android.os.AsyncTask;
import android.util.Log;
public class NetworkOperation extends AsyncTask<String, Void, String>
private XMPPClient xmppClient;
@Override
protected String doInBackground(String... urls)
String host = "talk.google.com"; //getText(R.id.host);
String port = "5222"; //getText(R.id.port);
String service = "gmail.com"; //getText(R.id.service);
String username = "hassaan.rabbani@gmail.com"; //getText(R.id.userid);
String password = "**********"; //getText(R.id.password);
// Create a connection
ConnectionConfiguration connConfig =
new ConnectionConfiguration(host, Integer.parseInt(port),service);
XMPPConnection connection = new XMPPConnection(connConfig);
try
connection.connect();
Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());
catch (XMPPException ex)
Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());
xmppClient.setConnection(null);
try
connection.login(username, password);
Log.i("XMPPClient", "Logged in as " + connection.getUser());
// Set the status to available
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
xmppClient.setConnection(connection);
catch (XMPPException ex)
Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + username);
xmppClient.setConnection(null);
return null;
@Override
protected void onPostExecute(String result)
我错过了什么吗?吗?
【问题讨论】:
我目前在你的代码中唯一不喜欢的是双try
块。如果第一个失败,第二个仍然执行。
在第二个 try 块中,执行此行 xmppClient.setConnection(connection); 后出现未捕获的异常;
【参考方案1】:
看起来你从来没有为你的 xmppClient 变量分配任何东西,因此它是空的。
你没有在第一次尝试中就成功了。
【讨论】:
【参考方案2】:试试下面的代码。
@Override
protected String doInBackground(String... urls)
String host = "talk.google.com"; //getText(R.id.host);
String port = "5222"; //getText(R.id.port);
String service = "gmail.com"; //getText(R.id.service);
String username = "hassaan.rabbani@gmail.com"; //getText(R.id.userid);
String password = "**********"; //getText(R.id.password);
// Create a connection
ConnectionConfiguration connConfig =
new ConnectionConfiguration(host, Integer.parseInt(port),service);
XMPPConnection connection = new XMPPConnection(connConfig);
if(xmppClient != null)
try
connection.connect();
Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());
connection.login(username, password);
Log.i("XMPPClient", "Logged in as " + connection.getUser());
// Set the status to available
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
xmppClient.setConnection(connection);
catch (XMPPException ex)
Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());
xmppClient.setConnection(null);
else
Log.d("Connection", "check your xmppClient");
return null;
【讨论】:
以上是关于致命异常错误Android的主要内容,如果未能解决你的问题,请参考以下文章