从活动 1 开始意图后,我试图在活动 2 中显示来自数据库的一些数据,例如:
{ case R.id.buttonRead:
intent = new Intent(this, ListDataActivity.class);
startActivity(intent);
break;
}
ListDataActivity 具有以下编码:
{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_data);
ListView lv = (ListView) findViewById(R.id.view_all_data);
String[] from = new String[] { DBHelper.COLUMN_NAME,
DBHelper.COLUMN_LAST_NAME };
int[] to = new int[] { R.id.textView_name_reflect,
R.id.textView_last_name_reflect };
// create Adapter
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.item, datasource.getAllEmployees(), from, to,
FLAG_REGISTER_CONTENT_OBSERVER);
// make adapter available for list
lv.setAdapter(adapter);
}
Manifest 中的 ListDataActivity 没有 Intent Filter。当我单击按钮并启动 ListDataActivity 应用程序时。因 NullPointerException 而崩溃。最有趣的是,当我从 onCreate ListDataActivity 中删除 ListView 时,ListDataActivity 以正常方式运行,显示空白屏幕。你能告诉我有什么问题吗?
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_all_data"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
来自数据源的方法
公共游标 getAllEmployees() {
Log.d(LOG_TAG, "--- Rows in mytable: ---");
// make query of all data from the table and receive instance of Cursor
Cursor c = db.query(DBHelper.TABLE_STAFF, null, null, null, null, null,
null);
return c;
}
试过以下
case R.id.buttonRead:
ArrayList<String> value = datasource.getAllEmployees();
intent.putStringArrayListExtra("123", (ArrayList<String>)value);
startActivity(intent);
break;
}
改变了
public ArrayList<String> getAllEmployees() {
ArrayList<String> empList = new ArrayList<String>();
empList.add("Start");
return empList;
}
以及 ListDataActivity
Intent intent = getIntent();
ArrayList<String> value = intent.getStringArrayListExtra("123");
ListView lv = (ListView) findViewById(R.id.view_all_data);
// create Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, value);
// make adapter available for list
lv.setAdapter(adapter);
它工作得很好
已更改我的 getAllEmployees 以查看 DB 是否一切正确
公共无效getAllEmployees(){
open();
Log.d(LOG_TAG, "--- Rows in mytable: ---");
// make query of all data from the table and receive instance of Cursor
Cursor c = db.query(DBHelper.TABLE_STAFF, null, null, null, null, null,
null);
// put cursor into position of the first row of required data, if no
// rows false will return
if (c.moveToFirst()) {
// get columns numbers
int idColIndex = c.getColumnIndex(DBHelper.COLUMN_ID);
int nameColIndex = c.getColumnIndex(DBHelper.COLUMN_NAME);
int lastnameColIndex = c.getColumnIndex(DBHelper.COLUMN_LAST_NAME);
int positionColIndex = c.getColumnIndex(DBHelper.COLUMN_POSITION);
int departmentColIndex = c
.getColumnIndex(DBHelper.COLUMN_DEPARTMENT);
int inttelColIndex = c.getColumnIndex(DBHelper.COLUMN_INT_TEL);
int mobileColIndex = c.getColumnIndex(DBHelper.COLUMN_MOB_TEL);
int homeColIndex = c.getColumnIndex(DBHelper.COLUMN_HOME_TEL);
int officemailColIndex = c
.getColumnIndex(DBHelper.COLUMN_OFFICE_E_MAIL);
int personalmailColIndex = c
.getColumnIndex(DBHelper.COLUMN_PERSONAL_E_MAIL);
do {
// getting values by column numbers and put them into log
Log.d(LOG_TAG,
DBHelper.COLUMN_ID + c.getInt(idColIndex) + "\n"
+ DBHelper.COLUMN_NAME
+ c.getString(nameColIndex)
+ DBHelper.COLUMN_LAST_NAME
+ c.getString(lastnameColIndex)
+ DBHelper.COLUMN_POSITION
+ c.getString(positionColIndex)
+ DBHelper.COLUMN_DEPARTMENT
+ c.getString(departmentColIndex)
+ DBHelper.COLUMN_INT_TEL
+ c.getString(inttelColIndex)
+ DBHelper.COLUMN_MOB_TEL
+ c.getString(mobileColIndex)
+ DBHelper.COLUMN_HOME_TEL
+ c.getString(homeColIndex)
+ DBHelper.COLUMN_OFFICE_E_MAIL
+ c.getString(officemailColIndex)
+ DBHelper.COLUMN_PERSONAL_E_MAIL
+ c.getString(personalmailColIndex));
// move to next row, if no next then end loop
} while (c.moveToNext());
} else
Log.d(LOG_TAG, "0 rows");
c.close();
}
当我调用 datasource.getAllEmployees(); 时,非常适用于 Logs;从 MainActivity 但是当我尝试从 ListDataActivity 应用程序崩溃时调用它。 有人可以解释一下发生了什么吗? 已检查数据库一切正常。将 ListView 放入 MainActivity 时,一切正常。为什么它不适用于其他活动?