我的Andr​​oid应用程序将无法在2设备上运行,但适用于模拟器 [英] My android application won't run on 2 devices but works on emulator

查看:226
本文介绍了我的Andr​​oid应用程序将无法在2设备上运行,但适用于模拟器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写的登录界面后立即显示谷歌地图的应用程序。
构建目标设定为2.3和我的设备上运行:

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)三星Galaxy王牌(2.3.4)
2)Motorolla的Xoom(3.1)

1) Samsung Galaxy Ace (2.3.4) 2) Motorolla Xoom (3.1)

我尽我的Andr​​oid模拟器应用程序,它运行正常。登录和地图显示。

I try my app on the Android emulator and it runs fine. Login and map display.

当我通过我的计算机上的U​​SB连接两个设备,并选择他们来运行应用程序(注:旁边的构建目标选择时有一个橙色的警告标志),在登录屏幕出现就好了,当我点击登录在应用程序崩溃的Motorolla和冻结和了一会儿,然后做我的银河王牌什么都没有。

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的设备,同时贯穿USB连接的应用程序:

Logcat for the devices while running the app through USB connection:

1)三星Galaxy王牌

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)的Xoom Motorolla

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)

这是我的code为在MainActivity仅在登录和家庭活动这是哪里的地图显示:

This is my code for the MainActivity which is only Login and the Home activity which is where the map appears:

MainActivity:

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

}

和家庭活动(​​延伸MapActivity)

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。
我是一个新的Andr​​oid开发者。

P.S. I'm a new android developer.

推荐答案

在情况下,任何人有同样的问题,我想我已经找到了解决办法。

In case anyone else has the same issue I think I have found a solution.

我LogCat中查找错误:

I looked up the error in LogCat:

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

和根据此的http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

似乎有些(更新)的Andr​​oid版本不允许在MainActivity或类似的东西互联网的连接请求。

it seems that some (newer) android versions don't allow internet connection requests in the MainActivity or something like that.

虽然我打靶Android版本10(2.3)我仍然有错误,所以我改变了我的项目目标8版本,它似乎是现在的好工作。

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.

这篇关于我的Andr​​oid应用程序将无法在2设备上运行,但适用于模拟器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆