Android上下文在监听器类中不可用,阻止新意图的创建。

huangapple go评论67阅读模式
英文:

Android Context Unavailable In Listener Class Preventing New Intent Creation

问题

以下是翻译好的部分:

我是新手Android开发者在使用意图intents我有一个支付意图payment intent和一个支付监听器payment listener),我试图在监听器捕获到支付响应时触发另一个活动或意图问题是我无法在监听器类实现中获取上下文context)。

我尝试创建新意图的类是

public class PaymentConnectorListener implements IPaymentConnectorListener {
    @Override
    public void onSaleResponse(SaleResponse response) {
        String result;
        if (response.getSuccess()) {
            result = "Sale was successful";
        } else {
            result = "Sale was unsuccessful" + response.getReason() + ":" + response.getMessage();
        }
    }
}

我初始化监听器类的方式是

final IPaymentConnectorListener ccListener = new PaymentConnectorListener();

我使用监听器的地方是

paymentConnector = new PaymentConnector(this, account, ccListener, remoteApplicationId);
paymentConnector.initializeConnection();

我想在支付响应时触发一个意图但onSaleResponse()方法没有上下文context)。另外作为从Angular转换过来的人你能解释一下上下文是什么吗

请注意,由于代码部分不需要翻译,所以我只翻译了您提供的问题描述和相关文本。如果您需要更多帮助或有其他翻译需求,请随时提问。

英文:

I am new to android development and while working with intents, I have a payment intent and a payment listener and I am trying to fire another activity or intent when the listener picks up a payment response. The issue is, I am unable to get context int listener class implementation.

The class that I am trying to create a new intent in is:

public class PaymentConnectorListener implements IPaymentConnectorListener {
    @Override
    public void onSaleResponse(SaleResponse response) {
        String result;
        if (response.getSuccess()) {
            result = "Sale was successful";
        } else {
            result = "Sale was unsuccessful" + response.getReason() + ":" + response.getMessage();
        }
    }

}

How I initialize the listener class is:

    final IPaymentConnectorListener ccListener = new PaymentConnectorListener();

and where I use the listener is:

paymentConnector =  new PaymentConnector(this, account, ccListener, remoteApplicationId);
            paymentConnector.initializeConnection();

I want to fire an intent on payment response but the onSaleResponse() method does not have context. Also, switching from angular, can you explain a bit more of what context is.

答案1

得分: 0

你可以修改你的代码,在构造函数中包括Context参数:

public class PaymentConnectorListener implements IPaymentConnectorListener {
    private Context context;

    public PaymentConnectorListener(Context context) {
        this.context = context;
    }

    @Override
    public void onSaleResponse(SaleResponse response) {
        String result;
        if (response.getSuccess()) {
            result = "Sale was successful";
        } else {
            result = "Sale was unsuccessful" + response.getReason() + ":" + response.getMessage();
        }

        // 使用context启动一个新的活动或创建一个新的意图
        Intent intent = new Intent(context, YourActivity.class);
        intent.putExtra("result", result);
        context.startActivity(intent);
    }
}

在初始化PaymentConnectorListener时,确保传递适当的Context对象到它的构造函数中:

final IPaymentConnectorListener ccListener = new PaymentConnectorListener(this);

确保在初始化PaymentConnectorListener的范围内,"this" 引用的是一个有效的Context对象。

英文:

you can modify your code to include the Context parameter in the constructor

public class PaymentConnectorListener implements IPaymentConnectorListener {
    private Context context;

    public PaymentConnectorListener(Context context) {
        this.context = context;
    }

    @Override
    public void onSaleResponse(SaleResponse response) {
        String result;
        if (response.getSuccess()) {
            result = "Sale was successful";
        } else {
            result = "Sale was unsuccessful" + response.getReason() + ":" + response.getMessage();
        }

        // Use the context to start a new activity or create a new intent
        Intent intent = new Intent(context, YourActivity.class);
        intent.putExtra("result", result);
        context.startActivity(intent);
    }
}

When initializing the PaymentConnectorListener, pass the appropriate Context object to its constructor

final IPaymentConnectorListener ccListener = new PaymentConnectorListener(this);

Make sure that this refers to a valid Context object within the scope where you are initializing the PaymentConnectorListener

huangapple
  • 本文由 发表于 2023年6月9日 14:41:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定