Context在支持类中具有null对象

时间:2016-10-27 15:35:15

标签: java android android-context

我尝试在另一个类中添加方法,这需要帮助我处理错误。当我需要烤面包时,我会收到错误。我该如何判断这种情况? 请帮帮我解释一下如何使用上下文。谢谢。

这是我的错误:

  

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.content.res.Resources android.content.Context.getResources()'

这是我的代码:

public class DataHelper extends BaseAppCompatActivity {
protected Intent intent;

/**
 * Выводит сумму услуги
 * @param price цена
 * @param discount скидка
 * @return
 */
public static Double Sum (double price, double discount){
    Double sum = null;
    if (price == 0 && discount == 0){
         return null;
    } else if (price == discount){
         sum = price;
    } else if (price > discount || price < discount){
         sum = price - discount;
    }
    return sum;
}

/**
 * Обработка ошибок
 * @param errorCode код ошибки
 * @param errorDescription описание ошибки
 */
public void processingError(String errorCode, String errorDescription){
    switch (errorCode){
        case "inside":
            Toast.makeText(getApplicationContext(), errorDescription, Toast.LENGTH_SHORT).show();
            break;
        case "method_not_found":
            Toast.makeText(getApplicationContext(), errorDescription, Toast.LENGTH_SHORT).show();
            break;
        case "method_access_denied":
            intent = new Intent(this, AuthorizationActivity.class);
            startActivity(intent);
            finish();
            Toast.makeText(getApplicationContext(), errorDescription, Toast.LENGTH_SHORT).show();
            break;
        case "bad_authorization":
            intent = new Intent(this, AuthorizationActivity.class);
            startActivity(intent);
            finish();
            Toast.makeText(getApplicationContext(), errorDescription, Toast.LENGTH_SHORT).show();
            break;
        case "bad_client_secret_or_id":
            intent = new Intent(this, AuthorizationActivity.class);
            startActivity(intent);
            finish();
            Toast.makeText(getApplicationContext(), errorDescription, Toast.LENGTH_SHORT).show();
            break;
        case "app_access_denied":
            intent = new Intent(this, AuthorizationActivity.class);
            startActivity(intent);
            finish();
            Toast.makeText(getApplicationContext(), errorDescription, Toast.LENGTH_SHORT).show();
            break;
        case "wrong_params":
            Toast.makeText(getApplicationContext(), errorDescription, Toast.LENGTH_SHORT).show();
            break;
        case "wrong_param_value":
            Toast.makeText(getApplicationContext(), errorDescription, Toast.LENGTH_SHORT).show();
            break;
        case "object_not_found":
            Toast.makeText(getApplicationContext(), errorDescription, Toast.LENGTH_SHORT).show();
            break;
    }
}

}

0 个答案:

没有答案