![](/img/trans.png)
[英]requestWindowFeature() in External Class by Passing Activity Context
[英]Passing Activity context to Class file
我創建了兩個類(DataBase,addItem到數據庫類)和一個活動。 我不知道如何將Activity類傳遞給方法。 AndroidStudio會拋出錯誤。 我試圖使用getApplicationContext(),但沒有用。 這是代碼:
DataBase文件
public DataBase(Context context) {
super(context, DATABASE_NAME, null, TABLE_VERSION); }
DodajNadgranieClass(片段)文件
btZapisz.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String radiobutton = "", bruttoValue ="", data, tytul;
SharedPreferences pref = getSharedPreferences("prefs", MODE_PRIVATE);
data = edPodajDate.getText().toString();
tytul = edPodajTytul.getText().toString();
if (edPodajDate.getText().toString().isEmpty() || edPodajTytul.getText().toString().isEmpty() || radioGroup.getCheckedRadioButtonId() == -1) {
Toast.makeText(DodajNadgranieActivity.this, "Musisz podać wszystkie dane", Toast.LENGTH_SHORT).show();
} else {
if (rbOperetka.isChecked()) {
radiobutton = "1";
bruttoValue = pref.getString("operetka", "");
} else if (rbMusical.isChecked()) {
radiobutton = "2";
bruttoValue = pref.getString("musical", "");
} else if (rbKoncert.isChecked()) {
radiobutton = "3";
bruttoValue = pref.getString("koncert", "");
} else if (rbBajka.isChecked()) {
radiobutton = "4";
bruttoValue = pref.getString("bajka", "");
} else if (rbOpera.isChecked()) {
radiobutton = "5";
bruttoValue = pref.getString("opera", "");
} else if (rbInne.isChecked()) {
radiobutton = "6";
bruttoValue = pref.getString("inne", "");
}
ObliczPodatki obliczPodatki = new ObliczPodatki();
obliczPodatki.obliczPodatki(bruttoValue, data, tytul, radiobutton);
Toast.makeText(DodajNadgranieActivity.this, "DODANO", Toast.LENGTH_SHORT).show();
radioGroup.clearCheck();
edPodajTytul.setText(null);
edPodajDate.setText(null);
和活動文件
public class ObliczPodatki extends Application {
public void obliczPodatki (String brutto, String data, String tytul, String radiobutton) {
kwota_brutto = Double.parseDouble(brutto);
kwota_netto = kwota_brutto * KWOTA_NETTO;
kwota_ubezpieczenie_emerytalne = kwota_brutto * UBEZPIECZENIE_EMERYTALNE;
kwota_ubezpieczenie_rentowe = kwota_brutto * UBEZPIECZENIE_RENTOWE;
kwota_ubezpieczenie_zdrowotne = kwota_brutto * UBEZPIECZENIE_ZDROWOTNE;
kwota_ubezpieczenie_chorobowe = kwota_brutto * UBEZPIECZENIE_CHOROBOWE;
kwota_zaliczka_pit = kwota_brutto * ZALICZKA_PIT;
sBrutto = "" + kwota_brutto;
sNetto = "" + kwota_netto;
sEmerytalne = "" + kwota_ubezpieczenie_emerytalne;
sRentowe = "" + kwota_ubezpieczenie_rentowe;
sZdrowotne = "" + kwota_ubezpieczenie_zdrowotne;
sChorobowe = "" + kwota_ubezpieczenie_chorobowe;
sPit = "" + kwota_zaliczka_pit;
DataBase myDb = new DataBase(this);
myDb.addNewNadgranie(data, tytul, radiobutton, sBrutto, sNetto, sEmerytalne, sRentowe, sZdrowotne, sChorobowe, sPit);
}
}
您正在使用Application而不是Activity類。 請改為:
public class ObliczPodatki extends AppCompatActivity
您可以將obliczPodatki
的功能obliczPodatki
為:
public void obliczPodatki (Activity activity, String brutto, String data, String tytul, String radiobutton) {
用它來調用:
obliczPodatki.obliczPodatki((DodajNadgranieActivity)v.getContext(),bruttoValue, data, tytul, radiobutton);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.