我在从Activity 1启动intent后尝试从Activity 2中的DB显示一些数据,如:
{ 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时,以正常方式运行显示空白屏幕。你能告诉我有什么问题吗?
<?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>
来自DataSource的方法
public Cursor 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
public void 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()时,我的日志工作正常。来自MainActivity,但当我尝试从ListDataActivity应用程序崩溃时调用它。 有人可以解释一下发生了什么吗? 检查过DB一切正常。将ListView放入MainActivity时,一切正常。 为何不与其他活动合作?
答案 0 :(得分:0)
您忘记在Oncreate()方法中使用datasource.open()
或datasource = new YOURCLASS(this)
答案 1 :(得分:0)
很抱歉对stackoverflow社区感到烦恼,但似乎练习Android的长时间延迟对我起了作用。简单地忘了声明DataSource类的变量。人们对你所做的事情,对自己和对周围的事情都有所了解!