次のように、アクティビティ 1 からインテントを開始した後、アクティビティ 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);
}
マニフェストの ListDataActivity はインテント フィルターなしのままです。ボタンをクリックして ListDataActivity アプリを起動すると。NullPointerException でクラッシュします。最も興味深いのは、ListView を onCreate から削除すると、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>
メソッドから 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 に入れると、すべて正常に動作します。別のアクティビティで機能しないのはなぜですか?