【问题标题】:different URL opening the same不同的网址打开相同的
【发布时间】:2015-10-16 22:39:51
【问题描述】:

所以,伙计们,在我的应用程序中,我构建了一个带有可扩展列表视图的菜单,其中一个组有 4 个孩子,一个打开 facebook 页面,另一个打开网站页面,一个 youtube 页面和一个 google+ 页面。但无论我在哪里点击它们都会打开 google+ 页面,我不明白为什么。代码如下:

public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            int pos = childPosition;
            //clique em contatos
            if(groupPosition == 5){
                switch(pos) {
                    //Clique em emails e contatos
                    case 0:
                        Intent i = new Intent(MainActivity.this, Contatos.class);
                        MainActivity.this.startActivity(i);
                }
            }
            //clique em hiperligacoes
            if(groupPosition == 3){
                switch(pos) {
                    //click no facebook
                    case 0:
                        Intent browserFace = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/moises.transporte.passageiros?fref=ts"));
                        startActivity(browserFace);
                    //click no site
                    case 1:
                        Intent browserSite = new Intent(Intent.ACTION_VIEW, Uri.parse("http://moises-transportes.pt/"));
                        startActivity(browserSite);
                    //click no youtube
                    case 2:
                        Intent browserYoutube = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/channel/UCXeHbISNnc0eLCPnTeolxLg"));
                        startActivity(browserYoutube);
                    //click no google+
                    case 3:
                        Intent browserGoogle = new Intent(Intent.ACTION_VIEW, Uri.parse("https://plus.google.com/111005531753993560637/about"));
                        startActivity(browserGoogle);
                }
            }

你们能帮我找出为什么会这样吗?

【问题讨论】:

  • 这个问题是不是取决于你测试时先点击哪个的顺序?
  • 不,我已经得到了答案,我错过了休息时间。但是非常感谢您的宝贵时间!

标签: android url android-studio expandablelistview


【解决方案1】:

break.

在每个 case 语句之后使用 break。 Orelse所有的情况下,正确的一个接一个地执行。

     if(groupPosition == 3){


             switch(pos) {
                    //click no facebook
                    case 0:
                        Intent browserFace = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/moises.transporte.passageiros?fref=ts"));
                        startActivity(browserFace);
                        break;
                    //click no site
                    case 1:
                        Intent browserSite = new Intent(Intent.ACTION_VIEW, Uri.parse("http://moises-transportes.pt/"));
                        startActivity(browserSite);
                        break;
                    //click no youtube
                    case 2:
                        Intent browserYoutube = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/channel/UCXeHbISNnc0eLCPnTeolxLg"));
                        startActivity(browserYoutube);
                    //click no google+
                        break;
                    case 3:
                        Intent browserGoogle = new Intent(Intent.ACTION_VIEW, Uri.parse("https://plus.google.com/111005531753993560637/about"));
                        startActivity(browserGoogle);
                        break;
                }
     }

【讨论】:

  • 你说得对,非常感谢,我完全忘记了。您能否也回答与这段代码有关的另一个问题。使用该代码,我可以在外部浏览器 chrome 中打开 URL。有没有办法让我在应用程序中打开它?就像,如果我在应用程序中使用主详细信息流 url 打开,但在这里我使用的是可扩展列表视图,有没有办法可以做到?
  • 您需要为此使用 WebView。在 google 上查找如何使用它。有很多很好的教程。
【解决方案2】:

你忘记在 switch 中打破你的案例了。在为每个案例编写下一个案例之前,先写 break;。另外,添加一个默认情况。

【讨论】:

  • 你说得对,非常感谢,我完全忘记了。您能否也回答与这段代码有关的另一个问题。使用该代码,我可以在外部浏览器 chrome 中打开 URL。有没有办法让我在应用程序中打开它?就像,如果我在应用程序中使用主详细信息流 url 打开,但在这里我使用的是可扩展列表视图,有没有办法可以做到?
猜你喜欢
  • 1970-01-01
  • 2020-12-03
  • 1970-01-01
  • 2016-12-23
  • 2018-11-13
  • 1970-01-01
  • 1970-01-01
  • 2010-10-22
  • 1970-01-01
相关资源
最近更新 更多