父类中的 onClick 子活动触发
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这是因为您是从父活动而不是子活动调用 setContentView 。为什么不直接在意图中启动活动并在新活动中设置内容视图?那就简单多了。
试试这个:
然后在 StoreActivity.java 中执行以下操作:
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:
and then in StoreActivity.java do:
好吧,我已经解决了这个问题。该问题与任何此代码无关。我的活动有一个共同的基类,并且我不小心将充气机设置为单例。这意味着所有膨胀的布局都属于创建该单例实例的第一个类,该类恰好是错误接收 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.