父类中的 onClick 子活动触发

发布于 2024-11-15 10:39:58 字数 686 浏览 3 评论 0原文

我在 ActivityGroup 的子活动中使用 onClick 处理程序时遇到一些问题。

我正在使用以下方式启动 StoreActivity:

Intent storeIntent = new Intent(this, StoreActivity.class);
storeIntent.putExtra(StoreActivity.INTENT_STORE_ID, storeId);

View newVeiw = getLocalActivityManager().startActivity("StoreActivity", storeIntent).getDecorView();
setContentView(newVeiw);

Log.e("DEBUG", "current activity: " + getLocalActivityManager().getCurrentActivity().toString());

在 StoreActivity 布局中,我有一个定义 onClick 方法的按钮。然而,由于某种原因,它试图在启动 StoreActivity 的父类中调用它。我在启动活动时做错了什么吗?上面 Log.e 的输出表明 StoreActivity 是当前活动,所以我有点不明白为什么会发生这种情况。我可以通过为 StoreActvity 中的代码中的按钮定义 onClickListener 来解决此问题,但我希望尽可能避免这种情况。

I am having some trouble with the onClick handler in a sub activity of an ActivityGroup.

I am launching a StoreActivity using:

Intent storeIntent = new Intent(this, StoreActivity.class);
storeIntent.putExtra(StoreActivity.INTENT_STORE_ID, storeId);

View newVeiw = getLocalActivityManager().startActivity("StoreActivity", storeIntent).getDecorView();
setContentView(newVeiw);

Log.e("DEBUG", "current activity: " + getLocalActivityManager().getCurrentActivity().toString());

In the StoreActivity layout I have a button which defines an onClick method. For some reason however, it is trying to call this in the parent class that launched StoreActivity. Am I doing something wrong when launching the activity? The output of Log.e above says that StoreActivity is the current activity so I am a bit lost as to why this is happening. I can get around this by defining an onClickListener for the button in code in StoreActvity but I would like to avoid that if possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我很坚强 2024-11-22 10:39:58

我认为这是因为您是从父活动而不是子活动调用 setContentView 。为什么不直接在意图中启动活动并在新活动中设置内容视图?那就简单多了。

试试这个:

Intent storeIntent = new Intent(this, StoreActivity.class);
storeIntent.putExtra(StoreActivity.INTENT_STORE_ID, storeId);
startActivity(storeIntent);

然后在 StoreActivity.java 中执行以下操作:

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   View newVeiw = getLocalActivityManager().startActivity("StoreActivity", storeIntent).getDecorView();

    setContentView(newView); //not sure if this would work, would probably be easier to put your xml layout file in here.
}

I think this is because you are calling setContentView from the parent activity instead of the subactivity. Why don't you just start the activity in the intent instead and set the content view in the new activity? It would be much simpler.

Try this:

Intent storeIntent = new Intent(this, StoreActivity.class);
storeIntent.putExtra(StoreActivity.INTENT_STORE_ID, storeId);
startActivity(storeIntent);

and then in StoreActivity.java do:

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   View newVeiw = getLocalActivityManager().startActivity("StoreActivity", storeIntent).getDecorView();

    setContentView(newView); //not sure if this would work, would probably be easier to put your xml layout file in here.
}
情话难免假 2024-11-22 10:39:58

好吧,我已经解决了这个问题。该问题与任何此代码无关。我的活动有一个共同的基类,并且我不小心将充气机设置为单例。这意味着所有膨胀的布局都属于创建该单例实例的第一个类,该类恰好是错误接收 onClick 事件的类。删除该单例解决了这个问题。

Ok I have solved this. The problem was unrelated to any of this code. I had a common base class to my activities and in that I had accidentally made the inflater a singleton. This meant that all inflated layouts belonged to the first class that created that singleton instance which happened to be the class that was incorrectly receiving the onClick event. Removing that singleton resolved this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文