不幸的是<应用>已经停止 [英] Unfortunately <application> has stopped

查看:131
本文介绍了不幸的是<应用>已经停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序在用户提交了一些数据的形式然后将其发送到服务器。我对平板电脑和Android智能手机(GALAXY S2)测试。在平板电脑上,当我点击提交,应用程序将停止与信息工作不幸的是已停止工作。这个问题是没有看到任何电话或仿真器,它有我难住了。

I have an app where the user submits some data in a form which is then sent to a server. I am testing it on a tablet and an Android smartphone (Galaxy S2). On the tablet, as soon as I click on "Submit", the application stops working with the message "Unfortunately has stopped working". This problem is not seen on either the phone or the emulator, which has me stumped.

有一个在其中用户必须重新提交相同的凭证的选项该应用的另一个屏幕。有太多,同样会遇到问题。该应用程序的其他部分工程确定。这导致我得出结论,问题可能在于我将数据发送到服务器的方式。这code片断如下:

There is another screen in the app where the user has the option to re-submit the same credentials. There too, the same problem is encountered. The rest of the app works OK. This has led me to conclude that the problem might lie in the way I am sending data to the server. That code snippet is as follows:

//code to send to server should begin here.
        HttpClient hc = new DefaultHttpClient();
        HttpPost hp = new HttpPost("http://www.mywebsite.com/takeDetails.php");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            String val = "new";
            nameValuePairs.add(new BasicNameValuePair("mode", val));
            nameValuePairs.add(new BasicNameValuePair("name", name));
            nameValuePairs.add(new BasicNameValuePair("number", number));
            nameValuePairs.add(new BasicNameValuePair("email", emailID));
            Log.v(this.toString(), "Email = " + emailID);
            hp.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            // Execute HTTP Post Request
            HttpResponse response = hc.execute(hp);

            //Toast.makeText(getApplicationContext(), "Attempting to register.", Toast.LENGTH_LONG).show();

            String responseBody = EntityUtils.toString(response.getEntity());
            if(responseBody.contains("Success")) {
                Toast.makeText(getApplicationContext(), "Thank you for registering! You will receive an email with your username and password shortly.", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(), "Attempt to register failed.", Toast.LENGTH_LONG).show();
            }
            Log.v(this.toString(), "HTTP Response = " + responseBody);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }

logcat的输出:

Logcat output:

V/com.sriram.htmldisplay.htmlDisplay@4107bef0( 3766): Line read = Name: jguyjfhf
V/com.sriram.htmldisplay.htmlDisplay@4107bef0( 3766): Line read = Number: 668895898
V/com.sriram.htmldisplay.htmlDisplay@4107bef0( 3766): Line read = Email ID:jvjhfhc@ccf.mkj
V/com.sriram.htmldisplay.htmlDisplay@4107bef0( 3766): User details gleaned = Name = jguyjfhf
V/com.sriram.htmldisplay.htmlDisplay@4107bef0( 3766): 668895898
V/com.sriram.htmldisplay.htmlDisplay@4107bef0( 3766): jvjhfhc@ccf.mkj
V/com.sriram.htmldisplay.htmlDisplay@4107bef0( 3766): Email = jvjhfhc@ccf.mkj
D/AndroidRuntime( 3766): Shutting down VM
W/dalvikvm( 3766): threadid=1: thread exiting with uncaught exception (group=0x409f11f8)
E/AndroidRuntime( 3766): FATAL EXCEPTION: main
E/AndroidRuntime( 3766): android.os.NetworkOnMainThreadException
E/AndroidRuntime( 3766):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
E/AndroidRuntime( 3766):    at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
E/AndroidRuntime( 3766):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
E/AndroidRuntime( 3766):    at java.net.InetAddress.getAllByName(InetAddress.java:220)
E/AndroidRuntime( 3766):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
E/AndroidRuntime( 3766):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
E/AndroidRuntime( 3766):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
E/AndroidRuntime( 3766):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
E/AndroidRuntime( 3766):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
E/AndroidRuntime( 3766):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
E/AndroidRuntime( 3766):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
E/AndroidRuntime( 3766):    at com.sriram.htmldisplay.htmlDisplay.writeSendDetails(htmlDisplay.java:200)
E/AndroidRuntime( 3766):    at com.sriram.htmldisplay.htmlDisplay.access$10(htmlDisplay.java:127)
E/AndroidRuntime( 3766):    at com.sriram.htmldisplay.htmlDisplay$1.onClick(htmlDisplay.java:110)
E/AndroidRuntime( 3766):    at android.view.View.performClick(View.java:3511)
E/AndroidRuntime( 3766):    at android.view.View$PerformClick.run(View.java:14105)
E/AndroidRuntime( 3766):    at android.os.Handler.handleCallback(Handler.java:605)
E/AndroidRuntime( 3766):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 3766):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 3766):    at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime( 3766):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 3766):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 3766):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime( 3766):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime( 3766):    at dalvik.system.NativeStart.main(Native Method)
D/dalvikvm( 3766): GC_CONCURRENT freed 290K, 7% free 6697K/7175K, paused 4ms+6ms
W/ActivityManager( 1268):   Force finishing activity com.sriram.htmldisplay/.htmlDisplay
D/TabletStatusBar( 1340): hiding the MENU button
W/ActivityManager( 1268): Activity pause timeout for ActivityRecord{41406c60 com.sriram.htmldisplay/.htmlDisplay

我的提问:
1.有没有更好的办法处理HttpClient的?
错误 2.什么可仅使平板电脑失败的任何想法是最欢迎的。

My questions:
1. Is there a better way to handle errors from the HTTPClient?
2. Any ideas on what may be causing only the tablet to fail are most welcome.

推荐答案

NetworkOnMainThreadException:当应用程序试图在其主线程进行网络操作时引发的异常

NetworkOnMainThreadException: The exception that is thrown when an application attempts to perform a networking operation on its main thread.

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

StrictMode.setThreadPolicy(policy); 


或添加HTTP调用code到的AsyncTask


or add HTTP call code into AsyncTask

class RetreiveFeedTask extends AsyncTask<String, Void, RSSFeed> {

        private Exception exception;

        protected RSSFeed doInBackground(String... urls) {
            try {

                // add your all code here

            } catch (Exception e) {
                this.exception = e;
                return null;
            }
        }

        protected void onPostExecute(RSSFeed feed) {
            // TODO: check this.exception 
            // TODO: do something with the feed
        }
     }

执行AsyncTask的:

to execute the AsyncTask:

new RetreiveFeedTask().execute(urlToRssFeed);

希望你已经添加下面的权限在Android清单文件

hope you have added below permission in android manifest file

<uses-permission android:name="android.permission.INTERNET"/>

这篇关于不幸的是&lt;应用&GT;已经停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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