Android Problems Pass variable/intent to an activity
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.
"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.
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);
精彩评论