以下是我的片段的基本功能:1。在onCreate方法中从名为Backendless.com的后端服务(名称和电话号码)中检索数据。 2.将数据存储在本地SQlite数据库中,然后用于填充ListView。用户在片段中执行他们想要的操作(添加新联系人,删除旧联系人等)3。一旦用户退出片段,就将操作数据从SQlite数据库保存回后端服务。这发生在onPause方法中。一切正常,直到达到onPause()方法然后我得到java.lang.OutOfMemory
错误
这是我的SQlite数据管理器类,它在本地存储名称和电话号码。
public class DBAdapter {
private final static String TAG = "DBAdapter";
//Field Names
public final static String KEY_ROWID = "_id";
public final static String KEY_NAME = "name";
public final static String KEY_NUMBER = "number";
public final static String[] ALL_KEYS = {KEY_ROWID,KEY_NAME,KEY_NUMBER};
//Database Info
public static final String DATABASE_NAME = "ContactsDatabase";
public static final String DATABASE_TABLE = "Contacts";
public static final int DATABASE_VERSION = 1;
//SQL to create database
private static final String DATABASE_CREATE_SQL =
"CREATE TABLE " + DATABASE_TABLE
+ " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_NAME + " TEXT, "
+ KEY_NUMBER + " TEXT"
+");";
private final Context context;
private DatabaseHelper myDBHelper;
private SQLiteDatabase db;
public DBAdapter(Context c){
this.context = c;
myDBHelper = new DatabaseHelper(context);
}
public DBAdapter open(){
db = myDBHelper.getWritableDatabase();
return this;
}
public void close(){
db.close();
}
public long insertRow(String name,String number){
ContentValues values = new ContentValues();
values.put(KEY_NAME, name);
values.put(KEY_NUMBER, number);
return db.insert(DATABASE_TABLE,null,values);
}
public boolean deleteRow(long rowID){
String where = KEY_ROWID + "=" + rowID;
return db.delete(DATABASE_TABLE,where,null) !=0;
}
public void deleteAll(){
Cursor c = getAllRows();
long rowID = c.getColumnIndexOrThrow(KEY_ROWID);
if (c.moveToFirst()){
do{
deleteRow(c.getLong((int)rowID));
}while(c.moveToNext());
}
}
public Cursor getAllRows(){
String where = null;
Cursor c = db.query(DATABASE_TABLE, ALL_KEYS, null, null, null, null, KEY_NAME + " ASC");
if (c!=null){
c.moveToFirst();
}
return c;
}
// Get a Specific row by rowID
public Cursor getRow(long rowID){
String where = KEY_ROWID + "=" + rowID;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, null,null);
if (c!=null){
c.moveToFirst();
}
return c;
}
//Change an existing row with new Data
public boolean updateRow(long rowID, String name, String num){
String where = KEY_ROWID + "=" +rowID;
ContentValues values = new ContentValues();
values.put(KEY_NAME,name);
values.put(KEY_NUMBER,num);
return db.update(DATABASE_TABLE,values,where,null)!=0;
}
private static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE_SQL);
Log.v("Adapter", "UPDATED");
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
db.execSQL(DATABASE_CREATE_SQL);
Log.v("Adapter", "DOWNGRADED");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}}
这里是我的碎片代码。当我尝试在onPause()方法上从本地数据库中检索数据时,应用程序一直崩溃:
public class ContactsFragments extends Fragment {
public DBAdapter myDB;
EditText inputName,inputNumber;
private View view;
private Button saveButton, deleteAllButton;
public ListView myList;
public long rowClickedForEdit = -1;
public boolean shouldWeEdit = false;
private ArrayList<String> numbersForWeb = new ArrayList<>();
private ArrayList<String> namesForWeb = new ArrayList<>();
public ContactsFragments() {setRetainInstance(true);}
@Override
public void onCreate(Bundle savedInstanceState) {
final String userEmail = Backendless.UserService.CurrentUser().getEmail();
Toast.makeText(getActivity(), "UE: " + userEmail, Toast.LENGTH_LONG).show();
retrieveDataFromCloud(userEmail);
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_contacts_fragments, container, false);
inputName = (EditText)view.findViewById(R.id.inputName);
inputNumber = (EditText)view.findViewById(R.id.inputNumber);
saveButton = (Button)view.findViewById(R.id.saveButton);
deleteAllButton = (Button)view.findViewById(R.id.deleteAllButton);
openDB();
populateListView();
listViewItemCancel();
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (shouldWeEdit) {
myDB.updateRow(rowClickedForEdit,
inputName.getText().toString(),
inputNumber.getText().toString()
);
populateListView();
} else {
onClick_Add(v);
}
shouldWeEdit = false;
inputName.setText("");
inputNumber.setText("");
inputName.setHint("ENTER CONTACT NAME");
inputNumber.setHint("ENTER CONTACT NUMBER");
final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
}
});
deleteAllButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onClickDeleteAll(v);
shouldWeEdit = false;
inputName.setText("");
inputNumber.setText("");
inputName.setHint("ENTER CONTACT NAME");
inputNumber.setHint("ENTER CONTACT NUMBER");
}
});
myList = (ListView)view.findViewById(R.id.listViewFragment);
return view;
}
private void openDB(){
myDB = new DBAdapter(getActivity());
myDB.open();
}
public void onClick_Add(View v){
if (!TextUtils.isEmpty(inputName.getText().toString())){
myDB.insertRow(inputName.getText().toString(), inputNumber.getText().toString());
}else{
Toast.makeText(getActivity(), "Please Enter A Name", Toast.LENGTH_LONG).show();
}
populateListView();
}
public void populateListView(){
Cursor cursor = myDB.getAllRows();
String[] fromFieldNames = new String[] {DBAdapter.KEY_NAME,DBAdapter.KEY_NUMBER};
int[] toViewIDs = new int[] {R.id.customRowContactName,R.id.customRowContactNumber};
CursorAdapter cursorAdapter;
cursorAdapter = new CursorAdapter(getActivity(),R.layout.contacts_custom_row,cursor,fromFieldNames,toViewIDs);
myList = (ListView)view.findViewById(R.id.listViewFragment);
myList.setAdapter(cursorAdapter);
}
public void onClickDeleteAll(View v){
myDB.deleteAll();
populateListView();
}
private void listViewItemCancel(){
myList = (ListView) view.findViewById(R.id.listViewFragment);
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
inputName.setText("");
inputNumber.setText("");
inputName.setHint("ENTER CONTACT NAME");
inputNumber.setHint("ENTER CONTACT NUMBER");
shouldWeEdit = false;
populateListView();
}
});
}
class CursorAdapter extends SimpleCursorAdapter {
private Cursor c;
private Context context;
public CursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.c = c;
this.context = context;
}
@Override
public View getView(int pos, View inView, ViewGroup parent) {
View vix = inView;
if (vix == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vix = inflater.inflate(R.layout.contacts_custom_row, null);
}
this.c.moveToPosition(pos);
String name = this.c.getString(this.c.getColumnIndex(DBAdapter.KEY_NAME));
String num = this.c.getString(this.c.getColumnIndex(DBAdapter.KEY_NUMBER));
final String id = this.c.getString(this.c.getColumnIndex(DBAdapter.KEY_ROWID));
TextView textName = (TextView) vix.findViewById(R.id.customRowContactName);
textName.setText(name);
final TextView textNum = (TextView) vix.findViewById(R.id.customRowContactNumber);
textNum.setText(num);
final TextView textCancel = (TextView)vix.findViewById(R.id.customRowCancelText);
textCancel.setVisibility(View.INVISIBLE);
final TextView deleteText = (TextView)vix.findViewById(R.id.customRowDeleteText);
final TextView editInfo = (TextView)vix.findViewById(R.id.customRowEditText);
deleteText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myDB.deleteRow(Integer.valueOf(id));
populateListView();
}
});
editInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textCancel.setVisibility(View.VISIBLE);
inputName.setText("");
inputNumber.setText("");
inputName.setHint("ENTER NEW NAME");
inputNumber.setHint("ENTER NEW NUMBER");
rowClickedForEdit = Long.valueOf(id);
shouldWeEdit = true;
}
});
textNum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
URI uri;
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + textNum.getText().toString()));
startActivity(intent);
}
});
return vix;
}
}
public void retrieveDataFromCloud(String userEmail){
String whereClause = "userEmailID = '"+ userEmail +"'";
BackendlessDataQuery dataQuery = new BackendlessDataQuery();
dataQuery.setWhereClause( whereClause );
Backendless.Persistence.of(LocalPhoneNum.class).find(dataQuery, new AsyncCallback<BackendlessCollection<LocalPhoneNum>>(){
@Override
public void handleResponse( BackendlessCollection<LocalPhoneNum> foundContacts )
{
List<String> dataArrayList = new ArrayList<String>();
for (LocalPhoneNum temp : foundContacts.getData()){
dataArrayList.add(temp.getPhoneNum());
}
if (dataArrayList.isEmpty()){
Toast.makeText(getActivity(), "No Data Found", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getActivity(), dataArrayList.get(0), Toast.LENGTH_LONG).show();
}
}
@Override
public void handleFault( BackendlessFault fault )
{
Toast.makeText(getActivity(), "Error" , Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onPause() {
** ERROR OCCURS HERE
Cursor cursor = myDB.getAllRows();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
String data = cursor.getString(cursor.getColumnIndex("name"));
namesForWeb.add(data);
}
String userEmail = Backendless.UserService.CurrentUser().getEmail();
LocalPhoneNum localPhoneNum = new LocalPhoneNum(userEmail, "617 8", "mark");
super.onPause();
Backendless.Persistence.save(localPhoneNum, new AsyncCallback<LocalPhoneNum>() {
@Override
public void handleResponse(LocalPhoneNum localPhoneNum) {
Toast.makeText(getActivity().getApplicationContext(), "Saved To Cloud", Toast.LENGTH_LONG).show();
}
@Override
public void handleFault(BackendlessFault backendlessFault) {
Toast.makeText(getActivity().getApplicationContext(), "Not Saved To Cloud " + backendlessFault, Toast.LENGTH_LONG).show();
}
});
}}
有谁知道我为什么会收到此错误?应用程序只是做得太多了吗?谢谢!