0

我正在尝试将坐标从 android mobile 发送到 MySQL wamp 服务器。它在台式计算机上运行良好,但是当我将其移至笔记本电脑时,会出现异常,即我的活动没有响应。我试图通过在 onclicklistener 方法之前添加一条 toast 消息来修复它。它运行良好,但第二个 toast 不起作用(第二个在 onclick 方法块中)。我该如何解决这个问题?

编码:

Toast.makeText(getApplicationContext(), "msg 1", Toast.LENGTH_SHORT).show();
        btnShowLocation = (Button) findViewById(R.id.btnShowLocation);

        // show location button click event
        btnShowLocation.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {        
                // create class object
                Toast.makeText(getApplicationContext(), "msg 2", Toast.LENGTH_SHORT).show();

                gps = new GPSTracker(AndroidGPSTrackingActivity.this);

                // check if GPS enabled     
                if(gps.canGetLocation()){


                    String latitude = String.valueOf(gps.getLatitude());
                    String longitude = String.valueOf(gps.getLongitude());

                    // Building Parameters
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("latitude", latitude));
                    params.add(new BasicNameValuePair("longitude", longitude));                 
                    // \n is for new line
                    JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                            "POST", params);
                    Log.d("Create Response", json.toString());

                    Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();    
                }else{
                    // can't get location
                    // GPS or Network is not enabled
                    // Ask user to enable GPS/network in settings
                    gps.showSettingsAlert();
                }

            }
        });

第二个:

public void showSettingsAlert(){
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

        // Setting Dialog Title
        alertDialog.setTitle("GPS is settings");

        // Setting Dialog Message
        alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

        // On pressing Settings button
        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mContext.startActivity(intent);
            }
        });

        // on pressing cancel button
        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
            }
        });

        // Showing Alert Message
        alertDialog.show();
    }

按下按钮后的logcat:

05-12 00:03:30.709: ERROR/ActivityManager(59): ANR in com.example.gpstracking (com.example.gpstracking/.AndroidGPSTrackingActivity)
05-12 00:03:30.709: ERROR/ActivityManager(59): Reason: keyDispatchingTimedOut
05-12 00:03:30.709: ERROR/ActivityManager(59): Load: 0.67 / 1.06 / 0.49
05-12 00:03:30.709: ERROR/ActivityManager(59): CPU usage from 7935ms to 64ms ago:
05-12 00:03:30.709: ERROR/ActivityManager(59):   system_server: 15% = 7% user + 8% kernel / faults: 172 minor 4 major
05-12 00:03:30.709: ERROR/ActivityManager(59):   ple.gpstracking: 5% = 0% user + 5% kernel / faults: 28 minor 5 major
05-12 00:03:30.709: ERROR/ActivityManager(59):   adbd: 3% = 0% user + 3% kernel
05-12 00:03:30.709: ERROR/ActivityManager(59):   m.android.phone: 1% = 0% user + 1% kernel / faults: 139 minor
05-12 00:03:30.709: ERROR/ActivityManager(59):   logcat: 1% = 0% user + 0% kernel
05-12 00:03:30.709: ERROR/ActivityManager(59):   ronsoft.openwnn: 0% = 0% user + 0% kernel / faults: 93 minor
05-12 00:03:30.709: ERROR/ActivityManager(59):   id.defcontainer: 0% = 0% user + 0% kernel / faults: 86 minor
05-12 00:03:30.709: ERROR/ActivityManager(59):   zygote: 0% = 0% user + 0% kernel / faults: 67 minor
05-12 00:03:30.709: ERROR/ActivityManager(59):   m.android.email: 0% = 0% user + 0% kernel / faults: 123 minor 2 major
05-12 00:03:30.709: ERROR/ActivityManager(59):   m.android.music: 0% = 0% user + 0% kernel / faults: 78 minor
05-12 00:03:30.709: ERROR/ActivityManager(59):   android.protips: 0% = 0% user + 0% kernel / faults: 87 minor
05-12 00:03:30.709: ERROR/ActivityManager(59):   .quicksearchbox: 0% = 0% user + 0% kernel / faults: 94 minor
05-12 00:03:30.709: ERROR/ActivityManager(59):   com.svox.pico: 0% = 0% user + 0% kernel / faults: 59 minor
05-12 00:03:30.709: ERROR/ActivityManager(59):   kswapd0: 0% = 0% user + 0% kernel
05-12 00:03:30.709: ERROR/ActivityManager(59):   ndroid.settings: 0% = 0% user + 0% kernel / faults: 112 minor
05-12 00:03:30.709: ERROR/ActivityManager(59):   d.process.acore: 0% = 0% user + 0% kernel / faults: 84 minor
05-12 00:03:30.709: ERROR/ActivityManager(59):   roid.alarmclock: 0% = 0% user + 0% kernel / faults: 85 minor
05-12 00:03:30.709: ERROR/ActivityManager(59):   com.android.mms: 0% = 0% user + 0% kernel / faults: 95 minor 1 major
05-12 00:03:30.709: ERROR/ActivityManager(59):   qemud: 0% = 0% user + 0% kernel
05-12 00:03:30.709: ERROR/ActivityManager(59):   ndroid.launcher: 0% = 0% user + 0% kernel / faults: 60 minor
05-12 00:03:30.709: ERROR/ActivityManager(59): TOTAL: 37% = 11% user + 25% kernel + 0% irq + 0% softirq
05-12 00:03:30.999: DEBUG/dalvikvm(59): GC_FOR_MALLOC freed 4749 objects / 237112 bytes in 237ms
05-12 00:03:31.039: WARN/WindowManager(59): No window to dispatch pointer action 1
05-12 00:03:31.289: DEBUG/dalvikvm(59): GC_FOR_MALLOC freed 346 objects / 19296 bytes in 163ms
05-12 00:03:31.519: DEBUG/dalvikvm(59): GC_FOR_MALLOC freed 313 objects / 14056 bytes in 214ms
05-12 00:03:31.519: INFO/dalvikvm-heap(59): Grow heap (frag case) to 4.862MB for 83464-byte allocation
05-12 00:03:31.739: DEBUG/dalvikvm(59): GC_FOR_MALLOC freed 86 objects / 4952 bytes in 206ms
05-12 00:03:31.949: DEBUG/dalvikvm(59): GC_FOR_MALLOC freed 45 objects / 86216 bytes in 196ms
05-12 00:03:31.949: INFO/dalvikvm-heap(59): Grow heap (frag case) to 4.974MB for 166912-byte allocation
05-12 00:03:32.159: DEBUG/dalvikvm(59): GC_FOR_MALLOC freed 10 objects / 464 bytes in 204ms
05-12 00:03:32.399: DEBUG/dalvikvm(59): GC_FOR_MALLOC freed 62 objects / 231752 bytes in 212ms
05-12 00:03:32.659: DEBUG/dalvikvm(59): GC_FOR_MALLOC freed 58 objects / 173848 bytes in 207ms
05-12 00:03:32.759: WARN/WindowManager(59): No window to dispatch pointer action 1
05-12 00:03:32.759: WARN/WindowManager(59): No window to dispatch pointer action 0
05-12 00:03:32.769: WARN/WindowManager(59): No window to dispatch pointer action 1
05-12 00:03:32.769: WARN/WindowManager(59): No window to dispatch pointer action 0
05-12 00:03:32.788: WARN/WindowManager(59): No window to dispatch pointer action 1
05-12 00:03:32.788: WARN/WindowManager(59): No window to dispatch pointer action 0
05-12 00:03:32.809: WARN/WindowManager(59): No window to dispatch pointer action 1
05-12 00:03:32.809: WARN/WindowManager(59): No window to dispatch pointer action 0
05-12 00:03:32.819: WARN/WindowManager(59): No window to dispatch pointer action 1
05-12 00:03:32.819: WARN/WindowManager(59): No window to dispatch pointer action 0
05-12 00:03:32.829: WARN/WindowManager(59): No window to dispatch pointer action 1
05-12 00:03:32.841: WARN/WindowManager(59): No window to dispatch pointer action 0
05-12 00:03:32.841: WARN/WindowManager(59): No window to dispatch pointer action 1
05-12 00:03:32.849: WARN/WindowManager(59): No window to dispatch pointer action 0
05-12 00:03:32.859: WARN/WindowManager(59): No window to dispatch pointer action 1
05-12 00:03:32.859: WARN/WindowManager(59): No window to dispatch pointer action 0
05-12 00:03:32.869: WARN/WindowManager(59): No window to dispatch pointer action 1
05-12 00:03:32.869: WARN/WindowManager(59): No window to dispatch pointer action 0
05-12 00:03:32.869: WARN/WindowManager(59): No window to dispatch pointer action 1
05-12 00:03:32.869: WARN/WindowManager(59): No window to dispatch pointer action 0
05-12 00:03:32.869: WARN/WindowManager(59): No window to dispatch pointer action 1
05-12 00:03:32.869: WARN/WindowManager(59): No window to dispatch pointer action 0
05-12 00:03:32.889: WARN/WindowManager(59): No window to dispatch pointer action 1
05-12 00:03:32.889: WARN/WindowManager(59): No window to dispatch pointer action 0
05-12 00:03:32.889: WARN/WindowManager(59): No window to dispatch pointer action 1
05-12 00:03:32.889: WARN/WindowManager(59): No window to dispatch pointer action 0
05-12 00:03:32.898: WARN/WindowManager(59): No window to dispatch pointer action 1
05-12 00:03:32.898: WARN/WindowManager(59): No window to dispatch pointer action 0
05-12 00:03:32.909: WARN/WindowManager(59): No window to dispatch pointer action 1
05-12 00:03:32.909: WARN/WindowManager(59): No window to dispatch pointer action 0
05-12 00:03:32.909: WARN/WindowManager(59): No window to dispatch pointer action 1
05-12 00:03:33.309: INFO/ARMAssembler(59): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x22b838:0x22b8f4] in 763754 ns
05-12 00:04:32.018: WARN/System.err(279): java.net.SocketException: The operation timed out
05-12 00:04:32.048: WARN/System.err(279):     at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocketImpl(Native Method)
05-12 00:04:32.048: WARN/System.err(279):     at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:115)
05-12 00:04:32.058: WARN/System.err(279):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:244)
05-12 00:04:32.058: WARN/System.err(279):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:533)
05-12 00:04:32.069: WARN/System.err(279):     at java.net.Socket.connect(Socket.java:1055)
05-12 00:04:32.069: WARN/System.err(279):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
05-12 00:04:32.078: WARN/System.err(279):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
05-12 00:04:32.088: WARN/System.err(279):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-12 00:04:32.088: WARN/System.err(279):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-12 00:04:32.088: WARN/System.err(279):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
05-12 00:04:32.098: WARN/System.err(279):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-12 00:04:32.098: WARN/System.err(279):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-12 00:04:32.098: WARN/System.err(279):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-12 00:04:32.118: WARN/System.err(279):     at com.example.gpstracking.JSONParser.makeHttpRequest(JSONParser.java:51)
05-12 00:04:32.118: WARN/System.err(279):     at com.example.gpstracking.AndroidGPSTrackingActivity$1.onClick(AndroidGPSTrackingActivity.java:63)
05-12 00:04:32.128: WARN/System.err(279):     at android.view.View.performClick(View.java:2408)
05-12 00:04:32.128: WARN/System.err(279):     at android.view.View$PerformClick.run(View.java:8816)
05-12 00:04:32.128: WARN/System.err(279):     at android.os.Handler.handleCallback(Handler.java:587)
05-12 00:04:32.158: WARN/System.err(279):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-12 00:04:32.158: WARN/System.err(279):     at android.os.Looper.loop(Looper.java:123)
05-12 00:04:32.158: WARN/System.err(279):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-12 00:04:32.158: WARN/System.err(279):     at java.lang.reflect.Method.invokeNative(Native Method)
05-12 00:04:32.169: WARN/System.err(279):     at java.lang.reflect.Method.invoke(Method.java:521)
05-12 00:04:32.169: WARN/System.err(279):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-12 00:04:32.169: WARN/System.err(279):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-12 00:04:32.169: WARN/System.err(279):     at dalvik.system.NativeStart.main(Native Method)
05-12 00:04:32.188: ERROR/Buffer Error(279): Error converting result java.lang.NullPointerException
05-12 00:04:32.268: ERROR/JSON Parser(279): Error parsing data org.json.JSONException: End of input at character 0 of 
05-12 00:04:32.268: DEBUG/AndroidRuntime(279): Shutting down VM
05-12 00:04:32.278: WARN/dalvikvm(279): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-12 00:04:32.308: ERROR/AndroidRuntime(279): FATAL EXCEPTION: main
05-12 00:04:32.308: ERROR/AndroidRuntime(279): java.lang.NullPointerException
05-12 00:04:32.308: ERROR/AndroidRuntime(279):     at com.example.gpstracking.AndroidGPSTrackingActivity$1.onClick(AndroidGPSTrackingActivity.java:65)
05-12 00:04:32.308: ERROR/AndroidRuntime(279):     at android.view.View.performClick(View.java:2408)
05-12 00:04:32.308: ERROR/AndroidRuntime(279):     at android.view.View$PerformClick.run(View.java:8816)
05-12 00:04:32.308: ERROR/AndroidRuntime(279):     at android.os.Handler.handleCallback(Handler.java:587)
05-12 00:04:32.308: ERROR/AndroidRuntime(279):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-12 00:04:32.308: ERROR/AndroidRuntime(279):     at android.os.Looper.loop(Looper.java:123)
05-12 00:04:32.308: ERROR/AndroidRuntime(279):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-12 00:04:32.308: ERROR/AndroidRuntime(279):     at java.lang.reflect.Method.invokeNative(Native Method)
05-12 00:04:32.308: ERROR/AndroidRuntime(279):     at java.lang.reflect.Method.invoke(Method.java:521)
05-12 00:04:32.308: ERROR/AndroidRuntime(279):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-12 00:04:32.308: ERROR/AndroidRuntime(279):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-12 00:04:32.308: ERROR/AndroidRuntime(279):     at dalvik.system.NativeStart.main(Native Method)
05-12 00:04:32.358: WARN/ActivityManager(59):   Force finishing activity com.example.gpstracking/.AndroidGPSTrackingActivity
05-12 00:04:32.948: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{44f8abd8 com.example.gpstracking/.AndroidGPSTrackingActivity}
05-12 00:04:43.728: WARN/ActivityManager(59): Activity destroy timeout for HistoryRecord{44f8abd8 com.example.gpstracking/.AndroidGPSTrackingActivity}
05-12 00:05:50.939: DEBUG/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol
05-12 00:09:32.479: INFO/Process(279): Sending signal. PID: 279 SIG: 9
05-12 00:09:33.379: INFO/ActivityManager(59): Process com.example.gpstracking (pid 279) has died.
05-12 00:09:33.398: INFO/WindowManager(59): WIN DEATH: Window{4500db48 com.example.gpstracking/com.example.gpstracking.AndroidGPSTrackingActivity paused=false}
05-12 00:09:33.458: WARN/InputManagerService(59): Got RemoteException sending setActive(false) notification to pid 279 uid 10032
05-12 00:09:33.678: WARN/GpsLocationProvider(59): Unneeded remove listener for uid 1000
4

2 回答 2

1
 JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                            "POST", params);

您正在 UI 线程上执行 HTTP 请求。这可能是您 ANR 的原因,因为例如,服务器可能会缓慢返回响应。移动异步任务中的所有阻塞调用。所有长处理方法都应该以异步方式执行。读这个

于 2013-05-12T14:15:05.313 回答
0

你得到 SocketException: The operation timed out。这意味着笔记本电脑中的连接速度很慢或无法访问。

此外,您正在主线程中创建一个 HttpRequest。为连接目的启动一个单独的线程。

于 2013-05-12T14:24:33.780 回答