使用 ActivityGroup 在视图之间切换会导致 StackOverflowError
我有一个有 4 个不同选项卡的程序。
这些选项卡之一是 ActivityGroup,其中有一个 ListView。当我单击列表项之一时,它会切换到 WebActivity:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(TabActivity2.this, WebActivity.class);
Bundle b = new Bundle();
b.putString("URL", URLs[(int)id]);
b.putString("prevActivity", "TabActivity2");
intent.putExtras(b);
replaceContentView("web", intent);
}
});
}
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); this.setContentView(view);
}
所以现在我们位于 WebActivity 类中。代码如下:
public class WebActivity extends ActivityGroup {
WebView mWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
Bundle b = getIntent().getExtras();
String URL = b.getString("URL");
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.requestFocus();
mWebView.loadUrl(URL);
mWebView.setWebViewClient(new FirstTabWebViewClient());
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
Bundle b = getIntent().getExtras();
String retActivity = b.getString("prevActivity");
if (retActivity == "TabActivity2") {
Intent intent = new Intent(WebActivity.this, TabActivity2.class);
replaceContentView("list_webpages", intent);
return true;
}
return super.onKeyDown(keyCode, event);
}
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); this.setContentView(view);
}
我必须实现自己的 onKeyDown,因为否则单击“后退”按钮会导致关闭应用程序而不是返回到 ListView。
所以 StackOverflowError 似乎是我的程序中的结构问题,我不知道如何解决它。任何帮助将不胜感激。
I have a program with 4 different tabs.
One of these tabs is an ActivityGroup which has a ListView in it. When I click on one of the list items, it switches to WebActivity:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(TabActivity2.this, WebActivity.class);
Bundle b = new Bundle();
b.putString("URL", URLs[(int)id]);
b.putString("prevActivity", "TabActivity2");
intent.putExtras(b);
replaceContentView("web", intent);
}
});
}
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); this.setContentView(view);
}
So now we're in the WebActivity class. Here's the code:
public class WebActivity extends ActivityGroup {
WebView mWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
Bundle b = getIntent().getExtras();
String URL = b.getString("URL");
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.requestFocus();
mWebView.loadUrl(URL);
mWebView.setWebViewClient(new FirstTabWebViewClient());
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
Bundle b = getIntent().getExtras();
String retActivity = b.getString("prevActivity");
if (retActivity == "TabActivity2") {
Intent intent = new Intent(WebActivity.this, TabActivity2.class);
replaceContentView("list_webpages", intent);
return true;
}
return super.onKeyDown(keyCode, event);
}
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); this.setContentView(view);
}
I had to implement my own onKeyDown because otherwise hitting the Back button resulted in closing the application rather than returning to the ListView.
So the StackOverflowError seems to be a structural problem in my program and I'm not sure how to go about resolving it. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终在同一个 Activity 中同时使用了 FrameLayout 和 WebView 和 ListView,并使用可见性在它们之间进行切换。
I ended up using a FrameLayout in the same activity with both WebView and ListView and used visilibity to switch between them.