#android
#Android
Вопрос:
Я хочу вызвать метод в другом классе, но я получаю сообщение об ошибке в этой строке:
dbh=new DatabaseHelper(getApplicationContext());
Сообщение об ошибке:
«Метод getApplicationContext() не определен для типа add_point»
Комментарии:
1. где вы вызываете эту строку? В каком методе / классе
Ответ №1:
Если вы планируете использовать этот помощник в некоторых activity
неклассах, я бы посоветовал вам передать Context
его один раз с помощью установщика или определенного конструктора и сделать свой помощник статическим. Класс неактивности / обслуживания не знает о applicationContext
.
private static Context context;
public static void setContext(Context context){
DatabaseHelper.context = context;
}
Вы также можете использовать Singleton
шаблон, чтобы иметь один единственный экземпляр помощника.
Ответ №2:
у меня есть класс databsehelper , но вызов метода в новом классе показывает ошибку.
public class add_point extends TestCase {
SQLiteDatabase db;
DatabaseHelper dbh;
final long ONE_DAY = 24 * 60 * 60 * 1000L;
Calendar cal = Calendar.getInstance();
Hijri hij= new Hijri();
Monasebat mon=new Monasebat();
JalaliCalendar jl=new JalaliCalendar();
String event;
Date today = new Date();
dateConvert_Shamsi dshamsi =new dateConvert_Shamsi();
String text;
String DateFirst= "03/20/2016";
String DateEnde= "04/22/2016";
String df="03/20/2016";
String ef="04/22/2016";//42";
private ProgressDialog progressDialog;
private ProgressBar bar;
public void start2()
{
dbh=new DatabaseHelper(getApplicationContext());
//dbh=new DatabaseHelper(getApplicationContext());
for(int i=0;i<13;i )
{
try
{
switch (i) {
case 1:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_" String.valueOf(i),"روزکار","روزکار","روزکار","روزکار","روزکار","روزکار","روزکار");
break;
case 2:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_" String.valueOf(i),"فولادمبارکه","شیفت","A","روزکار","شب کار","استراحت اول","استراحت دوم");
break;
case 3:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_" String.valueOf(i),"فولادمبارکه","شیفت","B","استراحت اول","استراحت دوم","روزکار","شب کار");
break;
case 4:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_" String.valueOf(i),"فولادمبارکه","شیفت","C","شب کار","استراحت اول","استراحت دوم","روزکار");
break;
case 5:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_" String.valueOf(i),"فولادمبارکه","شیفت","D","استراحت دوم","روزکار","شب کار","استراحت اول");
break;
case 6:
//getDatesBetween(Dat"04/22/2016"irst,DateEnde,"tbl_Addshift_" String.valueOf(i),"فولادمبارکه","شیفت","نوبت کار","شیفت","شب کار","استراحت اول","استراحت دوم");
break;
/*case 7:
getDatesBetween("03/20/2016",ef,"tbl_Addshift_" String.valueOf(i),"ذوب آهن","روزکار","روزکار","روزکار","روزکار","روزکار","روزکار");
break;*/
case 7:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_" String.valueOf(i),"ذوب آهن","شیفت","1","روزکار","شب کار","استراحت اول","استراحت دوم");
break;
case 8:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_" String.valueOf(i),"ذوب آهن","شیفت","2","روزکار","شب کار","استراحت اول","استراحت دوم");
break;
case 9:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_" String.valueOf(i),"ذوب آهن","شیفت","3","روزکار","شب کار","استراحت اول","استراحت دوم");
break;
case 10:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_" String.valueOf(i),"ذوب آهن","شیفت","4","روزکار","شب کار","استراحت اول","استراحت دوم");
break;
case 11:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_" String.valueOf(i),"فولادمبارکه","شیفت","1نوبت کار","روزکار","شب کار","استراحت اول","استراحت دوم");
break;
case 12:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_" String.valueOf(i),"فولادمبارکه","شیفت","2نوبت کار","روزکار","شب کار","استراحت اول","استراحت دوم");
break;
case 13:
getDatesBetween("03/20/2016","04/22/2016","tbl_Addshift_" String.valueOf(i),"روزکار","روزکار","روزکار","روزکار","روزکار","روزکار","روزکار");
break;
}
}
catch (Exception e)
{
System.out.println(e "--------------start2--------------");
}
}
}
public void getDatesBetween(String startDate,String endDate,String tbl,String nameWork,String typeshift,String nameShift,String day1,String day2,String day3,String day4) {
dbh=new DatabaseHelper(getApplicationContext());
int DayShamsi = 0;
int MountShamsi;
int YearShamsi;
String txt_day_count="";
//Calendar mydate = Calendar.getInstance();
String DateJalali;
String DateJalali2 = null;
String DateJalali3 = null;
String datetime="";
try
{
long from=Date.parse(startDate);
long to=Date.parse(endDate);
int x=0;
int cont=1;
while(from <= to) {
x=x 1;
if (cont==1)
{
txt_day_count=day1;
cont ;
}
else if (cont==2)
{
txt_day_count=day2;
cont ;
}
else if(cont==3)
{
txt_day_count=day3;
cont ;
}
else if(cont==4)
{
txt_day_count=day4;
cont=1;
}
cal.setTime(new Date(from));
DateJalali=jl.getJalaliDate(new Date(from));
dshamsi.setGregorianDate(cal.get(cal.YEAR),cal.get(cal.MONTH) 1,cal.get(cal.DAY_OF_MONTH));
event=mon.JalaliMonasebat(cal.get(cal.MONTH) 1,cal.get(cal.DAY_OF_MONTH));
hij.miladi(new Date(from));
DateJalali2=hij.Dayhejri();
DateJalali3=String.valueOf(hij.JalaliTatil() "n" mon.JalaliTatil());
int tatile=0;
int tatileH=0;
tatile = mon.JalaliTatil();
tatileH = hij.JalaliTatil();
dbh.Add_Shift(datetime,String.valueOf(cal.getTime()),
nameWork,
typeshift,
nameShift,
txt_day_count,
String.valueOf(dshamsi.getIranianYear() "-" dshamsi.getIranianMonth()),//String.valueOf(jl.JalaliYear(cal.getTime())) "-" String.valueOf(jl.JalaliMonth(cal.getTime())),//DateJalali3 "-" DateJalali2,// String.valueOf(YearShamsi "-" MountShamsi),
String.valueOf(new Date(from)),
event "n" DateJalali2,
DateJalali3,
tbl,
String.valueOf(tatile "-" tatileH)
);
System.out.println(String.valueOf(new Date(from)) "-----------getDatesBetween-----------------");
from = ONE_DAY;
}
}
catch (Exception e)
{
System.out.println(e "-----------0000-getDatesBetween---------------- " "n" String.valueOf(datetime cal.getTime().toString() "-"
nameWork "-"
typeshift "-"
nameShift "-"
txt_day_count "-"
String.valueOf(dshamsi.getIranianYear() "-" dshamsi.getIranianMonth()) "-" //String.valueOf(jl.JalaliYear(cal.getTime())) "-" String.valueOf(jl.JalaliMonth(cal.getTime())),//DateJalali3 "-" DateJalali2,// String.valueOf(YearShamsi "-" MountShamsi),
String.valueOf(new Date(startDate)) "-"
event "n" DateJalali2 "-"
DateJalali3 "-"
tbl
String.valueOf("tatile" "-" "tatileH")));
}
}
public void add()
{
dbh=new DatabaseHelper(getApplicationContext());
for(int i=0 ; i<12 ; i )
{
try
{
switch (i) {
case 1:
dbh.AddRowtable("روزکار","روزکار","روزکار","tbl_Addshift_" String.valueOf(i));
break;
case 2:
dbh.AddRowtable("فولادمبارکه","شیفت","A","tbl_Addshift_" String.valueOf(i));
break;
case 3:
dbh.AddRowtable("فولادمبارکه","شیفت","B","tbl_Addshift_" String.valueOf(i));
break;
case 4:
dbh.AddRowtable("فولادمبارکه","شیفت","C","tbl_Addshift_" String.valueOf(i));
break;
case 5:
dbh.AddRowtable("فولادمبارکه","شیفت","D","tbl_Addshift_" String.valueOf(i));
break;
case 6:
dbh.AddRowtable("فولادمبارکه","شیفت","نوبت کار","tbl_Addshift_" String.valueOf(i));
break;
case 7:
dbh.AddRowtable("ذوب آهن","شیفت","1","tbl_Addshift_" String.valueOf(i));
break;
case 8:
dbh.AddRowtable("ذوب آهن","شیفت","2","tbl_Addshift_" String.valueOf(i));
break;
case 9:
dbh.AddRowtable("ذوب آهن","شیفت","3","tbl_Addshift_" String.valueOf(i));
break;
case 10:
dbh.AddRowtable("ذوب آهن","شیفت","4","tbl_Addshift_" String.valueOf(i));
break;
case 11:
dbh.AddRowtable("ذوب آهن","نوبت کار","1","tbl_Addshift_" String.valueOf(i));
break;
case 12:
dbh.AddRowtable("ذوب آهن","نوبت کار","2","tbl_Addshift_" String.valueOf(i));
break;
}
}
catch (Exception e)
{
System.out.println(e "--------------add--------------");
}
//dbh.AddRowtable(txt_ID.getText().toString(), txt_name.getText().toString(), txt_family.getText().toString());
}
}
}
Ответ №3:
Если использование Fragment тогда не поддерживает getApplicationContext(), он использует только getActivity() .
и еще использование активности, пожалуйста, обратитесь по этой ссылке:
http://www.androidhive.info/2013/09/android-sqlite-database-with-multiple-tables/