1

我需要你的帮助来修复我的代码。

我的意图是,如果我单击 ListView 上的项目,它将带我进入意图 act​​ion_view。

问题是如果我单击 ListView 上的项目,我会强制关闭。

我认为问题出在 onItemClick 方法中,您能否提供更好的方法来完成它。

这是我的代码:

public class HotelList extends ListActivity{
hotelHelper dbHotelHelper;
protected Cursor cursor;
protected ListAdapter adapter;
ListView numberList;    

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.hotellist);

    numberList = (ListView)findViewById(android.R.id.list);
    numberList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            // TODO Auto-generated method stub
                    String selectedItem = (String) getListAdapter().getItem(position);
                    String query = "SELECT lat, long FROM hoteltbl WHERE name = '" + selectedItem + "'";
                    SQLiteDatabase dbs = dbHotelHelper.getReadableDatabase();
                    Cursor result = dbs.rawQuery(query, null);
                    result.moveToFirst();

                    double lat = result.getDouble(result.getColumnIndex("lat"));
                    double longi = result.getDouble(result.getColumnIndex("long"));

                    Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?f=d&saddr=&daddr="+lat+","+longi));
                    startActivity(intent);

                }                   
            });     


    dbHotelHelper = new hotelHelper(this);
    try{
        dbHotelHelper.createDataBase();         
    }
    catch (Exception ioe){
        Log.e("err","Unable to create database");
    }        
    view();   


    }



private void view() {
    // TODO Auto-generated method stub
    SQLiteDatabase db = dbHotelHelper.getReadableDatabase();
    try{
        cursor = db.rawQuery("SELECT * FROM hoteltbl", null);
        adapter = new SimpleCursorAdapter(
                this, 
                android.R.layout.simple_list_item_1, 
                cursor, 
                new String[]{"name"}, 
                new int[] {android.R.id.text1}
                );
        numberList.setAdapter(adapter);     
    }
    catch (Exception e){
        Log.e("error",e.toString());
    }       
}

}

4

1 回答 1

0

在项目内单击您有很多可能导致异常的方式,异常将导致您的应用程序强制关闭您的代码未处理异常

我列出了可能导致异常的原因,请检查以下内容

String selectedItem = (String) getListAdapter().getItem(position);

在这里你可能会遇到类型对话问题,你正在转换字符串不支持的东西

SQLiteDatabase dbs = dbHotelHelper.getReadableDatabase();
Cursor result = dbs.rawQuery(query, null);
result.moveToFirst();
double lat = result.getDouble(result.getColumnIndex("lat"));
double longi = result.getDouble(result.getColumnIndex("long"));
  1. 如果 dbHotelHelper.getReadableDatabase 返回 null 或 dbHotelHelper null,在这种情况下您的应用程序将强制关闭
  2. 如果 dbs.rawQuery 返回 null,您的应用程序将强制关闭
  3. 如果结果为 null,那么您可能会在 null 对象上调用 moveToFirst、getDouble 和 getColumnIndex,在这种情况下,您的应用程序也会强制关闭

供您参考,API 级别 11 中不推荐使用 SimpleCursorAdapter 构造函数。不鼓励使用此选项,因为它会导致在应用程序的 UI 线程上执行游标查询,从而可能导致响应速度不佳甚至应用程序无响应错误。作为替代方案,将 LoaderManager 与 CursorLoader 一起使用。

于 2017-01-18T11:50:19.887 回答