使用 ActivityGroup 在视图之间切换会导致 StackOverflowError

发布于 2024-12-10 22:31:36 字数 2521 浏览 0 评论 0原文

我有一个有 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 技术交流群。

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

发布评论

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

评论(1

凑诗 2024-12-17 22:31:36

我最终在同一个 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.

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