I have the following weird problem.
My main activity looks like this:
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);
}
}
I striped the code down to find the problem. The code above does nothing more then jumping directly to the XMPPClient activity. XMPPClient activity is using the asmack.jar library and that library is added to my eclipse build path.
The XMPPClient activity code:
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();
}
}
}
The problem occurs when I call functions from the asmack.jar lib. For instance the conn1.connect(); crashed my application over and over again. I get errors like:
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)
Manifest file:
<?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>
Anyone have an idea on what I’m doing wrong?
INFO: API level 14
Establishing a remote connection can take a long time.
You are trying to establish a connection directly from an activity and by default this activity is bound to the UI thread.
As a result, as long as your activity is trying to establish the connection, the user interface is blocked. The user cannot press any button and the screen is frozen.
By throwing a NetworkOnMainThreadException the android operating system is forcing you to change your implementation and is forcing you to establish your connection from another thread.
To summarise, you should not try to establish your remote connection from the UI thread. Try to establish it from a IntentService or a Service.
If you really want to establish it from an activity try to use an AsyncTask or a HandlerThread.
EDIT : Take a look at the android StrictMode :
It might be enabled for your application.