URL value is not passing to another activity

2019-08-02 17:07发布

问题:

This is an app which fetch a rss.xml file and shows the topic's headings n a list view.. When an item(topic heading) of listview is clicked, it is opening in webview in Contenurl activity. But the page is blank. It means "feedUri" is not passing the value to Contenturl.java If I replace "feedUri" with any address like http://google.com then it is working fine. Then if i click on any item of listview, google.com is opening in webview (within my app). But how can I pass the url value with "feedUri" ? Do I need to make any change in Contenurl.java file? plz help

This is the part of MainActivity.java

 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {


  Uri feedUri = Uri.parse(myRssFeed.getItem(position).getLink());
  Intent w = new Intent(this, Contenturl.class);
  w.putExtra(org.rss.mywindows.Contenturl.URL,feedUri);
  startActivity(w);

here is Contenturl.java

public class Contenturl extends Activity {

    public static final String URL = "";
    private static final String TAG = "WebscreenClass";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contenturl);

        String turl = getIntent().getStringExtra(URL);
        Log.i(TAG, " URL = "+turl);

        WebView webview = new WebView(this);
        setContentView(webview);

        // Simplest usage: No exception thrown for page-load error
        webview.loadUrl(turl);  

    }
}

回答1:

use

w.putExtra(org.rss.mywindows.Contenturl.URL,feedUri.toString());

instead of

w.putExtra(org.rss.mywindows.Contenturl.URL,feedUri);

and in your Contenturl Activity

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contenturl);

        String turl = getIntent().getStringExtra(URL);

        Uri feedUri = Uri.parse(turl);

because you are sending Uri from first Activity and receiving it as String in another Activity. then solution is send it as String from first activity



回答2:

Write below code instead of your onListItemClick() code.

Uri feedUri = Uri.parse(myRssFeed.getItem(position).getLink());
Intent w = new Intent(this, Contenturl.class);
w.putExtra("Uri",feedUri);
startActivity(w);

And Use below code for retrive url from intent in second activity.

Bundle bdl = getIntent().getExtras();
mUri = bdl.getString("Uri");


回答3:

in the activity you are pasing empty string as key

String turl = getIntent().getStringExtra(URL);

public static final String URL = "";