2011-06-06 92 views
0

在我的應用程序的用戶界面中有四個按鈕,其中三個按鈕(當單擊時)從mysql db中檢索圖像並將其顯示在android手機中。第四個按鈕的工作是從數據庫中檢索文本。如何防止我的android應用程序強制關閉?

如果有任何檢索圖像的按鈕先被點擊,圖像將被顯示。然後如果點擊檢索圖像的按鈕,則會顯示文本。

但是,當點擊顯示圖像的任何按鈕之前首先單擊檢索文本的按鈕時,應用程序強制關閉!

檢索文本如下

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 [email protected] 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 [email protected] 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 [email protected] 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 [email protected] 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(); 

        } 
      }); 
+0

你可以請發佈stacktrace? – BigFwoosh 2011-06-06 19:04:28

+0

就像BigFwosh說的那樣,我們需要一個堆棧軌跡來了解從哪裏開始。這可能是因爲它無法從你的數據庫中讀取,可能是因爲任何原因你的方法中的任何對象都是空的。 – CaseyB 2011-06-06 19:15:03

+0

logcat中有很多條目..我怎麼知道要發佈哪一個? – Shalini 2011-06-06 19:29:21

回答

0

M表示的按鈕的onclick活性的代碼你的感覺是一個空指針錯誤。我要做的是將整個方法放在一個大的try{} catch (Throwable t) {}區塊中。

然後打印出堆棧跟蹤和錯誤消息。事情是,你將不得不調試步驟的代碼,因爲System.out.println()在android中沒有太大的作用。

(旁白:在Android中,我已經習慣了使用Log.v()或一次性調試代碼,其中在其他平臺上你可以使用系統輸出類似的東西)

或者你可以嘗試android的日誌框架與類似的東西:

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

然後,您可以搜索您的logcat下載單詞「Ric_TEST」。或者(假設您使用Eclipse),您可以通過輸入標記文本來過濾logcat輸出。

另一種類似的方法是使用adb(adb logcat -v time)連接到您的設備,並且您可以觀看日誌,並在應用程序崩潰時查看堆棧跟蹤。

請注意:一旦發現錯誤,請刪除我提到的try..catch塊 - 我只是將它作爲一種工具來發現錯誤。