如何prevent我从强制关闭Android应用? [英] how to prevent my android app from force closing?

查看:362
本文介绍了如何prevent我从强制关闭Android应用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序的用户界面有四个按钮外面的三个按钮(点击时)从MySQL数据库检索图像,并在Android手机上显示出来。第四个按钮的工作是检索数据库的文本。

如果任何检索图像按钮被点击第一个图像displayed.and那么如果检索按钮的图像被点击时显示的文本。

但是,当点击第一个检索的文本按钮显示的图像的任何按钮被点击应用程序强制关闭!!甚至在

该按钮的onclick活动code检索全文如下

  b.setOnClickListener(新OnClickListener(){        公共无效的onClick(查看为arg0){
            Toast.LENGTH_SHORT).show();            尝试{
                data.put(ID,身份证);
            }赶上(JSONException E1){
                // TODO自动生成catch块
                e1.printStackTrace();
            }
            字符串URL =htt​​p://10.0.2.2:8080/finder/review/listA;            尝试{
                img.setImageBitmap(NULL);
                JSON = HTTPPoster.doPost(URL,数据);                Toast.makeText(getBaseContext(),json.toString(),Toast.LENGTH_SHORT).show();
                 标题= json.getJSONObject(标题);
                 Toast.makeText(getBaseContext(),titles.toString(),Toast.LENGTH_SHORT).show();
                 封邮件= json.getJSONObject(封邮件);
                 名称= json.getJSONObject(名字);            串的一个= titles.getString(0);
            Toast.makeText(getBaseContext(),one.toString(),Toast.LENGTH_LONG).show();
            t.se​​tText(\\ nReviews \\ n);
            字符串内容=;
            的for(int i = 0;我3;;我++)
            {
                弦乐味精= msgs.getString(1 +);
                 名称= names.getString(1 +);
                字符串title = titles.getString(1 +);                内容= t.getText()+(\\ n名称+名字+\\ n标题:+标题+\\ n消息:+味精);
                t.se​​tText(内容);                Toast.makeText(getBaseContext(),姓名:+姓名+\\ n标题:+标题+\\ n消息+味精,Toast.LENGTH_SHORT).show();
            }                // t.setText()
            }赶上(ClientProtocolException E){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(JSONException E){
                // TODO自动生成catch块
                e.printStackTrace();
            }
        }
    });

logcat的

  06-07 00:25:29.923:信息/流程(234):发送信号。 PID:234 SIG:9
06-07 00:25:30.042:信息/窗口管理器(58):WIN死亡:窗口{44e66ef0 com.lpss / com.lpss.MyList暂停= FALSE}
06-07 00:25:30.053:信息/窗口管理器(58):WIN死亡:窗口{44d72da0 com.lpss / com.lpss.lpss暂停= FALSE}
06-07 00:25:30.053:信息/ ActivityManager(58):过程com.lpss(PID 234)已经死亡。
06-07 00:25:30.062:WARN / GpsLocationProvider(58):不需要的删除UID 1000监听器
06-07 00:25:30.062:DEBUG / GpsLocationProvider(58):stopNavigating
06-07 00:25:30.062:信息/窗口管理器(58):WIN死亡:窗口{44dedb10吐司暂停= FALSE}
06-07 00:25:30.091:在5000毫秒坠毁服务com.lpss调度重启/ .NotifyService:WARN / ActivityManager(58)
06-07 00:25:30.103:信息/窗口管理器(58):WIN死亡:窗口{44dd9490 com.lpss / com.lpss.Contents暂停= FALSE}
06-07 00:25:30.244:信息/ ActivityManager(58):启动PROC com.lpss的活动com.lpss / .MyList:PID = 248 UID = 10030导报= {3003,1015}
06-07 00:25:30.244:ERROR / ActivityThread(58):未能找到android.server.checkin供应商信息
06-07 00:25:30.244:警告/检入(58):无法登录事件SYSTEM_SERVICE_LOOPING:java.lang.IllegalArgumentException异常:未知的URL内容://android.server.checkin/events
06-07 00:25:30.743:DEBUG / DDM堆(248):GOT功能列表请求
06-07 00:25:30.912:信息/ usagestats机(58):com.lpss意外的简历,同时已在com.lpss恢复
06-07 00:25:31.443:WARN / InputManagerService(58):GOT的RemoteException发送SETACTIVE(假)的通知为PID 234 UID 10030
06-07 00:25:31.603:信息/ ActivityManager(58):显示活动com.lpss / .MyList:1487毫秒(共1487毫秒)
06-07 00:25:31.713:WARN / NotificationService(58):对象死亡试图隐藏在包com.lpss通知android.app.ITransientNotification$Stub$Proxy@44de2548
06-07 00:25:31.713:WARN / ActivityManager(58):setProcessForeground呼吁未知PID:234
06-07 00:25:31.723:WARN / NotificationService(58):对象死亡试图显示包com.lpss通知android.app.ITransientNotification$Stub$Proxy@44de2c70
06-07 00:25:31.723:WARN / ActivityManager(58):setProcessForeground呼吁未知PID:234
06-07 00:25:31.723:WARN / NotificationService(58):对象死亡试图显示包com.lpss通知android.app.ITransientNotification$Stub$Proxy@44dd78a0
06-07 00:25:31.723:WARN / ActivityManager(58):setProcessForeground呼吁未知PID:234
06-07 00:25:31.723:WARN / NotificationService(58):对象死亡试图显示包com.lpss通知android.app.ITransientNotification$Stub$Proxy@44dd7fc8
06-07 00:25:31.723:WARN / ActivityManager(58):setProcessForeground呼吁未知PID:234
06-07 00:41:25.262:DEBUG / dalvikvm(58):GC释放19490对象/在209ms 957584字节

code检索图像

  b3.setOnClickListener(新OnClickListener(){                    公共无效的onClick(查看为arg0){                        尝试{
                            data.put(名,名);
                        }赶上(JSONException E){
                            // TODO自动生成catch块
                            e.printStackTrace();
                        }
                        字符串URL =htt​​p://10.0.2.2:8080/finder/place/ph?id=+ ID;
                        // JSON = HTTPPoster.doPost(URL,数据);
                        downloadImage(URL);
// Toast.makeText(这一点,json.toString(),Toast.LENGTH_SHORT).show();                    }
             });
             b4.setOnClickListener(新OnClickListener(){                    公共无效的onClick(查看为arg0){
                        t.se​​tText();
                        尝试{
                            data.put(名,名);
                        }赶上(JSONException E){
                            // TODO自动生成catch块
                            e.printStackTrace();
                        }
                        字符串URL =htt​​p://10.0.2.2:8080/finder/place/map?id=+ ID;
                        // JSON = HTTPPoster.doPost(URL,数据);
                        downloadImage(URL);
// Toast.makeText(这一点,json.toString(),Toast.LENGTH_SHORT).show();                    }
             });


解决方案

我的感觉是一个空指针错误。我会做的是环绕在整个方法的大尝试{}赶上(的Throwable T){} 块。

然后打印出堆栈跟踪和错误信息。事情是,你将不得不调试步骤的code,因为的System.out.println()不会在Android中做多。

(旁白:在Android中,我已经习惯了使用Log.v()或一次性调试code其中在其他平台上你会使用系统输出类似的东西)

或者你可以尝试android的日志框架的东西,如:

 捕获(的Throwable T)
{
    Log.v(Ric_TEST,t.getMessage();
}

您可以搜索您的logcat下载单词Ric_TEST。或者(假设你使用Eclipse),您可以通过键入的标记文字过滤logcat的输出。

另一个类似的方法是使用亚洲开发银行(亚行logcat -v时间)连接到您的设备,你可以看日志,看看堆栈跟踪,当应用程序崩溃。

请注意:一旦你找到了故障,取出try..catch块我已经提到 - 我只提倡它作为查找故障的工具

In the user interface of my application there are four buttons out of which three buttons(when clicked) retrieve an image from mysql db and display it in the android phone. the fourth button's job is to retrieve text from the db.

if any of the buttons that retrieve image are clicked first the image is displayed.and then if the button that retrieve's an image is clicked the text is displayed.

But when the button that retrieves the text is clicked first even before any button that displays the image is clicked the application force closes!!

the code for the onClick activity of the button that retrieves text is as follows

b.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {        
            Toast.LENGTH_SHORT).show();

            try {
                data.put("id",id);
            } catch (JSONException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } 
            String url="http://10.0.2.2:8080/finder/review/listA";

            try { 
                img.setImageBitmap(null);
                json =HTTPPoster.doPost(url, data);

                Toast.makeText(getBaseContext(),json.toString(),     Toast.LENGTH_SHORT).show();
                 titles=json.getJSONObject("titles");
                 Toast.makeText(getBaseContext(),    titles.toString(), Toast.LENGTH_SHORT).show();
                 msgs=json.getJSONObject("msgs");
                 names=json.getJSONObject("names");

            String one=titles.getString("0");
            Toast.makeText(getBaseContext(), one.toString(),Toast.LENGTH_LONG).show();
            t.setText("\nReviews \n");
            String content="";


            for(int i=0;i<3;i++)
            {
                String msg=msgs.getString(i+"");
                 name=names.getString(i+"");
                String title=titles.getString(i+"");

                content=t.getText()+("\n Name"+name+"\n Title: "+title+"\n Msg:"+msg);
                t.setText(content);

                Toast.makeText(getBaseContext(),"Name:"+name+"\n Title: "+title+"\n Message"+msg,Toast.LENGTH_SHORT).show(); 
            }

                // t.setText()
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

logcat

06-07 00:25:29.923: INFO/Process(234): Sending signal. PID: 234 SIG: 9
06-07 00:25:30.042: INFO/WindowManager(58): WIN DEATH: Window{44e66ef0 com.lpss/com.lpss.MyList paused=false}
06-07 00:25:30.053: INFO/WindowManager(58): WIN DEATH: Window{44d72da0 com.lpss/com.lpss.lpss paused=false}
06-07 00:25:30.053: INFO/ActivityManager(58): Process com.lpss (pid 234) has died.
06-07 00:25:30.062: WARN/GpsLocationProvider(58): Unneeded remove listener for uid 1000
06-07 00:25:30.062: DEBUG/GpsLocationProvider(58): stopNavigating
06-07 00:25:30.062: INFO/WindowManager(58): WIN DEATH: Window{44dedb10 Toast paused=false}
06-07 00:25:30.091: WARN/ActivityManager(58): Scheduling restart of crashed service com.lpss/.NotifyService in 5000ms
06-07 00:25:30.103: INFO/WindowManager(58): WIN DEATH: Window{44dd9490 com.lpss/com.lpss.Contents paused=false}
06-07 00:25:30.244: INFO/ActivityManager(58): Start proc com.lpss for activity com.lpss/.MyList: pid=248 uid=10030 gids={3003, 1015}
06-07 00:25:30.244: ERROR/ActivityThread(58): Failed to find provider info for android.server.checkin
06-07 00:25:30.244: WARN/Checkin(58): Can't log event SYSTEM_SERVICE_LOOPING: java.lang.IllegalArgumentException: Unknown URL content://android.server.checkin/events
06-07 00:25:30.743: DEBUG/ddm-heap(248): Got feature list request
06-07 00:25:30.912: INFO/UsageStats(58): Unexpected resume of com.lpss while already resumed in com.lpss
06-07 00:25:31.443: WARN/InputManagerService(58): Got RemoteException sending setActive(false) notification to pid 234 uid 10030
06-07 00:25:31.603: INFO/ActivityManager(58): Displayed activity com.lpss/.MyList: 1487 ms (total 1487 ms)
06-07 00:25:31.713: WARN/NotificationService(58): Object died trying to hide notification android.app.ITransientNotification$Stub$Proxy@44de2548 in package com.lpss
06-07 00:25:31.713: WARN/ActivityManager(58): setProcessForeground called on unknown pid: 234
06-07 00:25:31.723: WARN/NotificationService(58): Object died trying to show notification android.app.ITransientNotification$Stub$Proxy@44de2c70 in package com.lpss
06-07 00:25:31.723: WARN/ActivityManager(58): setProcessForeground called on unknown pid: 234
06-07 00:25:31.723: WARN/NotificationService(58): Object died trying to show notification android.app.ITransientNotification$Stub$Proxy@44dd78a0 in package com.lpss
06-07 00:25:31.723: WARN/ActivityManager(58): setProcessForeground called on unknown pid: 234
06-07 00:25:31.723: WARN/NotificationService(58): Object died trying to show notification android.app.ITransientNotification$Stub$Proxy@44dd7fc8 in package com.lpss
06-07 00:25:31.723: WARN/ActivityManager(58): setProcessForeground called on unknown pid: 234
06-07 00:41:25.262: DEBUG/dalvikvm(58): GC freed 19490 objects / 957584 bytes in 209ms

code that retrieves image

b3.setOnClickListener(new OnClickListener() {

                    public void onClick(View arg0) {

                        try {
                            data.put("name",name);
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        String url="http://10.0.2.2:8080/finder/place/ph?id="+id;
                        //      json =HTTPPoster.doPost(url, data);
                        downloadImage(url);
//      Toast.makeText(this,json.toString() , Toast.LENGTH_SHORT).show();

                    }
             });
             b4.setOnClickListener(new OnClickListener() {

                    public void onClick(View arg0) {
                        t.setText("");
                        try {
                            data.put("name",name);
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        String url="http://10.0.2.2:8080/finder/place/map?id="+id;
                        //      json =HTTPPoster.doPost(url, data);
                        downloadImage(url);
//      Toast.makeText(this,json.toString() , Toast.LENGTH_SHORT).show();

                    }
             });

解决方案

My feeling is a null-pointer error. What I would do is surround the whole method in a big try{} catch (Throwable t) {} block.

Then print out the stack trace, and the error message. Thing is that you would have to debug step the code, since System.out.println() doesn't do much in android.

(aside: in android, I've gotten used to using Log.v() or something similar for throwaway debug code where on other platforms you would use sysout)

Or you could try the android logging framework with something like:

catch (Throwable t)
{
    Log.v("Ric_TEST", t.getMessage();
}

You can then search your logcat download for the word "Ric_TEST". Alternatively (assuming you use Eclipse) you can filter the logcat output by typing the tag text in.

Another similar way is to connect to your device using the adb (adb logcat -v time) and you can watch the log, and see the stack trace when the app crashes.

Pls note: once you find the fault, remove the try..catch block I have mentioned - I'm only advocating it as a tool to find the fault.

这篇关于如何prevent我从强制关闭Android应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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