在我的应用程序中,我使用导航抽屉和sqlite数据库来显示片段中sqlite的数据。当我点击其中一个导航元素即搜索医院时,它应该从数据库加载数据但是应用程序闪烁“不幸的是应用程序已关闭“。
以下是片段的代码
public class SHospitals extends Fragment {
private hospitalDBadapter dbhelper;
private SimpleCursorAdapter dataAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.shospitals, container, false);
dbhelper = new hospitalDBadapter(getActivity());
dbhelper.open();
dbhelper.deleteallhospitals();
dbhelper.insertsomehospitals();
Cursor cursor = dbhelper.fetchallhospitals();
String[] columns = new String[] { hospitalDBadapter.KEY_HOSP,
hospitalDBadapter.KEY_ADD, hospitalDBadapter.KEY_CONT };
int[] to = new int[] { R.id.hospital, R.id.address, R.id.contact, };
dataAdapter = new SimpleCursorAdapter(getActivity(),
R.layout.hospitalinfo, cursor, columns, to, 0);
final ListView listview = (ListView) getView().findViewById(R.id.lv1);
listview.setAdapter(dataAdapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Cursor cursor = (Cursor) listview.getItemAtPosition(position);
String hospital_name = cursor.getString(cursor
.getColumnIndexOrThrow("hosp_name"));
Toast.makeText(getActivity(), hospital_name, Toast.LENGTH_SHORT)
.show();
}
});
EditText myfilter = (EditText) getView().findViewById(R.id.filter);
myfilter.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
dataAdapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
// TODO Auto-generated method stub
return dbhelper.fetchhospitalsbyname(constraint.toString());
}
});
return rootView;
}}
这是我的databasehelper类,
public class hospitalDBadapter {
public static final String KEY_ID = "_id";
public static final String KEY_HOSP = "hosp_name";
public static final String KEY_ADD = "hosp_Add";
public static final String KEY_CONT = "hosp_cont";
private static final String DATABASE_NAME = "hospitals_dbs";
private static final String SQLITE_TABLE = "hospitals_tables";
private static final int DATABASE_VERSION = 1;
public static final String TAG = "hospitalDBadapter";
public final Context ourcontext;
private DatabaseHelper mDBHelper;
private SQLiteDatabase mDB;
public static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + SQLITE_TABLE + " (" + KEY_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_HOSP
+ " TEXT NOT NULL, " + KEY_ADD + " TEXT NOT NULL, "
+ KEY_CONT + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);
onCreate(db);
}
}
public hospitalDBadapter(Context c) {
ourcontext = c;
}
public hospitalDBadapter open() throws SQLException {
mDBHelper = new DatabaseHelper(ourcontext);
mDB = mDBHelper.getWritableDatabase();
return this;
}
public void close() {
if (mDBHelper != null) {
mDBHelper.close();
}
}
public boolean deleteallhospitals() {
// TODO Auto-generated method stub
int donedelete = 0;
donedelete = mDB.delete(SQLITE_TABLE, null, null);
Log.w(TAG, Integer.toString(donedelete));
return donedelete > 0;
}
public void insertsomehospitals() {
// TODO Auto-generated method stub
createhospital("Lilavati Hospital", "Peddar Road", "6541324532");
createhospital("Sion Hospital", "A B Road", "6451235478");
createhospital("Dayaben Hospital", "S P Road", "4132456787");
createhospital("ABC Hospital", "P D Road", "8451295421");
createhospital("DEF Hospital", "D S Road", "9832151231");
}
private long createhospital(String hosp_name, String hosp_Add,
String hosp_cont) {
// TODO Auto-generated method stub
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_HOSP, hosp_name);
initialValues.put(KEY_ADD, hosp_Add);
initialValues.put(KEY_CONT, hosp_cont);
return mDB.insert(SQLITE_TABLE, null, initialValues);
}
public Cursor fetchallhospitals() {
// TODO Auto-generated method stub
Cursor mCursor = mDB.query(SQLITE_TABLE, new String[] { KEY_ID,
KEY_HOSP, KEY_ADD, KEY_CONT }, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor fetchhospitalsbyname(String string) throws SQLException {
// TODO Auto-generated method stub
Log.w(TAG, string);
Cursor mCursor = null;
if (string == null || string.length() == 0) {
mCursor = mDB.query(SQLITE_TABLE, new String[] { KEY_ID, KEY_HOSP,
KEY_ADD, KEY_CONT }, null, null, null, null, null);
} else {
mCursor = mDB.query(true, SQLITE_TABLE, new String[] { KEY_ID,
KEY_HOSP, KEY_ADD, KEY_CONT }, KEY_HOSP + " like '%"
+ string + "%'", null, null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}
plz.help me out。
这是我的LogCat
02-19 08:54:15.059: W/dalvikvm(2013): threadid=1: thread exiting with uncaught exception (group=0xb1aefba8)
02-19 08:54:15.069: E/AndroidRuntime(2013): FATAL EXCEPTION: main
02-19 08:54:15.069: E/AndroidRuntime(2013): Process: com.beproject.pocketdoctor, PID: 2013
02-19 08:54:15.069: E/AndroidRuntime(2013): java.lang.NullPointerException
02-19 08:54:15.069: E/AndroidRuntime(2013): at com.beproject.pocketdoctor.SHospitals.onCreateView(SHospitals.java:45)
02-19 08:54:15.069: E/AndroidRuntime(2013): at android.app.Fragment.performCreateView(Fragment.java:1700)
02-19 08:54:15.069: E/AndroidRuntime(2013): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
02-19 08:54:15.069: E/AndroidRuntime(2013): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
02-19 08:54:15.069: E/AndroidRuntime(2013): at android.app.BackStackRecord.run(BackStackRecord.java:684)
02-19 08:54:15.069: E/AndroidRuntime(2013): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
02-19 08:54:15.069: E/AndroidRuntime(2013): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
02-19 08:54:15.069: E/AndroidRuntime(2013): at android.app.BackStackRecord.run(BackStackRecord.java:684)
02-19 08:54:15.069: E/AndroidRuntime(2013): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
02-19 08:54:15.069: E/AndroidRuntime(2013): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-19 08:54:15.069: E/AndroidRuntime(2013): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-19 08:54:15.069: E/AndroidRuntime(2013): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
首先,这行看起来很奇怪,逗号是数组中的最后一个条目:
int[] to = new int[] { R.id.hospital, R.id.address, R.id.contact, };
其次,我猜这不起作用:
final ListView listview = (ListView) getView().findViewById(R.id.lv1);
应该是
final ListView listview = (ListView) rootView.findViewById(R.id.lv1);