Android 应用程序无响应

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 应用程序无响应相关的知识,希望对你有一定的参考价值。

使用 Android 手机过程中有时使用某应用过程中,定时几秒出现xx无响应...,可是该应用明明正在响应,在 开发人员选项 中已经关闭了 应用程序无响应... 弹窗,没有变化。

一般如果应用程序遇到一些bug,就会自动退出,也就是无响应或者直接弹出对话框,这时候你需要从其应用或者重启软件,重置一下软件的状态。 参考技术A 应用程序无响应一般有三种类型:

一:KeyDispatchTimeout(5 seconds) --主要类型
按键或触摸事件在特定时间内无响应
2:BroadcastTimeout(10 seconds)
BroadcastReceiver在特定时间内无法处理完成
3:ServiceTimeout(20 seconds) --小概率类型
Service在特定的时间内无法处理完成
二:如何避免KeyDispatchTimeout
1:UI线程尽量只做跟UI相关的工作
2:耗时的工作(比如数据库操作,I/O,连接网络或者别的有可能阻碍UI线程的操作)把它放入单独的线程处理
3:尽量用Handler来处理UIthread和别的thread之间的交互追问

触摸有反应,大概每5秒弹一次,关闭软件重新打开就好了

参考技术B 有可能是手机QQ的问题 参考技术C 没刷好吧应该是你刷机文件有问题或者是刷的时候有东西损坏了中文的android下面“android系统”这个进程是包含了所有/system/app里面的程序,也就是系统文件夹下面的程序。应该是刷机的时候没刷好或者干脆就是文件有问题

无法连接到服务器。 XMPPException 服务器无响应

【中文标题】无法连接到服务器。 XMPPException 服务器无响应【英文标题】:Unable to connect to server. XMPPException No response from server 【发布时间】:2014-06-26 07:39:42 【问题描述】:

我在 xmpp 上使用 asmack 库进行 android 聊天。下面是我用来连接服务器的代码。但是该应用程序。但是我可以从桌面聊天登录。我无法理解问题所在。这是我使用的代码。

    public class MainActivity extends Activity implements MessageListener

    public static final String HOST = "mail.speedymx.com";
    public static final int PORT = 5222;
    //public static final String SERVICE = "testService";
    public static final String USERNAME = "username@speedymx.com";
    public static final String PASSWORD = "password";
        private Connection connection;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        System.setProperty("java.net.preferIPv6Addresses", "false");
        setContentView(R.layout.activity_main);
                connect();
        connection.disconnect();
        Log.i("MyActivity", "disconnected from server");
    

    public void connect() 

        final ProgressDialog dialog = ProgressDialog.show(this,
                "Connecting...", "Please wait...",false, true );
        new Thread(new Runnable() 
            @Override
            public void run() 
                // Create a connection
                System.setProperty("smack.debugEnabled", "true");
                ConnectionConfiguration connConfig = new ConnectionConfiguration(HOST, PORT);
                connConfig.setCompressionEnabled(true);
                connConfig.setReconnectionAllowed(true);
                connConfig.setDebuggerEnabled(true);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) 
                    connConfig.setTruststoreType("AndroidCAStore");
                    connConfig.setTruststorePassword(null);
                    connConfig.setTruststorePath(null);
                 else 
                    connConfig.setTruststoreType("BKS");
                    String path = System.getProperty("javax.net.ssl.trustStore");
                    if (path == null)
                        path = System.getProperty("java.home") + File.separator + "etc"
                            + File.separator + "security" + File.separator
                            + "cacerts.bks";
                    connConfig.setTruststorePath(path);
                

    connConfig.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
                SASLAuthentication.supportSASLMechanism("PLAIN");
                connConfig.setSASLAuthenticationEnabled(true);
                connection = new XMPPConnection(connConfig);
                try 
                    connection.connect();
                    Log.i("MyActivity","Connected to "+ connection.getHost()+"connection is secured"+ connection.isSecureConnection());
                
    catch(XMPPException e)
                
                    Log.i("MyActivity", "connection error"+e.toString());
                
                try
                
                    connection.login(USERNAME, PASSWORD);
                    Log.i("MyActivity","Connected to "+ connection.getUser());

                    runOnUiThread(new Runnable() 
                        public void run() 
                            dialog.dismiss();
                        
                    );
 
                catch (XMPPException ex) 
                    Log.e("MyActivity", "Xmpp exception "+ USERNAME+ ex.toString());

                    connection.disconnect();
                    Log.i("MyActivity", "disconnected from server");
                 
            `enter code here`catch (Exception ee) 
                    Log.e("MyActivity", ee.toString());
                    connection.disconnect();
                    Log.i("MyActivity", "disconnected from server");
                
            



        ).start();
        dialog.show();


        Presence presence = new Presence(Presence.Type.available);
          presence.setStatus("available");
          presence.setPriority(24);
         presence.setMode(Presence.Mode.available);
         connection.sendPacket(presence);

         sendmessgage();
private void sendmessgage() 
    // TODO Auto-generated method stub
    try 
    Chat chat = connection.getChatManager().createChat("samreshs@speedymx.com", this);
    chat.sendMessage("this is test message");
    
    catch (XMPPException e) 
        Log.i("MyActivity","Error Delivering block");
        connection.disconnect();
    


@Override
public void processMessage(Chat arg0, Message arg1) 
    // TODO Auto-generated method stub
    Log.i("MyActivity", "Received message is "+ arg1);



这里是日志以供参考

    06-26 11:37:44.558: D/dalvikvm(421): GC_FOR_ALLOC freed 31K, 6% free 12337K/12999K, paused 88ms
06-26 11:37:44.568: I/dalvikvm-heap(421): Grow heap (frag case) to 12.753MB for 358108-byte allocation
06-26 11:37:44.650: D/dalvikvm(421): GC_FOR_ALLOC freed <1K, 6% free 12686K/13383K, paused 64ms
06-26 11:37:44.837: D/dalvikvm(421): GC_FOR_ALLOC freed 215K, 6% free 12649K/13383K, paused 74ms
06-26 11:37:44.847: I/dalvikvm-heap(421): Grow heap (frag case) to 12.936MB for 229392-byte allocation
06-26 11:37:44.937: D/dalvikvm(421): GC_FOR_ALLOC freed <1K, 6% free 12872K/13639K, paused 80ms
06-26 11:37:45.068: D/dalvikvm(421): GC_FOR_ALLOC freed 129K, 6% free 12858K/13639K, paused 64ms
06-26 11:37:45.068: I/dalvikvm-heap(421): Grow heap (frag case) to 13.091MB for 178704-byte allocation
06-26 11:37:45.168: D/dalvikvm(421): GC_FOR_ALLOC freed 0K, 6% free 13032K/13831K, paused 83ms
06-26 11:37:45.318: D/dalvikvm(421): GC_FOR_ALLOC freed 98K, 7% free 12934K/13831K, paused 126ms
06-26 11:37:45.338: I/dalvikvm-heap(421): Grow heap (frag case) to 14.098MB for 1155904-byte allocation
06-26 11:37:45.448: D/dalvikvm(421): GC_FOR_ALLOC freed 0K, 7% free 14063K/14983K, paused 86ms
06-26 11:37:45.618: D/dalvikvm(421): GC_CONCURRENT freed <1K, 7% free 14063K/14983K, paused 7ms+3ms
06-26 11:37:45.899: D/dalvikvm(421): GC_FOR_ALLOC freed <1K, 7% free 14063K/14983K, paused 121ms
06-26 11:37:45.918: I/dalvikvm-heap(421): Grow heap (frag case) to 16.058MB for 2054928-byte allocation
06-26 11:37:46.052: D/dalvikvm(421): GC_FOR_ALLOC freed 0K, 6% free 16070K/17031K, paused 71ms
06-26 11:37:46.238: D/dalvikvm(421): GC_CONCURRENT freed <1K, 6% free 16070K/17031K, paused 7ms+13ms
06-26 11:37:46.477: D/AndroidRuntime(421): Shutting down VM
06-26 11:37:46.477: W/dalvikvm(421): threadid=1: thread exiting with uncaught exception (group=0x40014760)
06-26 11:37:46.508: E/AndroidRuntime(421): FATAL EXCEPTION: main
06-26 11:37:46.508: E/AndroidRuntime(421): java.lang.RuntimeException: Unable to start activity ComponentInfocom.org.ChatBoss/com.org.ChatBoss.MainActivity: java.lang.NullPointerException
06-26 11:37:46.508: E/AndroidRuntime(421):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
06-26 11:37:46.508: E/AndroidRuntime(421):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
06-26 11:37:46.508: E/AndroidRuntime(421):  at android.app.ActivityThread.access$1500(ActivityThread.java:122)
06-26 11:37:46.508: E/AndroidRuntime(421):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
06-26 11:37:46.508: E/AndroidRuntime(421):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 11:37:46.508: E/AndroidRuntime(421):  at android.os.Looper.loop(Looper.java:132)
06-26 11:37:46.508: E/AndroidRuntime(421):  at android.app.ActivityThread.main(ActivityThread.java:4025)
06-26 11:37:46.508: E/AndroidRuntime(421):  at java.lang.reflect.Method.invokeNative(Native Method)
06-26 11:37:46.508: E/AndroidRuntime(421):  at java.lang.reflect.Method.invoke(Method.java:491)
06-26 11:37:46.508: E/AndroidRuntime(421):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-26 11:37:46.508: E/AndroidRuntime(421):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-26 11:37:46.508: E/AndroidRuntime(421):  at dalvik.system.NativeStart.main(Native Method)
06-26 11:37:46.508: E/AndroidRuntime(421): Caused by: java.lang.NullPointerException
06-26 11:37:46.508: E/AndroidRuntime(421):  at com.org.ChatBoss.MainActivity.connect(MainActivity.java:263)
06-26 11:37:46.508: E/AndroidRuntime(421):  at com.org.ChatBoss.MainActivity.onCreate(MainActivity.java:75)
06-26 11:37:46.508: E/AndroidRuntime(421):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
06-26 11:37:46.508: E/AndroidRuntime(421):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
06-26 11:37:46.508: E/AndroidRuntime(421):  ... 11 more
06-26 11:37:47.188: D/SMACK(421): 11:37:47 AM SENT (1087309296): <stream:stream to="mail.speedymx.com" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0">
06-26 11:37:47.418: D/SMACK(421): 11:37:47 AM RCV  (1087309296): <?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='2134678635' from='speedymx.com' xml:lang='en'><stream:error><host-unknown xmlns='urn:ietf:params:xml:ns:xmpp-streams'/></stream:error></stream:stream>
06-26 11:37:47.488: D/SMACK(421): 11:37:47 AM SENT (1087309296): <presence id="NREU4-0" type="unavailable"></presence>
06-26 11:37:47.508: I/MyActivity(421): Connected to mail.speedymx.comconnection is securedfalse
06-26 11:37:47.518: D/SMACK(421): 11:37:47 AM SENT (1087309296): <iq id="NREU4-1" type="get"><query xmlns="jabber:iq:auth"><username>username@speedymx.com</username></query></iq>
06-26 11:37:47.528: D/SMACK(421): 11:37:47 AM SENT (1087309296): </stream:stream>
06-26 11:37:47.678: W/System.err(421): stream:error (host-unknown)
06-26 11:37:47.678: W/System.err(421):  at org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:255)
06-26 11:37:47.678: W/System.err(421):  at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:43)
06-26 11:37:47.678: W/System.err(421):  at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:69)
06-26 11:37:52.574: E/MyActivity(421): Xmpp exception No response from the server.: 
06-26 11:37:52.787: I/MyActivity(421): disconnected from server
06-26 11:37:53.878: I/Process(421): Sending signal. PID: 421 SIG: 9

【问题讨论】:

尝试使用断点调试代码并确保服务器正在运行 实际上您已连接到您的服务器。看到这一行 -> 06-26 11:37:47.508: I/MyActivity(421): Connected to mail.speedymx.comconnection issecuredfalse 试过了.. connection.login() 出现错误。我只是想知道是否缺少某些东西。是的,通过从桌面手动登录来确保服务器正常工作 @WaqarAhmed 我使用过 connConfig.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);仍然当我打印 connection.isSecured 它给出错误。我不知道为什么 您使用的是哪个 aSmack 版本? 【参考方案1】:

日志中的重要信息是:

<host-unknown xmlns='urn:ietf:params:xml:ns:xmpp-streams'/>

这是因为你告诉服务器你正在连接到mail.speedymx.com,但服务器只知道speedymx.com。您应该将HOST 设置为正确的值。

如果 speedymx.com 的 XMPP 服务器实际上在 mail.speedymx.com 上运行,那么您应该添加 SRV 记录来表明这一点。

【讨论】:

在相同的代码中,我在 conncetion.sendpacket(presence) 上获得了 nullpointereception。你能建议如何删除它 尝试正确的拼写连接? 我只是在写评论时拼错了.. 请参考代码 Presence presence = new Presence(Presence.Type.available);存在.setStatus(“可用”);存在.setPriority(24); Presence.setMode(Presence.Mode.available); connection.sendPacket(存在);我使用了正确的拼写 我找到了答案.. 在主类上实现 messgae 监听器产生了问题

以上是关于Android 应用程序无响应的主要内容,如果未能解决你的问题,请参考以下文章

QuickBlox Android 无响应

Android ANR的产生与分析

浅析 Android 系统稳定性中应用程序 ANR 无响应的原因

某些设备按下按钮时获取应用程序中的位置时Android无响应

应用程序在 Python2 和 Python3 中的 Windows 上运行良好,但收到错误:无法导入 android,并且按钮无响应

android Notification 点击事情无响应的注意事项