Rasmni yuklash uchun onArticle yaratish tashqari getApplicationContext ()

Serverga rasm yuklamoqchiman. Lekin rasm bilan yuborgan foydalanuvchidan foydalanuvchi nomini yuborish kerak.

SharedPreferences-da foydalanuvchi nomi bilan yozildim, shuning uchun uni olishim mumkin deb o'ylayman:

public class UploadRequest extends StringRequest {
    private static final String REGISTER_REQUEST_URL = "http://160.128.0.10/up.php";
    private Map params;

    public UploadRequest(String image, String name, Response.Listener listener){
        super(Method.POST, REGISTER_REQUEST_URL, listener, null);

        SharedPreferences pref = getApplicationContext().getSharedPreferences("pref01", MODE_PRIVATE); 
// CANNOT RESOLVE SYMBOL getApplicationContext
        String user = pref.getString("username", null);

        params = new HashMap<>();
        params.put("image",image);
        params.put("name",name);
    }

    @Override
    public Map getParams() {
        return params;
    }
}

noto'g'ri emasmi? Ushbu foydalanuvchi nomini qanday qilib olishim mumkin?

0
Konstruktor parametri sifatida kontekstni qabul qilishingiz mumkin.
qo'shib qo'ydi muallif pRaNaY, manba
Konstruktor parametri sifatida kontekstni qabul qilishingiz mumkin.
qo'shib qo'ydi muallif pRaNaY, manba
getApplicationContext() funksiyasi. Uni ushbu vazifaga ega bo'lgan ob'ektga chaqirishingiz kerak. Shunday qilib, uni boshqa kontekstda izlash kerak. Bu erda haqiqiy hal Sizning konstruktoringizga kontekstni kiritishingiz kerak.
qo'shib qo'ydi muallif Gabe Sechan, manba

6 javoblar

getApplicationContext() can only be called from a subclass of Context, Activity is one of them which is why you can call getApplicationContext() from your Activity.

Siz nima qilishingiz kerak, yoki bu sinf uchun parametr sifatida Context ilovasida o'zingizning Ilova sinfida (tavsiya etilmaydi) boshlanadigan global Context yoki unga ega bo'lasiz.

3
qo'shib qo'ydi

getApplicationContext() can only be called from a subclass of Context, Activity is one of them which is why you can call getApplicationContext() from your Activity.

Siz nima qilishingiz kerak, yoki bu sinf uchun parametr sifatida Context ilovasida o'zingizning Ilova sinfida (tavsiya etilmaydi) boshlanadigan global Context yoki unga ega bo'lasiz.

3
qo'shib qo'ydi

Siz nima qilishingiz kerak, faqat asosiy faoliyat kontekstini uning konstruktori tomonidan ushbu klassga o'tkazing. Shunday qilib, konstruktorni yarat:

public UploadRequest(Context context, String image, String name, Response.Listener listener){
    super(Method.POST, REGISTER_REQUEST_URL, listener, null);

    SharedPreferences pref = context.getSharedPreferences("pref01", MODE_PRIVATE); 
// CANNOT RESOLVE SYMBOL getApplicationContext
    String user = pref.getString("username", null);

    params = new HashMap<>();
    params.put("image",image);
    params.put("name",name);
}

Va buni sizning faoliyatingizdan boshlanganda, this argumentini tadbir mazmuni sifatida kiriting. Shunga o'xshash narsa:

UploadRequest ur = new UploadRequest(this, OTHER_PARAMETERS_HERE);

Boshqalarning fikrlarida qayd etilganidek, sizning kodingiz ishlamaydi, chunki getApplicationContext() funktsiyasidir va uni faqat ushbu funktsiya belgilangan bo'lsa, qo'ng'iroq qilishingiz mumkin.

2
qo'shib qo'ydi
katta rahmat
qo'shib qo'ydi muallif Rick Joe, manba

Siz nima qilishingiz kerak, faqat asosiy faoliyat kontekstini uning konstruktori tomonidan ushbu klassga o'tkazing. Shunday qilib, konstruktorni yarat:

public UploadRequest(Context context, String image, String name, Response.Listener listener){
    super(Method.POST, REGISTER_REQUEST_URL, listener, null);

    SharedPreferences pref = context.getSharedPreferences("pref01", MODE_PRIVATE); 
// CANNOT RESOLVE SYMBOL getApplicationContext
    String user = pref.getString("username", null);

    params = new HashMap<>();
    params.put("image",image);
    params.put("name",name);
}

Va buni sizning faoliyatingizdan boshlanganda, this argumentini tadbir mazmuni sifatida kiriting. Shunga o'xshash narsa:

UploadRequest ur = new UploadRequest(this, OTHER_PARAMETERS_HERE);

Boshqalarning fikrlarida qayd etilganidek, sizning kodingiz ishlamaydi, chunki getApplicationContext() funktsiyasidir va uni faqat ushbu funktsiya belgilangan bo'lsa, qo'ng'iroq qilishingiz mumkin.

2
qo'shib qo'ydi
katta rahmat
qo'shib qo'ydi muallif Rick Joe, manba

Sizga getApplicationContext() dan foydalanish uchun ruxsat berilmaydi. shuning uchun xatoliklar android nuqtai nazaridan juda yaxshi.

Tarmoq chaqiruvlariga asosan, istalgan joyda kontekstli murojaatlarni ishlatmang. Ko'pincha kontekstlar Ui (Activity) bilan bog'liq. NullPointersExpetions ning qiyin vaqtlariga tushasiz.

Biznes:

Foydalanuvchi nomingizni parametr sifatida kiriting.

public class UploadRequest extends StringRequest {
private static final String REGISTER_REQUEST_URL = "http://160.128.0.10/up.php";
private Map params;

public UploadRequest(String image, String usernName,String name, Response.Listener listener){
    super(Method.POST, REGISTER_REQUEST_URL, listener, null);

    SharedPreferences pref = getApplicationContext().getSharedPreferences("pref01", MODE_PRIVATE); 
// CANNOT RESOLVE SYMBOL getApplicationContext
    String user = pref.getString("username", null);

    params = new HashMap<>();
    params.put("image",image);
    params.put("name",name);
}

@Override
public Map getParams() {
    return params;
}
}
1
qo'shib qo'ydi

Sizga getApplicationContext() dan foydalanish uchun ruxsat berilmaydi. shuning uchun xatoliklar android nuqtai nazaridan juda yaxshi.

Tarmoq chaqiruvlariga asosan, istalgan joyda kontekstli murojaatlarni ishlatmang. Ko'pincha kontekstlar Ui (Activity) bilan bog'liq. NullPointersExpetions ning qiyin vaqtlariga tushasiz.

Biznes:

Foydalanuvchi nomingizni parametr sifatida kiriting.

public class UploadRequest extends StringRequest {
private static final String REGISTER_REQUEST_URL = "http://160.128.0.10/up.php";
private Map params;

public UploadRequest(String image, String usernName,String name, Response.Listener listener){
    super(Method.POST, REGISTER_REQUEST_URL, listener, null);

    SharedPreferences pref = getApplicationContext().getSharedPreferences("pref01", MODE_PRIVATE); 
// CANNOT RESOLVE SYMBOL getApplicationContext
    String user = pref.getString("username", null);

    params = new HashMap<>();
    params.put("image",image);
    params.put("name",name);
}

@Override
public Map getParams() {
    return params;
}
}
1
qo'shib qo'ydi