gpt4 book ai didi

android - 调用 getOwnerActivity() 方法总是返回 null

转载 作者:行者123 更新时间:2023-11-30 00:33:07 26 4
gpt4 key购买 nike

我创建了一个简单的自定义对话框类。在进一步的代码中,我想运行新的 Intent:

 Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=20.344,34.34&daddr=20.5666,45.345"));
startActivity(intent);

但问题是,每当我调用更改为 Intent 时,我总是在 getOwnerActivity() 中得到 null - 如何正确调用它方法?

public class AddToQueueDialog extends Dialog implements View.OnClickListener {

Activity mActivity;
private final String android_id = Settings.Secure.getString(getContext().getContentResolver(),
Settings.Secure.ANDROID_ID);

public Activity getmActivity() {
return mActivity;
}

public void setmActivity(Activity mActivity) {
this.mActivity = mActivity;
}

public AddToQueueDialog(Context context, WashLocation washLocation) {
super(context);
setWashLocation(washLocation);
setmActivity(getOwnerActivity());
}

最佳答案

如果您检查源代码,它返回的 Activity 仅在 setOwnerActivity(Activity activity) 中设置,不会在任何地方调用。因此,如果您希望 getOwnerActivity() 返回不同于 null 的值,则必须像下面这样更改您的构造函数

 public AddToQueueDialog(Context context, WashLocation washLocation) {
super(context);
if (context instanceof Activity) {
setOwnerActivity((Activity) context);
}
setWashLocation(washLocation);
setmActivity(getOwnerActivity());
}

关于android - 调用 getOwnerActivity() 方法总是返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43930910/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com