英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论