我有以下奇怪的问题。
我的主要活动如下所示:
public class CityGame extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent myIntent = new Intent(this, XMPPClient.class);
startActivity(myIntent);
}
}
我删除了代码以找出问题所在。上面的代码只是直接跳转到 XMPPClient 活动。XMPPClient 活动正在使用 asmack.jar 库,并且该库已添加到我的 Eclipse 构建路径中。
XMPPClient 活动代码:
public class XMPPClient extends Activity {
/**
* Called with the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("XMPPClient", "onCreate called");
//Create a connection to the jabber.org server.
Connection conn1 = new XMPPConnection("jabber.org");
try {
conn1.connect();
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
当我从 asmack.jar 库中调用函数时会出现问题。例如,conn1.connect();
我的应用程序一次又一次地崩溃。我收到如下错误:
06-07 09:05:03.085: E/AndroidRuntime(538): FATAL EXCEPTION: main
06-07 09:05:03.085: E/AndroidRuntime(538): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.joop.citygame/com.joop.citygame.XMPPClient}: android.os.NetworkOnMainThreadException
06-07 09:05:03.085: E/AndroidRuntime(538): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
06-07 09:05:03.085: E/AndroidRuntime(538): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
06-07 09:05:03.085: E/AndroidRuntime(538): at android.app.ActivityThread.access$600(ActivityThread.java:122)
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.joop.citygame"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk android:minSdkVersion="14" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".CityGame" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="@string/app_name"
android:name=".XMPPClient" >
<intent-filter>
<action android:name="com.joop.citygame.XMPPClient" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
有人知道我做错了什么吗?
信息:API 级别 14