0

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.

4

2 回答 2

0

我认为这是因为您从父活动而不是子活动调用 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.
}
于 2011-06-13T14:57:54.923 回答
0

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

于 2011-06-14T15:23:49.093 回答