0

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

Here is my starting Activity:

        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);

      }

    }); 

Here is my Secondary Activity:

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);



}

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.

luskbo
  • 155
  • 3
  • 18
  • No need to create a new Bundle, just do addonPage.putExtra("passed", "http://www.google.com/");. This definitely works, I pass Strings and ints between activities all the time. – Reuben Scratton Jun 04 '11 at 09:01
  • Seems I have a bigger issue now. Tried you suggestion and the ones below, and now for some reason, when I run the app,I have a white screen that overlays the entire app. I see my app for a split second, and then I get a white screen on top of it. It is very strange, the changes mentioned here were very minor, so I reverted the changes, and still get the same results. I can run other apps that I have created, but not this one any more. Hopefully if I can figure this out, I can see if your suggestion works. Thanks for the help though. – luskbo Jun 04 '11 at 14:35
  • So if you just sub in a URL in the second activity it works? – manno23 Jun 04 '11 at 08:30
  • No, it doesn't read the URL by itself. – luskbo Jun 04 '11 at 13:32

2 Answers2

1

Try this one in my starting Activity:

Intent addonPage = new Intent(ThirdTab.this,addonsBrowser.class);

addonPage.putExtra("passed", "http://google.com");

startActivityForResult(addonPage, 0);

Try this one in my Secondary Activity:

private String message;

message = getIntent().getExtras().getString(passed);
Muhammad Usman
  • 12,439
  • 6
  • 36
  • 59
Balaji Khadake
  • 3,449
  • 4
  • 24
  • 38
1

"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);
}

"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.

aditya
  • 353
  • 1
  • 2
  • 12