起初,我的sendData方法正在工作,但是当我从另一个活动中调用它时,它“未附加到上下文”。错误。
当我打开它时,它看起来可以用,但是删除一些数据后,它是不起作用的。
这是我的代码。
DeleteDialog.java
public class DeleteDialog extends android.support.v4.app.DialogFragment {
private Context mContext;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_delete_dialog, container, false);
mContext=this.getContext();
...
ok_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(editText.getText().toString().equals("")){
Toast.makeText(getContext(), "과목명을 입력해 주세요", Toast.LENGTH_SHORT).show();
}else{
dbHelper = new DBHelper(mContext);
db = dbHelper.getWritableDatabase();
dbHelper.deleteColum(editText.getText().toString());
timeTableFragment.sendData(mContext);
getDialog().dismiss();
}
}
});
TimeTableFragment.java
public class TimeTableFragment extends Fragment {
private Context mContext;
public void sendData(Context context) { //get data and draw to layout
dbHelper = new DBHelper(context);
db = dbHelper.getWritableDatabase();
dbHelper.onCreate(db);
String date = "", start = "", end = "", title = "", color = "";
ArrayList<SubjectItem> timetable_data;
timetable_data = dbHelper.getDataTimetable(); //get data from sql
SimpleDateFormat sf = new SimpleDateFormat("HHmm");
int resID = 0;
int mok;
Date a, b;
long diff, min;
View view = getView();
for (int i = 0; i < timetable_data.size(); i++) { // draw to layout
date = timetable_data.get(i).getSub_date();
start = timetable_data.get(i).getSub_start();
end = timetable_data.get(i).getSub_end();
title = timetable_data.get(i).getSub_title();
color = timetable_data.get(i).getSub_color();
if (date.substring(0, 2).equals("Th")) {
date = "h";
Log.d("ㄹㄹ","목요일 : " +date);
}
date = date.toLowerCase().substring(0, 1);
Log.d("ㄹㄹ","바깥의 데이터 : " +date);
start = start.replace(":", "");
end = end.replace(":", "");
try {
a = sf.parse(start);
b = sf.parse(end);
diff = b.getTime() - a.getTime();
min = (diff / (1000 * 60));
mok = (int) min / 30;
Log.d(TAG,"title " +title + " start : " + start +","+ a.getTime()+ " / end : " + end+","+b.getTime() + " date : " + date);
for (int k = 0; k < mok; k++) {
if (mok != 1) {
if (end.substring(2, 4).equals("30")) {
end = Integer.toString(Integer.parseInt(end) - 30);
if (end.length() == 3) end = "0" + end;
Log.d(TAG, " 그리기 title : " +title );
//Log.d(TAG , "콘텍스트 : " + getContext().getPackageName());
Error -> resID = getResources().getIdentifier(date + end, "id", getContext().getPackageName());
TextView textView = (TextView) view.findViewById(resID);
textView.setText(title);
paintTimetable(textView,color);
} else {
end = Integer.toString(Integer.parseInt(end) - 70);
if (end.length() == 3) end = "0" + end;
Log.d("Contxet " , "콘텍스트 : " + getContext().getPackageName());
resID = getResources().getIdentifier(date + end, "id", getContext().getPackageName());
TextView textView = (TextView) view.findViewById(resID);
textView.setText(title);
paintTimetable(textView,color);
Log.d(TAG, " 그리기 title : " +title);
}
} else {
Log.d("Contxet " , "콘텍스트 : " + getContext().getPackageName());
resID = getResources().getIdentifier(date + start, "id", getContext().getPackageName());
TextView textView = (TextView) view.findViewById(resID);
textView.setText(title);
paintTimetable(textView,color);
}
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mContext=getContext();
} catch (ClassCastException e) {
Log.d(TAG, "Exception" + e.getMessage());
}
}
DBHelper.java
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
...
public void deleteColum(String name)
{
String DELETE = "DELETE FROM " + TABLE_TIMETABLE + " WHERE " + KEY_TITLE + "='" + name + "'";
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(DELETE);
Log.d("dd", "sql deleted : " + name);
}
错误消息:
Fragment TimeTableFragment{f4aa844} not attached to a context.
在DB插入中,删除是完美的方法,但是在DeleteDialog中调用sendData方法发生“未附加到上下文”之后。
我应该发送和接收什么上下文?
答案 0 :(得分:0)
所有数据库任务应在异步(另一个线程)中完成。可能需要一段时间,工作完成后,您应该返回主线程并检查视图是否仍然存在,这样可以避免内存泄漏和您提到的错误。
答案 1 :(得分:0)
使用s/[[:blank:]]+$//"
初始化DBHelper。这样可以确保您的应用具有适当的上下文,并且确实会导致您收到错误。
其他信息: 将您的DBHelper设置为Singleton,它将优化您的应用程序资源。