Android - 强制关闭,如果我点击ListView上的项目

问题描述:

我需要你的帮助来修复我的代码。Android - 强制关闭,如果我点击ListView上的项目

我的意图是,如果我点击ListView上的项目,它会带我到intent action_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()); 
    }  
} 

}

+3

请发布您的logcat异常详细信息。 – zmbq

内部项目点击 你有很多方面,它可能会导致异常, 异常会导致您的应用程序强制关闭 你的代码是不是有例外处理

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

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空,在这种情况下,您的应用程序将强制关闭
  2. 如果dbs.rawQuery返回null,你的应用程序将强制关闭
  3. 如果结果为空,那么你可能调用moveToFirst,getDouble和getColumnIndex空对象,在这种情况下您的应用程序将强制关闭

您的信息 SimpleCursorAdapter构造函数API级别被废弃11 此选项气馁,因为它会导致应用程序的UI线程上执行光标查询,从而可能会导致反应不佳,甚至应用程序无响应错误。作为替代,使用带有CursorLoader的LoaderManager。