2012-04-28 111 views
0

我的,我從數據庫中獲取的地點列表視圖的所有項目的工作,我對項目作出了OnItemLongClickListener,長後單擊警報對話框一些信息被激活,在消息正文中,我從數據庫中獲取有關被點擊的項目。該對話框有3個按鈕(呼叫,映射和查看照片),它們工作正常。該對話框適用於前8個項目,然後對話框不會打開,並且應用程序組合關閉其餘項目,我不知道爲什麼!警告對話框不會爲列表

你能幫我嗎?

下面是其中顯示所有酒店的列表的代碼:

List<Hotel> values = datasource.getAllHotels(); 
      if (values.isEmpty()) 
      { 
       Toast.makeText(this, "No results found!", Toast.LENGTH_LONG).show(); 
       Intent i1 = new Intent(hotelSearchList.this, hotelSearch.class); 
       startActivity(i1); 
      } 
      ArrayAdapter<Hotel> adapter = new ArrayAdapter<Hotel>(this, 
        android.R.layout.simple_list_item_1, values); 
      setListAdapter(adapter); 
      ListView lv = getListView(); 
      lv.setOnItemLongClickListener(this); 

這是爲onItemLongClickListener代碼:

public boolean onItemLongClick(final AdapterView<?> parent, final View view, int position, 
     long id) { 
    // TODO Auto-generated method stub 
    String hotelName = (String) ((TextView)parent.getChildAt(position)).getText(); 
    final List<Hotel> location = datasource.getHotelLocation(hotelName); 
    String loc = "Location: " + TextUtils.join(", ", location); 
    List<Hotel> address = datasource.getHotelAddress(hotelName); 
    String add = "Address: " + TextUtils.join(", ", address); 
    List<Hotel> rating = datasource.getHotelRating(hotelName); 
    String rat = "Rating: " + TextUtils.join(", ", rating); 
    List<Hotel> phone = datasource.getHotelPhone(hotelName); 
    final String phoneN = "Phone: " + TextUtils.join(", ", phone); 
    List<Hotel> latitude = datasource.getHotelLat(hotelName); 
    final String lat = latitude.get(0).toString(); 
    List<Hotel> longitude = datasource.getHotelLon(hotelName); 
    final String lon = longitude.get(0).toString(); 
    List<Hotel> photo = datasource.getHotelPhoto(hotelName); 
    final String pho = photo.get(0).toString(); 
    //Toast.makeText(hotelSearchList.this, "The hotel clicked is " + hotelName, Toast.LENGTH_LONG).show(); 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle("Information About " + hotelName); 
    builder.setMessage(loc + "\n" + add + "\n" + rat + "\n" + phoneN + "\n"); 

    builder.setPositiveButton("Call", new DialogInterface.OnClickListener() { 

     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 
      Intent i = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneN)); 
      startActivity(i); 
     } 
    }); 
    builder.setNegativeButton("View Photo", new DialogInterface.OnClickListener() { 

     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 
      Intent i = new Intent(hotelSearchList.this, imageV.class); 
      i.putExtra("photo", pho); 
      startActivity(i); 
     } 
    }); 
    builder.setNeutralButton("Map it", new DialogInterface.OnClickListener() { 

     public void onClick(DialogInterface arg0, int arg1) { 
      // TODO Auto-generated method stub 
      Intent i = new Intent(hotelSearchList.this, mapActivity.class); 
      i.putExtra("latitude", lat); 
      i.putExtra("longitude", lon); 
      startActivity(i); 
     } 
    }); 

    builder.setCancelable(true); 
    builder.show(); 
    return false; 
} 

這裏是日誌貓:

04-28 19:47:15.159: E/AndroidRuntime(384): FATAL EXCEPTION: main 
04-28 19:47:15.159: E/AndroidRuntime(384): java.lang.NullPointerException 
04-28 19:47:15.159: E/AndroidRuntime(384): at egypt.interfaceAct.touristicASearchList.onItemLongClick(touristicASearchList.java:134) 
04-28 19:47:15.159: E/AndroidRuntime(384): at android.widget.AbsListView.performLongPress(AbsListView.java:1753) 
04-28 19:47:15.159: E/AndroidRuntime(384): at android.widget.AbsListView.access$600(AbsListView.java:72) 
04-28 19:47:15.159: E/AndroidRuntime(384): at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:1711) 
04-28 19:47:15.159: E/AndroidRuntime(384): at android.os.Handler.handleCallback(Handler.java:587) 
04-28 19:47:15.159: E/AndroidRuntime(384): at android.os.Handler.dispatchMessage(Handler.java:92) 
04-28 19:47:15.159: E/AndroidRuntime(384): at android.os.Looper.loop(Looper.java:123) 
04-28 19:47:15.159: E/AndroidRuntime(384): at android.app.ActivityThread.main(ActivityThread.java:4627) 
04-28 19:47:15.159: E/AndroidRuntime(384): at java.lang.reflect.Method.invokeNative(Native Method) 
04-28 19:47:15.159: E/AndroidRuntime(384): at java.lang.reflect.Method.invoke(Method.java:521) 
04-28 19:47:15.159: E/AndroidRuntime(384): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
04-28 19:47:15.159: E/AndroidRuntime(384): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
04-28 19:47:15.159: E/AndroidRuntime(384): at dalvik.system.NativeStart.main(Native Method) 
+2

請發佈相關的代碼和你的logcat錯誤,以便我們可以看到發生了什麼。 – Sam 2012-04-28 16:38:07

+0

代碼被添加 – MahaK 2012-04-28 16:46:51

+0

隨着日誌貓 – MahaK 2012-04-28 16:49:23

回答

0

這是一個很好的時間來設置一些斷點在onItemLongClick()和使用調試器。它將幫助您找到空值或至少找到崩潰的行。

我願意猜測打開的對話框中的次數是不相關的,我認爲有一個啞彈的第八入口。如果你先點擊第8項,它還會崩潰嗎?如果是這樣,你檢查你的數據庫,第8 hotelName行有所有您所請求的字段或一個(或多個)列有空值值?

加成

當列表滾動了,你用這個行:

String hotelName = (String) ((TextView)parent.getChildAt(position)).getText(); 

的位置超出返回導致您的NullPointerException異常,因爲父母唯一的孩子界可見的,而不是嘗試更快,更簡單的方法:

String hotelname = ((TextView) view).getText().toString(); 
+0

問題出在這一行: String hotelName =(String)((TextView)parent.getChildAt(position))。getText(); 但我不確定爲什麼它適用於第一個項目,而不是這個,我無法弄清楚,而調試! 如果我先點擊第8項,它仍會崩潰。 – MahaK 2012-04-28 17:32:03

+0

非常好,通過設置該行上的斷點,您是否驗證過:'position'和'parent'的值在'position'處有一個孩子? – Sam 2012-04-28 17:42:19

+0

我不明白你的意思,當我調試並單擊列表中的第8個元素時,儘管我按下了「Resume」按鈕兩次,然後「ZygoteInit $ MethodAndArgsCaller.run()」,調試器保持在同一行上。在這種情況下打開「源未找到」 – MahaK 2012-04-28 18:03:27

0

通過前8次,你可能指的是屏幕上顯示的項目,而無需滾動,對不對?

你需要發佈一些代碼,以便我們來幫助你。

+0

是的,我認爲這與所顯示的項目有關!我現在會發布一些代碼 – MahaK 2012-04-28 16:44:09