【问题标题】:How to pass an int value to another class [duplicate]如何将int值传递给另一个类[重复]
【发布时间】:2010-10-15 20:13:14
【问题描述】:

可能重复:
Passing a Int value to another class

我一直试图让“getExtra”工作但没有成功,我有一个列表视图有 4 个选项,它们在 webView 类中启动一个网页,当用户选择选项时让我们说选项 3 我想传递值3 到 webView 类,以便它加载正确的网页,目前我得到的只是错误,有人可以帮忙解决我哪里出错了。

这是我的意图

         if (position == 0)  {
                Intent intent = new Intent(this, official.class);
                startActivity(intent);intent.putExtra(webb = 3);}

这是我要加载的网页的官方类中的getextras代码

Bundle webb = getIntent().getExtras(),

variableGet = webb.getInt(webb);

if (webb == 2)      mWebView.loadUrl("http://bcafc.livewwware.co.uk/viewforum.php?f=7&sid=009c462b00069f307ef6dcd09e747f7c");
  if (webb == 3)   mWebView.loadUrl("http://www.bbc.co.uk");
  mWebView.setWebViewClient(new HelloWebViewClient());

提前致谢

【问题讨论】:

  • 看起来好像您在添加额外内容之前是带着意图开始活动的。还是我错过了什么?
  • 我已经改变了它在错误方面没有区别
  • 如果你遇到这个问题,你真的应该重新考虑编程。 1.)您在开始活动后设置额外内容。 2.) 当网络是Bundle 时,您使用的是web == 2
  • 法尔马里。如果这对您来说是个问题,我深表歉意我习惯的编程语言。

标签: java android android-intent


【解决方案1】:

"如果 (位置 == 0)
{ Intent intent = new Intent(this, official.class); startActivity(intent);intent.putExtra(webb = 3); }"

你的想法是对的。您想在开始活动之前初始化您的意图并获取/设置参数,如下所示:


Intent intent = new Intent(this, official.class); Intent.putExtra("weburl", 3); startActivity(intent);

然后在你开始的活动中,你会得到这样的“weburl”:


int number = getIntent().getIntExtra("weburl", 0);

if (number == 2) mWebView.loadUrl("http://bcafc....."); else if (number == 3) mWebView.loadUrl("http://www.bbc..");

【讨论】:

  • 谢谢,我想我的问题是我在 Visual Basic 语言中的想法太多了。
【解决方案2】:

再一次,您可能需要发布更多代码才能获得有用的答案。不过,您的第一个代码块至少应该重写为:

if (position == 0)  {
    Intent intent = new Intent(this, official.class);
    webb = 3;
    intent.putExtra("webb", webb);
    startActivity(intent);
}

然后进行检索,例如:

int webb = getIntent().getIntExtra("webb", -1);

您可能可以像以前一样继续使用捆绑包,但我很惊讶第一个代码块为您编译,我认为这段代码更容易理解。无论如何,您需要在开始活动之前设置 webb 值。

【讨论】:

    【解决方案3】:

    试试这个:

    Intent intent = new Intent(this, official.class);
    intent.putExtra("webb", 3);
    startActivity(intent);
    

    然后

    Bundle extras = this.getIntent().getExtras();
    int webb = extras.getInt("webb");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-24
      • 2021-03-20
      • 1970-01-01
      相关资源
      最近更新 更多