I'm writing an application that displays Google Map right after the login screen. The Build Target is set to 2.3 and my devices run:
1) Samsung Galaxy Ace (2.3.4) 2) Motorolla Xoom (3.1)
I try my app on the Android emulator and it runs fine. Login and map display.
When I connected both devices via USB on my computer and selected them to run the application (note: there was an orange warning sign next to the build target when selecting), the login screen appears just fine and when I click "Sign In" the application crashes on Motorolla and freezes and for a while and then does nothing on my Galaxy Ace.
Note that they can both connect to the internet.
Logcat for the devices while running the app through USB connection:
1) Samsung Galaxy Ace
04-06 00:24:21.599: I/InputReader(162): dispatchTouch::touch event's action is 0
04-06 00:24:21.599: I/InputDispatcher(162): Delivering touch to current input target: action: 0, channel '40796920 com.cylbs.android/com.cylbs.android.MainActivity (server)'
04-06 00:24:21.699: I/InputReader(162): dispatchTouch::touch event's action is 1
04-06 00:24:21.699: I/InputDispatcher(162): Delivering touch to current input target: action: 1, channel '40796920 com.cylbs.android/com.cylbs.android.MainActivity (server)'
04-06 00:24:27.709: W/PowerManagerService(162): Timer 0x3->0x3|0x1
04-06 00:24:28.559: D/BatteryService(162): update start
04-06 00:24:28.569: D/BatteryService(162): update start
04-06 00:24:28.579: D/BatteryService(162): update start
2) Motorolla Xoom
04-06 00:27:13.490: D/AndroidRuntime(3163): Shutting down VM
04-06 00:27:13.490: W/dalvikvm(3163): threadid=1: thread exiting with uncaught exception (group=0x40154760)
04-06 00:27:13.510: E/AndroidRuntime(3163): FATAL EXCEPTION: main
04-06 00:27:13.510: E/AndroidRuntime(3163): android.os.NetworkOnMainThreadException
04-06 00:27:13.510: E/AndroidRuntime(3163): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
04-06 00:27:13.510: E/AndroidRuntime(3163): at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:368)
04-06 00:27:13.510: E/AndroidRuntime(3163): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:208)
04-06 00:27:13.510: E/AndroidRuntime(3163): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:431)
04-06 00:27:13.510: E/AndroidRuntime(3163): at java.net.Socket.connect(Socket.java:901)
04-06 00:27:13.510: E/AndroidRuntime(3163): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:75)
04-06 00:27:13.510: E/AndroidRuntime(3163): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
04-06 00:27:13.510: E/AndroidRuntime(3163): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:304)
04-06 00:27:13.510: E/AndroidRuntime(3163): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
04-06 00:27:13.510: E/AndroidRuntime(3163): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292)
04-06 00:27:13.510: E/AndroidRuntime(3163): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:274)
04-06 00:27:13.510: E/AndroidRuntime(3163): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:217)
04-06 00:27:13.510: E/AndroidRuntime(3163): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:624)
04-06 00:27:13.510: E/AndroidRuntime(3163): at com.cylbs.android.MainActivity.tryLogin(MainActivity.java:65)
04-06 00:27:13.510: E/AndroidRuntime(3163): at com.cylbs.android.MainActivity$1.onClick(MainActivity.java:41)
04-06 00:27:13.510: E/AndroidRuntime(3163): at android.view.View.performClick(View.java:3110)
04-06 00:27:13.510: E/AndroidRuntime(3163): at android.view.View$PerformClick.run(View.java:11928)
04-06 00:27:13.510: E/AndroidRuntime(3163): at android.os.Handler.handleCallback(Handler.java:587)
04-06 00:27:13.510: E/AndroidRuntime(3163): at android.os.Handler.dispatchMessage(Handler.java:92)
04-06 00:27:13.510: E/AndroidRuntime(3163): at android.os.Looper.loop(Looper.java:132)
04-06 00:27:13.510: E/AndroidRuntime(3163): at android.app.ActivityThread.main(ActivityThread.java:4025)
04-06 00:27:13.510: E/AndroidRuntime(3163): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 00:27:13.510: E/AndroidRuntime(3163): at java.lang.reflect.Method.invoke(Method.java:491)
04-06 00:27:13.510: E/AndroidRuntime(3163): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
04-06 00:27:13.510: E/AndroidRuntime(3163): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
04-06 00:27:13.510: E/AndroidRuntime(3163): at dalvik.system.NativeStart.main(Native Method)
This is my code for the MainActivity which is only Login and the Home activity which is where the map appears:
MainActivity:
package com.cylbs.android;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button login;
private EditText username, password;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
login = (Button) findViewById(R.id.logIn);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String mUsername = username.getText().toString();
String mPassword = password.getText().toString();
tryLogin(mUsername, mPassword);
}
});
}
public boolean tryLogin(String mUsername, String mPassword)
{
HttpURLConnection connection;
OutputStreamWriter request = null;
URL url = null;
String response = null;
String parameters = "username="+mUsername+"&password="+mPassword;
final TextView result = (TextView) findViewById(R.id.result);
try
{
url = new URL("http://10.0.2.2/database_test/index.php");
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
request = new OutputStreamWriter(connection.getOutputStream());
request.write(parameters);
request.flush();
request.close();
String line = "";
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line);
}
// Response from server after login process will be stored in response variable.
response = sb.toString();
// You can perform UI operations here
Toast.makeText(this,"."+ response +".", 0).show();
if (response.equals("Success"))
{
Session obj = new Session();
obj.setUsername(mUsername);
Intent myIntent = new Intent(MainActivity.this, Home.class);
startActivity(myIntent);
result.setText("Login successful");
}
else
{
result.setText("Login fail. Please try again");
}
isr.close();
reader.close();
}
catch(IOException e)
{
// Error
}
return true;
}
}
And the Home activity (extends MapActivity)
package com.cylbs.android;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Home extends MapActivity {
MapController mControl;
GeoPoint geoP;
MapView mapV;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
mapV = (MapView) findViewById(R.id.mapView);
mapV.displayZoomControls(true);
mapV.setBuiltInZoomControls(true);
double lat = 40.8;
double longi = -96.666;
geoP = new GeoPoint((int) (lat *1E6), (int) (longi *1E6));
mControl = mapV.getController();
mControl.animateTo(geoP);
mControl.setZoom(13);
Button emergencies = (Button) findViewById(R.id.emergencyButton);
emergencies.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Home.this, EmergencyList.class);
Home.this.startActivity(myIntent);
}
});
Button disruptions = (Button) findViewById(R.id.disruptionButton);
disruptions.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent2 = new Intent(Home.this, DisruptionList.class);
startActivity(myIntent2);
}
});
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
Any ideas? Thoughts? It's all for now. Thank you so much.
P.S. I'm a new android developer.
In case anyone else has the same issue I think I have found a solution.
I looked up the error in LogCat:
04-06 00:27:13.510: E/AndroidRuntime(3163): android.os.NetworkOnMainThreadException
And according to this http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
it seems that some (newer) android versions don't allow internet connection requests in the MainActivity or something like that.
Even though I was targetting android version 10 (2.3) I still had the error so I changed my project target to version 8 and it seems to work okay now.
I presuppose you are using a diferente Maps API KEY for emulating the app, and another different signed with your MD5 signature to run the app on devices. Check it out here.
I'm not sure I understand what you mean. Since it's the same application with the same code running on the emulator and the same exactly running on both my devices...
Also please note that when I first generated the Maps API KEY all I got was grey grids (on my emulator. still doesn't run on devices).
Then, I saw a CornBoyzAndroid tutorial on YouTube and I created a Maps API Key for debugging which allowed my map to load perfectly on the emulator, but still doesn't work on my devices - bubbly 2012-04-05 20:43
Orange sign is this: http://i44.tinypic.com/e9hk07.png
On the Ace when I click Sign In (the step before the map) it just doesn't respond, and then it responds but it is as if I haven't pressed anything. If I press it again it happens again. It does not pop up the "Force Close" message. The Motorolla does though...
The Ace gives up no errors on the LogCat... The Motorolla gives some but I don't really understand them. I saved them in 2 different .txt files. Download them below.
http://www.mediafire.com/?plmhhk9f3p4vfl - bubbly 2012-04-05 21:31
I think you didn't understand that I already did that. I tried with a key for debugging but also tried with a normal one if I can call it that.
http://stackoverflow.com/a/6278059/1306468 followed these instructions if it helps you understand what I did - bubbly 2012-04-06 00:19
I have posted the code you wanted - bubbly 2012-04-08 17:16