[英]Populate ListView From SQLite database in onCreate() in Android
I created Activity 'A' and 'B'.I have inserted some data in Activity 'A' and i want to display all the data in listview in Activity 'B' on button click in Activity 'A'.But when i run the application it crashes and getting error and nullpointerException.Please help me.Thanks in advance. 我创建了活动'A'和'B'。我在活动'A'中插入了一些数据,我想在活动'A'中单击按钮时在活动'B'的列表视图中显示所有数据。但是当我运行应用程序崩溃,并得到错误和nullpointerException。请帮助我。谢谢。
Here is my Database method code. 这是我的数据库方法代码。
public Cursor employeeName(){
ArrayList<Employee> EmployeeNameList = new ArrayList<Employee>();
String selectQuery = "SELECT * FROM " + EMPLOYEE_DETAILS_TABLE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst())
{
do{
Employee empName = new Employee();
empName.setName(cursor.getString(1));
empName.setDepartment(cursor.getString(2));
empName.setDesignation(cursor.getString(3));
EmployeeNameList.add(empName);
} while(cursor.moveToNext());
}
return cursor;
}
public List<String> getAllEmployeeName() {
List<String> labels = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT * FROM " + EMPLOYEE_DETAILS_TABLE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(1));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return labels;
}
Here is my Activity code 这是我的活动代码
public class Employee_List extends ListActivity
{
DatabaseHelper databaseHelper;
Context context;
Employee emp;
protected SQLiteDatabase db;
protected Cursor cursor;
protected ListAdapter adapter;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.staff_employee_list);
populateListView();
}
private void populateListView()
{
DatabaseHelper databaseHelper = new DatabaseHelper(context);
db = (new DatabaseHelper(this)).getWritableDatabase();
Cursor cursor =(Cursor) databaseHelper.employeeName();
String[] fromFieldNames = new String[]
{databaseHelper.Staff_Employee_Name,databaseHelper.Department,databaseHelper.Designation};
int[] toViewIDs = new int[]
{R.id.textViewEmpName, R.id.textViewDepartmet, R.id.textViewDesignation};
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.staff_employee_list_item, cursor, fromFieldNames, toViewIDs);
ListView myList = (ListView) findViewById(android.R.id.list);
myList.setAdapter(myCursorAdapter);
}
}
Here is my Logcat 这是我的Logcat
06-27 15:25:51.498: E/AndroidRuntime(12033): FATAL EXCEPTION: main
06-27 15:25:51.498: E/AndroidRuntime(12033): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.employee_review/com.employee_review.Employee_List}: java.lang.NullPointerException
06-27 15:25:51.498: E/AndroidRuntime(12033): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-27 15:25:51.498: E/AndroidRuntime(12033): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-27 15:25:51.498: E/AndroidRuntime(12033): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-27 15:25:51.498: E/AndroidRuntime(12033): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-27 15:25:51.498: E/AndroidRuntime(12033): at android.os.Handler.dispatchMessage(Handler.java:99)
06-27 15:25:51.498: E/AndroidRuntime(12033): at android.os.Looper.loop(Looper.java:123)
06-27 15:25:51.498: E/AndroidRuntime(12033): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-27 15:25:51.498: E/AndroidRuntime(12033): at java.lang.reflect.Method.invokeNative(Native Method)
06-27 15:25:51.498: E/AndroidRuntime(12033): at java.lang.reflect.Method.invoke(Method.java:507)
06-27 15:25:51.498: E/AndroidRuntime(12033): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-27 15:25:51.498: E/AndroidRuntime(12033): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-27 15:25:51.498: E/AndroidRuntime(12033): at dalvik.system.NativeStart.main(Native Method)
06-27 15:25:51.498: E/AndroidRuntime(12033): Caused by: java.lang.NullPointerException
06-27 15:25:51.498: E/AndroidRuntime(12033): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
06-27 15:25:51.498: E/AndroidRuntime(12033): at com.employee_review.DatabaseHelper.employeeName(DatabaseHelper.java:129)
06-27 15:25:51.498: E/AndroidRuntime(12033): at com.employee_review.Employee_List.populateListView(Employee_List.java:35)
06-27 15:25:51.498: E/AndroidRuntime(12033): at com.employee_review.Employee_List.onCreate(Employee_List.java:28)
06-27 15:25:51.498: E/AndroidRuntime(12033): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-27 15:25:51.498: E/AndroidRuntime(12033): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-27 15:25:51.498: E/AndroidRuntime(12033): ... 11 more
The context
you pass to the database helper constructor here is not initialized: 您传递给数据库帮助程序构造函数的
context
未初始化:
DatabaseHelper databaseHelper = new DatabaseHelper(context);
Replace context
with eg this
. 用
this
替换context
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.