我的android应用程序不能在2台设备上运行,但可以在模拟器上运行

时间:2012-04-05 16:36:58

标签: android maps avd

我正在编写一个在登录屏幕后立即显示Google地图的应用程序。 Build Target设置为2.3,我的设备运行:

1)三星Galaxy Ace(2.3.4) 2)Motorolla Xoom(3.1)

我在Android模拟器上试用我的应用程序,运行正常。登录和地图显示。

当我通过计算机上的USB连接两个设备并选择它们来运行应用程序时(注意:选择时,构建目标旁边有一个橙色警告标志),登录屏幕显示正常,当我点击“登录”时在“应用程序在Motorolla上崩溃并冻结了一段时间然后在我的Galaxy Ace上没有任何作用。

请注意,他们都可以连接到互联网。

通过USB连接运行应用程序时设备的Logcat:

1)三星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)

这是我的MainActivity的代码,它只是Login和Home活动,它是地图出现的地方:

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;
    }

}

Home活动(扩展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;
    }
}

有什么想法吗?思考? 这一切都是现在。 非常感谢你。

P.S。 我是一个新的Android开发人员。

2 个答案:

答案 0 :(得分:1)

我预先假设您使用的是差异化的Maps API KEY来模拟应用,而另一种不同的MD5签名用于在设备上运行应用。请查看here

答案 1 :(得分:1)

如果其他人有同样的问题我认为我找到了解决方案。

我查看了LogCat中的错误:

04-06 00:27:13.510: E/AndroidRuntime(3163): android.os.NetworkOnMainThreadException

根据这个http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

似乎某些(较新的)Android版本不允许在MainActivity中使用互联网连接请求。

即使我的目标是Android版本10(2.3),我仍然遇到错误,所以我将项目目标更改为版本8,现在看起来工作正常。