0

我正在编写一个在登录屏幕后立即显示 Google Map 的应用程序。构建目标设置为 2.3,我的设备运行:

1) 三星 Galaxy Ace (2.3.4) 2) 摩托罗拉 Xoom (3.1)

我在 Android 模拟器上尝试了我的应用程序,它运行良好。登录和地图显示。

当我通过计算机上的 USB 连接两个设备并选择它们来运行应用程序时(注意:选择时构建目标旁边有一个橙色警告标志),登录屏幕显示得很好,当我单击“登录”时应用程序在摩托罗拉上崩溃并冻结了一段时间,然后在我的 Galaxy Ace 上什么也不做。

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

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

1) 三星银河王牌

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) 摩托罗拉 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 活动,它是地图出现的地方:

主要活动:

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

有任何想法吗?想法?这就是现在。非常感谢。

PS我是一个新的安卓开发者。

4

2 回答 2

1

我假设您正在使用不同的 Maps API KEY 来模拟应用程序,并使用另一个不同的方式使用您的 MD5 签名来在设备上运行应用程序。在这里查看。

于 2012-04-05T16:52:47.607 回答
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,现在它似乎可以正常工作了。

于 2012-04-11T11:20:50.940 回答