我开始使用片段,因为我习惯使用活动,我只想把事情带到另一个层面。到目前为止我学到的是在片段之间传递数据,现在我想尝试做一些包含数据库的事情。
这只是一个示例应用程序,它将向我展示如何处理这类内容。
所以基本上我有一个用于添加联系人的片段和第二个片段,用于在列表视图中显示联系人的名字。我在数据库中添加联系人,该部分工作正常。但是当我尝试在包含listview的片段内的数据库中显示数据时,我的应用程序会崩溃。
以下是代码:
public class MainActivity extends AppCompatActivity implements Contacts_Add.sendData {
DatabaseHandler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new DatabaseHandler(this, null, null, 1);
//____________________________PRINT DATABASE_____________________
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, new Contacts_All(), "allFrag");
fragmentTransaction.commit();
getSupportFragmentManager().executePendingTransactions();
Contacts_All fragAll = (Contacts_All) getSupportFragmentManager().findFragmentByTag("allFrag");
fragAll.printDatabase();
//______________________________________________________________________
//etValues method is outside of onCreate I just didnt put the whole code.
@Override
public void etValues(String fname, String lname, String phone, String email) {
ContactsObjects contact = new ContactsObjects(fname, lname, phone, email);
handler.insertContact(contact);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, new Contacts_All(), "allFrag");
fragmentTransaction.commit();
getSupportFragmentManager().executePendingTransactions();
Contacts_All fragAll = (Contacts_All) getSupportFragmentManager().findFragmentByTag("allFrag");
fragAll.printDatabase();
}
所有联系人片段:
public class Contacts_All extends ListFragment {
DatabaseHandler handler;
public Contacts_All() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.contacts_all, container, false);
return view;
}
public void printDatabase() {
List<ContactsObjects> contactsList = handler.getAllContacts();
ArrayList<ContactsObjects> newContactsList = new ArrayList<ContactsObjects>();
for (ContactsObjects cn : contactsList) {
newContactsList.add(cn);
}
ListAdapter la = new ArrayAdapter<ContactsObjects>(getActivity(), android.R.layout.simple_list_item_1, newContactsList);
setListAdapter(la);
}
}
数据库处理器:
public List<ContactsObjects> getAllContacts() {
List<ContactsObjects> contactList = new ArrayList<ContactsObjects>();
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
ContactsObjects contact = new ContactsObjects();
contact.set_id(Integer.parseInt(cursor.getString(cursor.getColumnIndex("_id"))));
contact.setFirstName(cursor.getString(cursor.getColumnIndex("firstname")));
contact.setLastName(cursor.getString(cursor.getColumnIndex("lastname")));
contact.setPhoneNumber(cursor.getString(cursor.getColumnIndex("phonenumber")));
contact.setEmail(cursor.getString(cursor.getColumnIndex("email")));
contactList.add(contact);
} while (cursor.moveToNext());
}
return contactList;
}
logcat错误: 第一个错误出现在这一行:
List<ContactsObjects> contactsList = handler.getAllContacts();
第二个错误出现在这一行:
fragAll.printDatabase(); //in onCreate
答案 0 :(得分:0)
在listfragment(Contacts_All类)的printDatabase()方法中,您的处理程序永远不会被初始化。您缺少这一行:
handler = new DatabaseHandler(this, null, null, 1);