警报对话框不适用于列表中的所有项目

时间:2012-04-28 16:34:07

标签: android listview alertdialog

我有一个列表视图,我从数据库中获取的地方,我在项目上做了一个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 个答案:

答案 0 :(得分:0)

前8次你可能意味着屏幕上显示的项目而不需要滚动,对吗?

您需要发布一些代码才能让我们为您提供帮助。

答案 1 :(得分:0)

这是在onItemLongClick()中设置一些断点并使用调试器的绝佳时机。它将帮助您找到空值或至少找到崩溃的行。

我愿意猜测对话框打开的次数是无关紧要的,我假设你的第8个条目有一个哑弹。如果你先点击第8个条目它还会崩溃吗?如果是,则检查数据库,第8行hotelName的行是否所有您请求的字段,或者一个(或多个)列的 null 值?

<强>加成

当列表滚动并使用此行时:

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

该位置超出界限,返回 null 导致您的NullPointerException,因为父项的唯一子项是可见的,而是尝试更快,更简单的方法:

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