如何防止我的Android应用程序强行关闭?

时间:2011-06-06 19:02:04

标签: android

在我的应用程序的用户界面中有四个按钮,其中三个按钮(单击时)从mysql数据库中检索图像并将其显示在Android手机中。第四个按钮的工作是从数据库中检索文本。

如果点击任何检索图像的按钮,则首先显示图像。然后,如果单击检索图像的按钮,则显示文本。

但是,当点击显示图像的任何按钮之前,首先点击检索文本的按钮时,应用程序强制关闭!!

检索文本的按钮的onClick活动的代码如下

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

检索图片的代码

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();

                    }
             });

1 个答案:

答案 0 :(得分:0)

我的感觉是空指针错误。我要做的是将整个方法包含在一个大的try{} catch (Throwable t) {}块中。

然后打印出堆栈跟踪和错误消息。事情是你必须调试步骤代码,因为System.out.println()在android中没有做太多。

(除了:在android中,我已经习惯了使用Log.v()或类似的东西进行一次性调试代码,在其他平台上你将使用sysout)

或者您可以使用以下内容尝试Android日志框架:

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

然后,您可以在logcat下载中搜索单词“Ric_TEST”。或者(假设您使用Eclipse),您可以通过在。

中键入标记文本来过滤logcat输出

另一种类似的方法是使用adb(adb logcat -v time)连接到您的设备,您可以观看日志,并在应用崩溃时查看堆栈跟踪。

请注意:一旦发现错误,请删除我提到的try..catch块 - 我只是提倡它作为查找错误的工具。