Android的问题传递变量/意图活动变量、意图、问题、Android

2023-09-12 06:05:54 作者:一眼一青春

好了,所以我已搜查这个网站,发现如何将变量传递给另一个活动,意图很多教程,但由于某些原因,我还是感到有麻烦过去了,除了一个空值的任何事情。 logcat的告诉我:

Okay, so I have searched this site and found many tutorials on how to pass variables to another activity with an Intent, but for some reason I still am having trouble getting any thing passed except for a null value. Logcat tells me this:

E/AndroidRuntime(  747): java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.sourceway.tutorials.BetterTabs/eu.sourceway.tutorials.BetterTabs.addonsBrowser}: java.lang.NullPointerException

下面是我的出发活动:

        lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
          String content = links[position];
            Intent addonPage = new Intent(ThirdTab.this,addonsBrowser.class);
            Bundle b = new Bundle();
            b.putString("passed", "http://google.com");
            addonPage.putExtras(b);
            startActivity(addonPage);

      }

    }); 

下面是我的次要活动:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Bundle extras = getIntent().getExtras();

String URL = extras.getString("passed");

browser=new WebView(this);
browser.setWebViewClient(new WebViewClient());
browser.setInitialScale(55);

setContentView(browser);
browser.loadUrl(URL);



}

我将在Eclipse中断点,并在第二个活动观看了通过捆绑变量临时演员,它显示为空,这样的logcat的结果是什么导致得出结论,它不被通过。 此外,如果你在第一个活动,其实,我的网址,从一个ArrayList被拉最初,然后存储在名为内容的字符串变量,但不会通过其中。所以这就是为什么我的埋入式的http://google.com地址。我曾试图与其他的网址,仍然无法通过。我所知道的网络作品,因为我已经能够o在一个web视图加载页面。 这样做的主要目的是为了有一个具有从网站的链接列表,并与onItemClick,将URL传递到另一个活动具有web视图加载它。 URL的生成到ArraysList工作。我已经能够填充列表。现在的问题是,它的力量,一旦关闭,我点击一个链接,因为发送的变量为空。 任何想法是多少AP preciated。

I set a breakpoint in Eclipse and viewed the passed Bundle variable "extras" in the second Activity, and it show as null, so that and the logcat results are what lead to to conclude that it is not being passed. Also if you look in the first Activity, I actually have the URL being pulled from an ArrayList originally, and then stored in a String variable called content, but that will not pass either. So that is why I subbed the "http://google.com" address. I have tried this with other URLs and still can't pass it. I know the web works, as I have been able o load pages in a webview. The main purpose of this is to have a list that has links from a website, and with the onItemClick, pass the URL to another Activity that has a WebView to load it. The generating of the URLs into the ArraysList works. I have been able to populate the list. The problem is that it force closes as soon as I click on a link because the variable sent is null. Any ideas are much appreciated.

推荐答案

看看这个只是把这个code在你的第一个活动。注意的onclick: - >直接捕获的EditText DONOT保存在字符串编辑文本和然后通过它,如果你会做表演,然后字符串将无法通过。见下面的例子

"check this out just put this code in your onclick of first activity Note:->directly capture the edittext donot save the edit text in string and then pass it ,if you will do show then string will not pass. see the below example"

 public void onClick(View v) {

// TODO Auto-generated method stub
Intent i = new Intent(this,Web.class);
i.putExtra("epuzzle",urlenter.getText().toString());

final int result = 1;
startActivityForResult(i, result);
}

现在次活动把这个,注意: - >是你会尝试拯救活动的passeed字符串第一个到另一个字符串中的活动第二则是行不通的,你应该直接传递给使用loadURL而不是字符串保存和传递给使用loadURL。

"now in second activity put this ,Note:-> is you will try to save the passeed string of activity first in to another string in activity second then it will not work ,you should directly pass the string to loadurl instead of saving and passing to loadurl."

 Intent intent = getIntent();
 w.loadUrl(intent.getExtras().getString("epuzzle"));
 w.getSettings().setJavaScriptEnabled(true);

嘿,让我知道,如果它为你工作或没有。

plz let me know if it worked for you or not.